Skip to main content

Parser

Trait Parser 

Source
pub trait Parser {
    type Error: Error + Send + Sync + 'static;

    // Required methods
    fn parse(&mut self, input: &str) -> Result<IRModule, Self::Error>;
    fn name(&self) -> &'static str;
    fn extensions(&self) -> &[&'static str];

    // Provided methods
    fn validate(&self, _input: &str) -> Result<(), Self::Error> { ... }
    fn metadata(&self) -> ParserMetadata { ... }
}
Expand description

Core trait for all parsers

Implement this trait to add support for new input formats. The parser is responsible for converting input text into an IR module that can be processed by code generators.

§Type Parameters

  • Error - The error type produced by this parser. Must implement std::error::Error + Send + Sync + 'static for composability.

§Examples

use unistructgen_core::{Parser, IRModule};

struct MyParser {
    options: MyOptions,
}

impl Parser for MyParser {
    type Error = MyParserError;

    fn parse(&mut self, input: &str) -> Result<IRModule, Self::Error> {
        // Parse input and return IR
        todo!()
    }

    fn name(&self) -> &'static str {
        "MyFormat"
    }

    fn extensions(&self) -> &[&'static str] {
        &["my", "myformat"]
    }
}

Required Associated Types§

Source

type Error: Error + Send + Sync + 'static

The error type this parser produces

Required Methods§

Source

fn parse(&mut self, input: &str) -> Result<IRModule, Self::Error>

Parse input text and return an IR module

§Arguments
  • input - The input text to parse
§Returns

Returns Ok(IRModule) on success, containing the parsed types.

§Errors

Returns Self::Error if parsing fails. The error should contain detailed information about what went wrong and where.

Source

fn name(&self) -> &'static str

Get the parser’s human-readable name

Used for diagnostics and error messages.

§Examples
assert_eq!(json_parser.name(), "JSON");
Source

fn extensions(&self) -> &[&'static str]

Get the file extensions this parser supports

Used for automatic format detection based on file extension.

§Examples
assert_eq!(json_parser.extensions(), &["json"]);
assert_eq!(markdown_parser.extensions(), &["md", "markdown"]);

Provided Methods§

Source

fn validate(&self, _input: &str) -> Result<(), Self::Error>

Validate input without full parsing (optional)

Provides a quick way to check if input is valid without performing full parsing. Default implementation always returns Ok(()).

§Arguments
  • input - The input text to validate
§Returns

Returns Ok(()) if input appears valid, Err otherwise.

Source

fn metadata(&self) -> ParserMetadata

Get metadata about the parser (optional)

Returns additional information about the parser’s capabilities, version, etc. Default implementation returns empty metadata.

Implementors§