pub trait Format: Send + Sync {
// Required method
fn name(&self) -> &str;
// Provided methods
fn description(&self) -> &str { ... }
fn file_extensions(&self) -> &[&str] { ... }
fn supports_parsing(&self) -> bool { ... }
fn supports_serialization(&self) -> bool { ... }
fn parse(&self, _source: &str) -> Result<Document, FormatError> { ... }
fn serialize(&self, _doc: &Document) -> Result<String, FormatError> { ... }
fn serialize_with_options(
&self,
doc: &Document,
options: &HashMap<String, String>,
) -> Result<SerializedDocument, FormatError> { ... }
}Expand description
Trait for document formats
Implementors provide bidirectional conversion between string representation and Document AST. Formats can support parsing, serialization, or both.
§Examples
struct MyFormat;
impl Format for MyFormat {
fn name(&self) -> &str {
"my-format"
}
fn supports_parsing(&self) -> bool {
true
}
fn supports_serialization(&self) -> bool {
true
}
fn parse(&self, source: &str) -> Result<Document, FormatError> {
// Parse source to Document
todo!()
}
fn serialize(&self, doc: &Document) -> Result<String, FormatError> {
// Serialize Document to string
todo!()
}
}Required Methods§
Provided Methods§
Sourcefn description(&self) -> &str
fn description(&self) -> &str
Optional description of this format
Sourcefn file_extensions(&self) -> &[&str]
fn file_extensions(&self) -> &[&str]
File extensions associated with this format (e.g., [“lex”], [“md”, “markdown”])
Returns a slice of file extensions without the leading dot. Used for automatic format detection from filenames.
Sourcefn supports_parsing(&self) -> bool
fn supports_parsing(&self) -> bool
Whether this format supports parsing (source → Document)
Sourcefn supports_serialization(&self) -> bool
fn supports_serialization(&self) -> bool
Whether this format supports serialization (Document → source)
Sourcefn parse(&self, _source: &str) -> Result<Document, FormatError>
fn parse(&self, _source: &str) -> Result<Document, FormatError>
Parse source text into a Document
Default implementation returns NotSupported error. Formats that support parsing should override this method.
Sourcefn serialize(&self, _doc: &Document) -> Result<String, FormatError>
fn serialize(&self, _doc: &Document) -> Result<String, FormatError>
Serialize a Document into source text
Default implementation returns NotSupported error. Formats that support serialization should override this method.
Sourcefn serialize_with_options(
&self,
doc: &Document,
options: &HashMap<String, String>,
) -> Result<SerializedDocument, FormatError>
fn serialize_with_options( &self, doc: &Document, options: &HashMap<String, String>, ) -> Result<SerializedDocument, FormatError>
Serialize a Document, optionally using extra parameters.
Formats that only emit textual output can rely on the default implementation,
which delegates to Format::serialize. Binary formats should override this
method to return SerializedDocument::Binary.