From 64ad16365f011751fd74f6748a45ff802e9f46b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Fri, 5 Feb 2021 22:54:22 +0100 Subject: [PATCH] less cryptic errors --- src/main.rs | 7 ++++--- yopa/src/lib.rs | 26 +++++++++++++++++++++----- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4097e66..eafcca8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,7 @@ fn main() { } } +#[allow(non_snake_case)] fn _main() -> anyhow::Result<()> { simple_logging::log_to_stderr(LevelFilter::Debug); @@ -75,7 +76,7 @@ fn _main() -> anyhow::Result<()> { id: Default::default(), parent_tpl_id: BookToRecipe, name: "page".to_string(), - optional: true, + optional: false, multiple: false, data_type: DataType::Integer, default: None @@ -101,11 +102,11 @@ fn _main() -> anyhow::Result<()> { model_id: BookToRecipe, related_id: MyBook1, values: vec![ - InsertValue::new(BookToRecipePage, TypedValue::Integer(123)) + //InsertValue::new(BookToRecipePage, TypedValue::Integer(123)) ] } ], - }); + })?; debug!("{:#?}", store); diff --git a/yopa/src/lib.rs b/yopa/src/lib.rs index 8199353..b0c69e5 100644 --- a/yopa/src/lib.rs +++ b/yopa/src/lib.rs @@ -168,6 +168,18 @@ impl InMemoryStorage { }; } + pub fn describe_id(&self, id: ID) -> String { + if let Some(x) = self.obj_models.get(&id) { + x.to_string() + } else if let Some(x) = self.rel_models.get(&id) { + x.to_string() + } else if let Some(x) = self.prop_models.get(&id) { + x.to_string() + } else { + id.to_string() + } + } + // DATA /// Insert object with relations, validating the data model constraints @@ -185,14 +197,14 @@ impl InMemoryStorage { model_id: obj_model_id, }; - let find_values_to_insert = |values: Vec, parent_id: ID| -> Result, StorageError> { + let find_values_to_insert = |values: Vec, parent_model_id: ID| -> Result, StorageError> { let mut values_by_id = values.into_iter().into_group_map_by(|iv| iv.model_id); let mut values_to_insert = vec![]; - for (id, prop) in self.prop_models.iter().filter(|(_id, p)| p.parent_tpl_id == parent_id) { + for (id, prop) in self.prop_models.iter().filter(|(_id, p)| p.parent_tpl_id == parent_model_id) { if let Some(values) = values_by_id.remove(id) { if values.len() > 1 && !prop.multiple { - return Err(StorageError::ConstraintViolation(format!("{} of {} cannot have multiple values", prop, obj_model).into())); + return Err(StorageError::ConstraintViolation(format!("{} of {} cannot have multiple values", prop, self.describe_id(parent_model_id)).into())); } for val_instance in values { values_to_insert.push(data::Value { @@ -213,7 +225,7 @@ impl InMemoryStorage { value: def.clone(), }); } else { - return Err(StorageError::ConstraintViolation(format!("{} is required for {} (and no default value is defined)", prop, obj_model).into())); + return Err(StorageError::ConstraintViolation(format!("{} is required for {} and no default value is defined", prop, self.describe_id(parent_model_id)).into())); } } } @@ -237,7 +249,11 @@ impl InMemoryStorage { for rel_instance in instances { if let Some(related) = self.objects.get(&rel_instance.related_id) { if related.model_id != relation_model.related_tpl_id { - return Err(StorageError::ConstraintViolation(format!("{} of {} requires object of type {}, got {}", relation_model, obj_model, relation_model.related_tpl_id, related.model_id).into())); + return Err(StorageError::ConstraintViolation( + format!("{} of {} requires object of type {}, got {}", + relation_model, obj_model, + self.describe_id(relation_model.related_tpl_id), + self.describe_id(related.model_id)).into())); } }