pub struct Encoder { /* private fields */ }Expand description
A bounded, state-machine driven encoder.
The Encoder maintains a stack of open scopes to enforce structural strictness and automatically back-patch length headers.
§Structural Invariants
All write methods validate the operation against the current Scope.
Returns an Error if the write violates the following rules:
- Map Scopes: Only
Tag::Variantitems may be written. - ADT Scopes (Option, Result, Variant): Exactly one item must be written. Attempts to write >1 item or close the scope with 0 items will fail.
- Root Scope: The encoder must end in the Root scope to finalize bytes.
Implementations§
Source§impl Encoder
impl Encoder
Sourcepub fn into_bytes(self) -> Result<Vec<u8>>
pub fn into_bytes(self) -> Result<Vec<u8>>
Consumes the encoder and returns the final byte vector.
§Errors
Returns Error::ScopeStillOpen if the stack depth > 1.
Sourcepub fn option_none(&mut self) -> Result<()>
pub fn option_none(&mut self) -> Result<()>
Encodes Option::None.
Sourcepub fn append_raw(&mut self, v: &[u8]) -> Result<()>
pub fn append_raw(&mut self, v: &[u8]) -> Result<()>
Appends pre-encoded neopack bytes directly to the buffer.
This is used to inject already-encoded data (like a pre-encoded list of values) into the stream without re-encoding. The caller must ensure the bytes are valid neopack-encoded data.
Sourcepub fn list_begin(&mut self) -> Result<()>
pub fn list_begin(&mut self) -> Result<()>
Sourcepub fn map_begin(&mut self) -> Result<()>
pub fn map_begin(&mut self) -> Result<()>
Begins a Map container.
§Invariants
- Must be closed via
map_end(). - Strict: Only
variant_begin()(Key/Value pair) is allowed as a direct child.
Sourcepub fn option_some_begin(&mut self) -> Result<()>
pub fn option_some_begin(&mut self) -> Result<()>
Begins an Option::Some container.
§Invariants
- Must be closed via
option_some_end(). - Strict: Requires exactly one item to be written.
Sourcepub fn option_some_end(&mut self) -> Result<()>
pub fn option_some_end(&mut self) -> Result<()>
Ends an Option::Some container.
Sourcepub fn result_ok_begin(&mut self) -> Result<()>
pub fn result_ok_begin(&mut self) -> Result<()>
Begins a Result::Ok container.
§Invariants
- Must be closed via
result_ok_end(). - Strict: Requires exactly one item to be written.
Sourcepub fn result_ok_end(&mut self) -> Result<()>
pub fn result_ok_end(&mut self) -> Result<()>
Ends a Result::Ok container.
Sourcepub fn result_err_begin(&mut self) -> Result<()>
pub fn result_err_begin(&mut self) -> Result<()>
Begins a Result::Err container.
§Invariants
- Must be closed via
result_err_end(). - Strict: Requires exactly one item to be written.
Sourcepub fn result_err_end(&mut self) -> Result<()>
pub fn result_err_end(&mut self) -> Result<()>
Ends a Result::Err container.
Sourcepub fn variant_begin(&mut self, name: &str) -> Result<()>
pub fn variant_begin(&mut self, name: &str) -> Result<()>
Begins a Variant (Named Payload).
Encodes the name string immediately.
§Invariants
- Must be closed via
variant_end(). - Strict: Requires exactly one item (the payload) to be written after this call.
Sourcepub fn variant_end(&mut self) -> Result<()>
pub fn variant_end(&mut self) -> Result<()>
Ends a Variant.