use crate::units::Unit;
use crate::ConceptId;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Resource {
id: ConceptId,
name: String,
unit: Unit,
namespace: String,
attributes: HashMap<String, Value>,
}
impl Resource {
#[deprecated(note = "Use new_with_namespace instead")]
pub fn new(name: impl Into<String>, unit: Unit) -> Self {
let name = name.into();
let namespace = "default".to_string();
Self {
id: ConceptId::from_concept(&namespace, &name),
name,
unit,
namespace,
attributes: HashMap::new(),
}
}
pub fn new_with_namespace(
name: impl Into<String>,
unit: Unit,
namespace: impl Into<String>,
) -> Self {
let namespace = namespace.into();
let name = name.into();
let id = ConceptId::from_concept(&namespace, &name);
Self {
id,
name,
unit,
namespace,
attributes: HashMap::new(),
}
}
pub fn from_legacy_uuid(
uuid: Uuid,
name: impl Into<String>,
unit: Unit,
namespace: impl Into<String>,
) -> Self {
Self {
id: ConceptId::from_legacy_uuid(uuid),
name: name.into(),
unit,
namespace: namespace.into(),
attributes: HashMap::new(),
}
}
pub fn id(&self) -> &ConceptId {
&self.id
}
pub fn name(&self) -> &str {
&self.name
}
pub fn unit(&self) -> &Unit {
&self.unit
}
pub fn unit_symbol(&self) -> &str {
self.unit.symbol()
}
pub fn namespace(&self) -> &str {
&self.namespace
}
pub fn set_attribute(&mut self, key: impl Into<String>, value: Value) {
self.attributes.insert(key.into(), value);
}
pub fn get_attribute(&self, key: &str) -> Option<&Value> {
self.attributes.get(key)
}
pub fn attributes(&self) -> &HashMap<String, Value> {
&self.attributes
}
}