use alloc::string::String;
use daemonic_error::daemonic::{Anchor, AnchorDomainSet, SemanticAnchor, SpatialAnchor, StructuralAnchor, SymbolicAnchor};
use daemonic_error::TopologyAnchor;
use crate::daemonic::glass::Glass;
use crate::{Annotation, GlassStable, Observation, ObservationTier, Severity, Temporal, TopologySegment};
use super::super::super::glass_flags::GlassFlags;
#[derive(Clone, Debug)]
pub struct Stable<GLASS> {
position: &'static TopologySegment,
severity: Severity,
tier: ObservationTier,
annotation: Annotation,
temporal: Temporal,
payload: GLASS,
flags: GlassFlags,
}
impl<GLASS> Stable<GLASS> {
pub fn new(p: GLASS, segment: &'static TopologySegment) -> Self {
Stable {
position: segment,
severity: Severity::Stable,
tier: ObservationTier::Composed,
annotation: Annotation::None,
temporal: Temporal::Current,
payload: p,
flags: GlassFlags::stable_defaults(),
}
}
pub fn extract_flags(&self) -> GlassFlags { self.flags }
pub fn extract_position(&self) -> &'static TopologySegment {
self.position
}
pub fn extract_position_label(&self) -> &str {
self.position.label
}
pub fn extract_tier(&self) -> ObservationTier {
self.tier
}
pub fn extract_annotation(&self) -> Annotation {
self.annotation.clone()
}
pub fn extract_temporal(&self) -> Temporal {
self.temporal
}
pub fn unwrap_payload(self) -> GLASS {
self.payload
}
pub fn extract_severity(&self) -> Severity {
self.severity
}
pub fn extract_owned_payload(self) -> Option<GLASS> {
core::prelude::v1::Some(self.payload)
}
pub fn mutate_annotation(&mut self, a: Annotation) {
self.annotation = a;
}
pub fn extract_referent_payload(&self) -> &GLASS {
&self.payload
}
pub fn with_note(mut self, note: impl Into<String>) -> Self {
self.annotation = Annotation::Note(note.into());
self
}
pub fn with_suggestion(mut self, suggestion: impl Into<String>) -> Self {
self.annotation = Annotation::Suggestion(suggestion.into());
self
}
pub fn with_help(mut self, help: impl Into<String>) -> Self {
self.annotation = Annotation::Suggestion(help.into());
self
}
pub fn mutate_tier(mut self, tier: impl Into<ObservationTier>) -> Self {
self.tier = tier.into();
self
}
}
impl<GLASS> GlassStable<GLASS> for Stable<GLASS> {
fn value(&self) -> Stable<&GLASS> {
let x = &self.payload;
Stable {
position: &self.extract_position(),
severity: Severity::Stable,
tier: self.extract_tier(),
annotation: self.extract_annotation(),
temporal: self.extract_temporal(),
payload: x,
flags: self.extract_flags(),
}
}
fn into_value(self) -> Stable<GLASS>
where
Self: Sized,
{
Self::new(self.payload, self.position)
}
}
impl<GLASS> Glass<GLASS> for Stable<GLASS> {
type Anchor = TopologySegment;
fn position(&self) -> &TopologySegment {
self.extract_position()
}
fn severity(&self) -> Severity {
self.extract_severity()
}
fn payload(&self) -> Option<&GLASS> {
Some(self.extract_referent_payload())
}
fn into_payload(self) -> Option<GLASS>
where
Self: Sized,
GLASS: Sized,
{
self.extract_owned_payload()
}
}
const GLASS_STABLE_TOPOLOGY_SEGMENT: TopologySegment = TopologySegment::new("Daemonic::Glass::State::Stable<GLASS>");
impl<GLASS> Anchor for Stable<GLASS> {
fn anchor_domains(&self) -> AnchorDomainSet {
AnchorDomainSet::SPATIAL
.union(AnchorDomainSet::STRUCTURAL)
.union(AnchorDomainSet::SYMBOLIC)
}
}
impl<GLASS> SymbolicAnchor for Stable<GLASS> {
type Anchor = TopologySegment;
fn meaningful_within(&self) -> Self::Anchor {
GLASS_STABLE_TOPOLOGY_SEGMENT.clone()
}
}
impl<GLASS> SemanticAnchor for Stable<GLASS> {}
impl<GLASS> SpatialAnchor for Stable<GLASS> {
fn anchor_position(&self) -> &TopologySegment {
&GLASS_STABLE_TOPOLOGY_SEGMENT
}
fn neighbors(&self) -> TopologySegment {
GLASS_STABLE_TOPOLOGY_SEGMENT.clone()
}
}
impl<GLASS> StructuralAnchor for Stable<GLASS> {
fn contract_description(&self) -> &str {
"Stable: auto-derived Daemonic anchor via #[daemonic] proc macro"
}
}
impl<GLASS> TopologyAnchor for Stable<GLASS> {
fn parent(&self) -> Option<TopologySegment> {
let label = GLASS_STABLE_TOPOLOGY_SEGMENT.label;
label.rsplit_once("::").map(|(parent, _)| {
<TopologySegment as TopologyAnchor>::new_anchor(
parent,
)
})
}
fn children(&self) -> &[TopologySegment] {
&[]
}
fn segments(&self) -> &TopologySegment {
&GLASS_STABLE_TOPOLOGY_SEGMENT
}
fn depth(&self) -> usize {
GLASS_STABLE_TOPOLOGY_SEGMENT.depth as usize
}
}