less cryptic errors

master
Ondřej Hruška 4 years ago
parent 8d1b8878d0
commit 64ad16365f
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 7
      src/main.rs
  2. 26
      yopa/src/lib.rs

@ -15,6 +15,7 @@ fn main() {
} }
} }
#[allow(non_snake_case)]
fn _main() -> anyhow::Result<()> { fn _main() -> anyhow::Result<()> {
simple_logging::log_to_stderr(LevelFilter::Debug); simple_logging::log_to_stderr(LevelFilter::Debug);
@ -75,7 +76,7 @@ fn _main() -> anyhow::Result<()> {
id: Default::default(), id: Default::default(),
parent_tpl_id: BookToRecipe, parent_tpl_id: BookToRecipe,
name: "page".to_string(), name: "page".to_string(),
optional: true, optional: false,
multiple: false, multiple: false,
data_type: DataType::Integer, data_type: DataType::Integer,
default: None default: None
@ -101,11 +102,11 @@ fn _main() -> anyhow::Result<()> {
model_id: BookToRecipe, model_id: BookToRecipe,
related_id: MyBook1, related_id: MyBook1,
values: vec![ values: vec![
InsertValue::new(BookToRecipePage, TypedValue::Integer(123)) //InsertValue::new(BookToRecipePage, TypedValue::Integer(123))
] ]
} }
], ],
}); })?;
debug!("{:#?}", store); debug!("{:#?}", store);

@ -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 // DATA
/// Insert object with relations, validating the data model constraints /// Insert object with relations, validating the data model constraints
@ -185,14 +197,14 @@ impl InMemoryStorage {
model_id: obj_model_id, model_id: obj_model_id,
}; };
let find_values_to_insert = |values: Vec<InsertValue>, parent_id: ID| -> Result<Vec<data::Value>, StorageError> { let find_values_to_insert = |values: Vec<InsertValue>, parent_model_id: ID| -> Result<Vec<data::Value>, StorageError> {
let mut values_by_id = values.into_iter().into_group_map_by(|iv| iv.model_id); let mut values_by_id = values.into_iter().into_group_map_by(|iv| iv.model_id);
let mut values_to_insert = vec![]; 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 let Some(values) = values_by_id.remove(id) {
if values.len() > 1 && !prop.multiple { 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 { for val_instance in values {
values_to_insert.push(data::Value { values_to_insert.push(data::Value {
@ -213,7 +225,7 @@ impl InMemoryStorage {
value: def.clone(), value: def.clone(),
}); });
} else { } 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 { for rel_instance in instances {
if let Some(related) = self.objects.get(&rel_instance.related_id) { if let Some(related) = self.objects.get(&rel_instance.related_id) {
if related.model_id != relation_model.related_tpl_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()));
} }
} }

Loading…
Cancel
Save