Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions prophet-model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ impl TryFrom<&BTreeMap<String, Value>> for Entity {
let fields = ressa::extract_vec(entity, "fields", Value::into_object)?
.into_iter()
.map(ressa::extract_object)
.flat_map(|f| Field::try_from(&f))
.flat_map(|obj| Fields::try_from(&obj))
.flat_map(Fields::into_inner)
.collect::<Vec<_>>();

Ok(Entity { name, fields, ty })
Expand Down Expand Up @@ -238,18 +239,35 @@ impl Field {
}
}

impl TryFrom<&BTreeMap<String, Value>> for Field {
#[derive(Debug)]
struct Fields(Vec<Field>);

impl TryFrom<&BTreeMap<String, Value>> for Fields {
type Error = ressa::result::Error;

fn try_from(entity: &BTreeMap<String, Value>) -> Result<Self, Self::Error> {
let name = ressa::extract(entity, "name", Value::into_string)?;
let ty = ressa::extract(entity, "type", Value::into_string)?;
let is_collection = ressa::extract_primitive(entity, "is_collection", Value::into_bool)?;
Ok(Field {
fn try_from(field: &BTreeMap<String, Value>) -> Result<Self, Self::Error> {
let name = ressa::extract(field, "name", Value::into_string)?;
let ty = ressa::extract(field, "type", Value::into_string)?;
let is_collection = ressa::extract_primitive(field, "is_collection", Value::into_bool)?;
let mut fields = ressa::extract_vec(field, "fields", Value::into_object)
.unwrap_or_else(|_| vec![])
.into_iter()
.map(ressa::extract_object)
.flat_map(|obj| Fields::try_from(&obj))
.flat_map(Fields::into_inner)
.collect::<Vec<_>>();
fields.push(Field {
name,
ty,
is_collection,
})
});
Ok(Fields(fields))
}
}

impl Fields {
fn into_inner(self) -> Vec<Field> {
self.0
}
}

Expand Down