use crate::ConceptId;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use uuid;
const DEFAULT_NAMESPACE: &str = "default";
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Flow {
id: ConceptId,
resource_id: ConceptId,
from_id: ConceptId,
to_id: ConceptId,
quantity: Decimal,
namespace: String,
attributes: HashMap<String, Value>,
}
impl Flow {
pub fn new(
resource_id: ConceptId,
from_id: ConceptId,
to_id: ConceptId,
quantity: Decimal,
) -> Self {
Self::new_with_namespace(resource_id, from_id, to_id, quantity, DEFAULT_NAMESPACE)
}
pub fn new_with_namespace(
resource_id: ConceptId,
from_id: ConceptId,
to_id: ConceptId,
quantity: Decimal,
namespace: impl Into<String>,
) -> Self {
let namespace = namespace.into();
let id = ConceptId::from_uuid(uuid::Uuid::new_v4());
Self {
id,
resource_id,
from_id,
to_id,
quantity,
namespace,
attributes: HashMap::new(),
}
}
pub fn id(&self) -> &ConceptId {
&self.id
}
pub fn resource_id(&self) -> &ConceptId {
&self.resource_id
}
pub fn from_id(&self) -> &ConceptId {
&self.from_id
}
pub fn to_id(&self) -> &ConceptId {
&self.to_id
}
pub fn quantity(&self) -> Decimal {
self.quantity
}
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
}
}