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§
Sourcefn check_event(&self, _event: PolicyEvent<'_>) -> Result<()>
fn check_event(&self, _event: PolicyEvent<'_>) -> Result<()>
Inspect a parser event during loading. Return Err(...) to
abort the parse with a diagnostic.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".