Skip to main content

Policy

Trait Policy 

Source
pub trait Policy:
    Send
    + Sync
    + Debug {
    // Provided methods
    fn check_event(&self, _event: PolicyEvent<'_>) -> Result<()> { ... }
    fn check_value(&self, _value: &Value) -> Result<()> { ... }
}
Expand description

Pluggable “Safe YAML” policy.

Implementors override either or both check methods. The default implementations accept everything, so a policy that only cares about the post-parse value can leave Policy::check_event alone.

§Examples

use noyalib::policy::{Policy, PolicyEvent, PolicyEventKind};
use noyalib::{from_str_with_config, ParserConfig, Value, Result, Error};

#[derive(Debug, Default)]
struct DenyTabs;
impl Policy for DenyTabs {
    fn check_event(&self, ev: PolicyEvent<'_>) -> Result<()> {
        if ev.kind == PolicyEventKind::Scalar
            && ev.scalar.is_some_and(|s| s.contains('\t'))
        {
            return Err(Error::Deserialize("tab in scalar".into()));
        }
        Ok(())
    }
}

let cfg = ParserConfig::new().with_policy(DenyTabs);
let res: Result<Value> = from_str_with_config("k: \"a\\tb\"\n", &cfg);
assert!(res.is_err());

Provided Methods§

Source

fn check_event(&self, _event: PolicyEvent<'_>) -> Result<()>

Inspect a parser event during loading. Return Err(...) to abort the parse with a diagnostic.

Source

fn check_value(&self, _value: &Value) -> Result<()>

Inspect the fully-built Value tree. Runs after the AST is assembled and before deserialisation continues. Return Err(...) to reject the document.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§