use crate::ExpandedName;
use std::{borrow::Borrow, collections::HashMap, fmt, ops::Deref};
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ContextId(String);
impl ContextId {
pub fn as_str(&self) -> &str {
&self.0
}
}
impl From<String> for ContextId {
fn from(value: String) -> Self {
Self(value)
}
}
impl From<&str> for ContextId {
fn from(value: &str) -> Self {
Self(value.to_owned())
}
}
impl Deref for ContextId {
type Target = str;
fn deref(&self) -> &Self::Target {
self.as_str()
}
}
impl AsRef<str> for ContextId {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl Borrow<str> for ContextId {
fn borrow(&self) -> &str {
self.as_str()
}
}
impl fmt::Display for ContextId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct EntityIdentifier {
pub scheme: String,
pub value: String,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Period {
Instant { date: String },
Duration { start: String, end: String },
Forever,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Context {
pub id: ContextId,
pub entity: EntityIdentifier,
pub period: Period,
pub dimensions: HashMap<ExpandedName, ExpandedName>,
pub segment_elements: Vec<ExpandedName>,
pub scenario_elements: Vec<ExpandedName>,
pub segment_has_instance_descendant: bool,
pub scenario_has_instance_descendant: bool,
}
impl Context {
pub fn new(id: ContextId, entity: EntityIdentifier, period: Period) -> Self {
Self {
id,
entity,
period,
dimensions: HashMap::new(),
segment_elements: Vec::new(),
scenario_elements: Vec::new(),
segment_has_instance_descendant: false,
scenario_has_instance_descendant: false,
}
}
pub fn add_dimension(&mut self, dimension: ExpandedName, member: ExpandedName) {
self.dimensions.insert(dimension, member);
}
}