pub trait Descriptor: Sized {
// Required methods
fn parse(content: &str) -> Result<Self, Error>;
fn to_descriptor_string(&self) -> String;
fn digest(
&self,
hash: DigestHash,
encoding: DigestEncoding,
) -> Result<String, Error>;
fn raw_content(&self) -> &[u8] ⓘ;
fn unrecognized_lines(&self) -> &[String];
}Expand description
Trait for parsing and serializing Tor descriptors.
This trait defines the common interface for all descriptor types in the library. Implementors can parse descriptor content, serialize back to the canonical string format, and compute cryptographic digests.
§Contract
Implementations must satisfy these invariants:
-
Round-trip consistency: For any valid descriptor content,
parse(content).to_descriptor_string()should produce semantically equivalent content (though whitespace may differ). -
Digest stability: The
digest()method must return consistent results for the same descriptor content. -
Error handling:
parse()should returnError::Parsefor malformed content with a descriptive error message.
§Example
use stem_rs::descriptor::{Descriptor, DigestHash, DigestEncoding};
use stem_rs::descriptor::ServerDescriptor;
let content = "router example 127.0.0.1 9001 0 0\n...";
let descriptor = ServerDescriptor::parse(content).unwrap();
// Serialize back to string
let serialized = descriptor.to_descriptor_string();
// Compute digest
let digest = descriptor.digest(DigestHash::Sha1, DigestEncoding::Hex).unwrap();
// Access raw content
let raw = descriptor.raw_content();
// Check for unrecognized lines
let unknown = descriptor.unrecognized_lines();§Implementors
ServerDescriptor- Server descriptorsMicrodescriptor- MicrodescriptorsExtraInfoDescriptor- Extra-info descriptorsNetworkStatusDocument- Consensus documents
Required Methods§
Sourcefn parse(content: &str) -> Result<Self, Error>
fn parse(content: &str) -> Result<Self, Error>
Parses a descriptor from its string content.
§Arguments
content- The descriptor content as a string
§Errors
Returns Error::Parse if the content is malformed or missing
required fields.
Sourcefn to_descriptor_string(&self) -> String
fn to_descriptor_string(&self) -> String
Serializes the descriptor to its canonical string format.
The output should be valid descriptor content that can be parsed
again with parse().
Sourcefn digest(
&self,
hash: DigestHash,
encoding: DigestEncoding,
) -> Result<String, Error>
fn digest( &self, hash: DigestHash, encoding: DigestEncoding, ) -> Result<String, Error>
Computes the cryptographic digest of the descriptor.
The digest is computed over the appropriate portion of the descriptor content (which varies by descriptor type).
§Arguments
hash- The hash algorithm to useencoding- The output encoding format
§Errors
Returns an error if the digest cannot be computed (e.g., if the descriptor content is invalid).
Sourcefn raw_content(&self) -> &[u8] ⓘ
fn raw_content(&self) -> &[u8] ⓘ
Returns the raw bytes of the original descriptor content.
This is the exact content that was parsed, preserving original formatting and whitespace.
Sourcefn unrecognized_lines(&self) -> &[String]
fn unrecognized_lines(&self) -> &[String]
Returns lines from the descriptor that were not recognized.
These are lines that don’t match any known keyword for this descriptor type. This is useful for forward compatibility when new fields are added to the descriptor format.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.