review: make value_of internal and remove unused function

This commit is contained in:
2025-10-09 15:11:54 +02:00
parent 02eddc19e3
commit b0d290407d
2 changed files with 4 additions and 25 deletions

View File

@@ -42,31 +42,13 @@ impl O {
} }
self self
} }
pub fn value_of(&self, id: &str) -> Option<&Value> { pub(crate) fn value_of(&self, id: &str) -> Option<&Value> {
let attribute = self.attributes.get(id); let attribute = self.attributes.get(id);
match attribute { match attribute {
None => None, None => None,
Some(attribute) => Some(&attribute.value), Some(attribute) => Some(&attribute.value),
} }
} }
pub fn set(&mut self, id: &str, value: impl Into<Value>) -> Option<Value> {
let attribute = Attribute::new(id, value);
let previous_attribute = self.attributes.insert(attribute.id.clone(), attribute);
match previous_attribute {
None => None,
Some(attribute) => Some(attribute.value),
}
}
pub fn unset(&mut self, id: &str) -> Option<Value> {
let attribute = self.attributes.remove(id);
match attribute {
None => None,
Some(attribute) => Some(attribute.value),
}
}
pub fn has_attribute(&self, id: &str) -> bool {
self.attributes.contains_key(id)
}
pub fn unwrap<T>(&self, id: &str) -> T pub fn unwrap<T>(&self, id: &str) -> T
where where
T: From<Value>, T: From<Value>,

View File

@@ -27,15 +27,12 @@ mod tests {
} }
impl From<Product> for Entity { impl From<Product> for Entity {
fn from(value: Product) -> Self { fn from(value: Product) -> Self {
let mut entity = Entity::new::<Product>() Entity::new::<Product>()
.with_id(&value.id) .with_id(&value.id)
.with_attribute("name", value.name) .with_attribute("name", value.name)
.with_attribute("price", value.price) .with_attribute("price", value.price)
.with_attribute("in_stock", value.in_stock); .with_attribute("in_stock", value.in_stock)
if let Some(model) = value.model { .with_opt_attribute("model", value.model)
entity.set("model", model);
}
entity
} }
} }
#[test] #[test]