Skip to main content

Parser

Trait Parser 

Source
pub trait Parser {
    // Required method
    fn parse(
        &self,
        content: &str,
        file_name: &str,
    ) -> Result<Value, ParseDiagnostic>;

    // Provided methods
    fn extract_schema_uri(
        &self,
        _content: &str,
        value: &Value,
    ) -> Option<String> { ... }
    fn annotate(&self, _content: &str, _schema_url: &str) -> Option<String> { ... }
    fn strip_annotation(&self, content: &str) -> String { ... }
}
Expand description

Parse file content into a serde_json::Value.

Implementations must produce a ParseDiagnostic with an accurate source span when parsing fails.

Required Methods§

Source

fn parse( &self, content: &str, file_name: &str, ) -> Result<Value, ParseDiagnostic>

§Errors

Returns a ParseDiagnostic with an accurate source span when parsing fails.

Provided Methods§

Source

fn extract_schema_uri(&self, _content: &str, value: &Value) -> Option<String>

Extract the $schema URI from file content and/or parsed value.

The default implementation reads value["$schema"], which works for JSON, JSON5, and JSONC. YAML and TOML override this to handle their format-specific conventions (modeline comments, etc.).

Source

fn annotate(&self, _content: &str, _schema_url: &str) -> Option<String>

Insert a schema annotation into the file content.

Returns Some(annotated_content) if the format supports inline schema annotations, or None if it does not (e.g. Markdown).

Source

fn strip_annotation(&self, content: &str) -> String

Remove an existing schema annotation from the file content.

Returns the content with the annotation stripped. If no annotation is found, returns the content unchanged.

Implementors§