diff --git a/cli/src/main.rs b/cli/src/main.rs index ad5d555..0a5ef50 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}; use std::error::Error; use std::fs::File; use std::io::{self, Read}; @@ -12,19 +12,10 @@ fn main() -> Result<(), Box> { let deserializer = serde_json::Deserializer::from_reader(reader); 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 9dde0a9..5c3c12a 100644 --- a/core/src/generate.rs +++ b/core/src/generate.rs @@ -2,6 +2,8 @@ use chrono::{DateTime, NaiveDate}; use serde_json::{Map, Number, Value}; use std::collections::{BTreeMap, BTreeSet}; +use crate::merge_hypothesis; + use crate::model::{ AnyNode, ArrayNode, DateNode, DateTimeNode, IntegerNode, NodeType, NumberNode, ObjectNode, ObjectProperty, SchemaHypothesis, StringNode, @@ -103,6 +105,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;