Descriptor

Trait Descriptor 

Source
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:

  1. Round-trip consistency: For any valid descriptor content, parse(content).to_descriptor_string() should produce semantically equivalent content (though whitespace may differ).

  2. Digest stability: The digest() method must return consistent results for the same descriptor content.

  3. Error handling: parse() should return Error::Parse for 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

Required Methods§

Source

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.

Source

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().

Source

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 use
  • encoding - The output encoding format
§Errors

Returns an error if the digest cannot be computed (e.g., if the descriptor content is invalid).

Source

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.

Source

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.

Implementors§