pub struct IncrementalSolver<'a> { /* private fields */ }Expand description
Incremental constraint solver for streaming deserialization.
Unlike batch solving where all field names are known upfront, this solver processes fields one at a time as they arrive. It minimizes deferred fields by immediately setting fields that have the same path in all remaining candidate configurations.
§Example
use facet::Facet;
use facet_solver::{Schema, IncrementalSolver, FieldDecision};
#[derive(Facet)]
struct SimpleConfig { port: u16 }
#[derive(Facet)]
struct AdvancedConfig { host: String, port: u16 }
#[derive(Facet)]
#[repr(u8)]
enum Config {
Simple(SimpleConfig),
Advanced(AdvancedConfig),
}
#[derive(Facet)]
struct Server {
name: String,
#[facet(flatten)]
config: Config,
}
let schema = Schema::build(Server::SHAPE).unwrap();
let mut solver = IncrementalSolver::new(&schema);
// "name" exists in all configs at the same path - set directly
assert!(matches!(solver.see_key("name"), FieldDecision::SetDirectly(_)));
// "host" only exists in Advanced - disambiguates!
match solver.see_key("host") {
FieldDecision::Disambiguated { config, current_field } => {
assert_eq!(current_field.serialized_name, "host");
assert!(config.field("port").is_some()); // Advanced has port too
}
_ => panic!("expected disambiguation"),
}Implementations§
Source§impl<'a> IncrementalSolver<'a>
impl<'a> IncrementalSolver<'a>
Sourcepub fn candidates(&self) -> Vec<&'a Configuration>
pub fn candidates(&self) -> Vec<&'a Configuration>
Get the current candidate configurations.
Sourcepub fn see_key(&mut self, key: &'a str) -> FieldDecision<'a>
pub fn see_key(&mut self, key: &'a str) -> FieldDecision<'a>
Process a field key. Returns what to do with the value.
Sourcepub fn deferred_keys(&self) -> &[&'a str]
pub fn deferred_keys(&self) -> &[&'a str]
Get the keys that have been deferred.
Sourcepub fn clear_deferred(&mut self)
pub fn clear_deferred(&mut self)
Clear the deferred keys (call after replaying).
Sourcepub fn finish(
self,
seen_keys: &BTreeSet<&str>,
) -> Result<&'a Configuration, SolverError>
pub fn finish( self, seen_keys: &BTreeSet<&str>, ) -> Result<&'a Configuration, SolverError>
Finish solving. Returns error if still ambiguous or missing required fields.
When multiple candidates remain, this filters out any that are missing
required fields. This handles the case where one variant’s fields are
a subset of another’s (e.g., Http { url } vs Git { url, branch }).
If input only contains url, both match the “has this field” check,
but Git is filtered out because it’s missing required branch.