diff --git a/cli/src/main.rs b/cli/src/main.rs index 97db29d..4191480 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,5 +1,5 @@ use clap::Parser; -use schema2000::{render_schema, SchemaHypothesis}; +use schema2000::{generate_hypothesis_from_iterator, render_schema, SchemaHypothesis}; fn main() -> Result<(), Box> { let _args = Args::parse(); @@ -10,19 +10,10 @@ fn main() -> Result<(), Box> { let deserializer = serde_json::Deserializer::from_reader(stdin); let iterator = deserializer.into_iter::(); - let mut current_hypothesis: Option = None; + let hypothesis: SchemaHypothesis = + generate_hypothesis_from_iterator(iterator.map(|value| value.unwrap())); - for json_document in iterator { - let new_hypo = schema2000::generate_hypothesis(&json_document?); - if current_hypothesis.is_none() { - current_hypothesis = Some(new_hypo); - } else { - current_hypothesis = - current_hypothesis.map(|cur| schema2000::merge_hypothesis(cur, new_hypo)); - } - } - - let result = render_schema(¤t_hypothesis.unwrap()); + let result = render_schema(&hypothesis); println!("{}", result); diff --git a/core/src/generate.rs b/core/src/generate.rs index 89dc8f0..3935fb6 100644 --- a/core/src/generate.rs +++ b/core/src/generate.rs @@ -1,5 +1,6 @@ use std::collections::{BTreeMap, BTreeSet}; +use crate::merge_hypothesis; use serde_json::{Map, Value}; use crate::model::{ @@ -88,6 +89,24 @@ pub fn generate_hypothesis(dom: &Value) -> SchemaHypothesis { } } +pub fn generate_hypothesis_from_iterator(values: I) -> SchemaHypothesis +where + I: Iterator, +{ + let mut current_hypothesis: Option = None; + + for json_document in values { + let new_hypo = generate_hypothesis(&json_document); + if current_hypothesis.is_none() { + current_hypothesis = Some(new_hypo); + } else { + current_hypothesis = current_hypothesis.map(|cur| merge_hypothesis(cur, new_hypo)); + } + } + + current_hypothesis.unwrap() +} + #[cfg(test)] mod test { use maplit::{btreemap, btreeset}; diff --git a/core/src/lib.rs b/core/src/lib.rs index a58515d..94df7df 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -1,6 +1,7 @@ #![allow(clippy::module_name_repetitions)] pub use generate::generate_hypothesis; +pub use generate::generate_hypothesis_from_iterator; pub use merge::merge_hypothesis; pub use model::SchemaHypothesis; pub use renderer::render_schema;