pub struct GenericLayerBuilder { /* private fields */ }Expand description
Builder for dynamically-defined custom protocol layers.
This builder is intended to be constructed once per protocol definition
(e.g. in Python via the PyO3 bindings) and then used to produce byte
buffers that conform to the protocol layout.
§Example
use std::sync::Arc;
use stackforge_core::layer::generic::builder::GenericLayerBuilder;
use stackforge_core::layer::generic::GenericFieldDesc;
use stackforge_core::layer::field::FieldType;
let fields = Arc::new(vec![
GenericFieldDesc {
name: "type_id".to_string(),
offset: 0,
size: 2,
field_type: FieldType::U16,
default_value: vec![0x00, 0x01],
},
GenericFieldDesc {
name: "flags".to_string(),
offset: 2,
size: 1,
field_type: FieldType::U8,
default_value: vec![0x00],
},
]);
let buf = GenericLayerBuilder::new(Arc::from("MyProto"), fields)
.set_u16("type_id", 0x0042)
.build();
assert_eq!(buf.len(), 3);
assert_eq!(buf[0], 0x00);
assert_eq!(buf[1], 0x42);
assert_eq!(buf[2], 0x00);Implementations§
Source§impl GenericLayerBuilder
impl GenericLayerBuilder
Sourcepub fn new(name: Arc<str>, field_descs: Arc<Vec<GenericFieldDesc>>) -> Self
pub fn new(name: Arc<str>, field_descs: Arc<Vec<GenericFieldDesc>>) -> Self
Create a builder for the named protocol with the given field descriptors.
Sourcepub fn set(self, name: &str, value: Vec<u8>) -> Self
pub fn set(self, name: &str, value: Vec<u8>) -> Self
Set a field by raw bytes.
If value is shorter than the field’s declared size it is zero-padded
(on the right); if it is longer it is truncated.
Sourcepub fn set_u16(self, name: &str, value: u16) -> Self
pub fn set_u16(self, name: &str, value: u16) -> Self
Set a U16 field by name (stored big-endian).
Sourcepub fn set_u32(self, name: &str, value: u32) -> Self
pub fn set_u32(self, name: &str, value: u32) -> Self
Set a U32 field by name (stored big-endian).
Sourcepub fn header_size(&self) -> usize
pub fn header_size(&self) -> usize
Total byte size of the protocol header (sum of all field sizes).
Sourcepub fn build(&self) -> Vec<u8> ⓘ
pub fn build(&self) -> Vec<u8> ⓘ
Build the layer into a byte buffer.
Fields not explicitly set fall back to their default_value.
Each field value is copied into the buffer starting at field.offset,
padded with zeros on the right or truncated if the value’s length does
not match field.size.
Sourcepub fn build_layer(&self) -> (GenericLayer, Vec<u8>)
pub fn build_layer(&self) -> (GenericLayer, Vec<u8>)
Build the byte buffer and wrap it in a GenericLayer.
The returned layer’s LayerIndex covers [0..header_size] within the
returned byte buffer.
Trait Implementations§
Source§impl Clone for GenericLayerBuilder
impl Clone for GenericLayerBuilder
Source§fn clone(&self) -> GenericLayerBuilder
fn clone(&self) -> GenericLayerBuilder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for GenericLayerBuilder
impl RefUnwindSafe for GenericLayerBuilder
impl Send for GenericLayerBuilder
impl Sync for GenericLayerBuilder
impl Unpin for GenericLayerBuilder
impl UnsafeUnpin for GenericLayerBuilder
impl UnwindSafe for GenericLayerBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more