daemonic_error 1.0.0

Errors that compose, predict, and leave receipts - Compose: algebraic combination (in active development) - Predict: Glass/Severity - Receipts: audit trail, position, checksum - Reflection: Runtime Reflection through TopologySegment (in active development)
use alloc::borrow::ToOwned;
use alloc::string::String;
use crate::{Annotation, DaemonicEmission, DaemonicError, Glass, GlassDrift, ObservationTier, Severity, Temporal, TopologySegment};
// use daemonic_derive::daemonic;
use daemonic_error::daemonic;
use super::super::super::glass_flags::GlassFlags;
#[derive(Clone, Debug)]
pub struct Drift<GLASS> {
	position: &'static TopologySegment,
	severity: Severity,
	tier: ObservationTier,
	annotation: Annotation,
	temporal: Temporal,
	payload: Option<GLASS>,
	flags: GlassFlags,
}
impl<GLASS> Drift<GLASS> {
	pub fn new(payload: Option<GLASS>, position: &'static TopologySegment) -> Self {
		Drift {
			position,
			severity: Severity::Drift,
			tier: ObservationTier::Composed,
			annotation: Annotation::None,
			temporal: Temporal::Current,
			payload,
			flags: GlassFlags::drift_defaults()
		}
	}
	pub fn extract_flags(&self) -> GlassFlags { self.flags.clone() }
	// pub fn mutate_tier(&self, tier: ObservationTier) -> Self {
	// 	Self {
	// 		position: &self.position,
	// 		severity: self.severity,
	// 		tier,
	// 		annotation: self.annotation,
	// 		temporal: self.temporal,
	// 		payload: self.payload,
	// 		flags: self.extract_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
	}
	/// This method seems redundant, but theres a reason for its existence, States declare their own severity
	/// as well as the label. One reason is due to a ton of shit already using severity, but also
	/// explicitness.
	/// The old Observation State struct had public fields and was generic by design, States should
	/// be equated with Severity.
	/// IE Severity == State == Severity
	/// So as an additional safety precaution, the new structures are no longer field public, constructors
	/// extract data and pass to the consumer. Call site declarations should reflect this.
	/// Use extractor functions, dont exfil field data.
	pub fn extract_severity(&self) -> Severity {
		self.severity
	}
	// pub fn with_note(&self, note: impl Into<String>) -> Self {
	// 	Drift {
	// 		position: &Self::extract_position(&self),
	// 		severity: Severity::Drift,
	// 		tier: Self::extract_tier(&self),
	// 		annotation: Annotation::Note(note.into()),
	// 		temporal: Temporal::Current,
	// 		payload: self.payload,
	// 		flags: self.extract_flags(),
	// 	}
	// }
	// pub fn with_suggestion(&self, suggestion: impl Into<String>) -> Self {
	// 	Drift {
	// 		position: &Self::extract_position(&self),
	// 		severity: Severity::Drift,
	// 		tier: Self::extract_tier(&self),
	// 		annotation: Annotation::Suggestion(suggestion.into()),
	// 		temporal: Temporal::Current,
	// 		payload: self.payload,
	// 		flags: self.extract_flags(),
	// 	}
	// }
	// pub fn with_help(&self, help: impl Into<String>) -> Self {
	// 	Drift {
	// 		position: &Self::extract_position(&self),
	// 		severity: Severity::Drift,
	// 		tier: Self::extract_tier(&self),
	// 		annotation: Annotation::Help(help.into()),
	// 		temporal: Temporal::Current,
	// 		payload: self.payload,
	// 		flags: self.extract_flags(),
	// 	}
	// }
	pub fn mutate_annotation(&mut self, a: Annotation) {
		self.annotation = a;
	}
	pub fn query_payload(&self) -> Option<&GLASS> {
		if self.payload.is_some() {
			Some(self.payload.as_ref().unwrap())
		} else {
			None
		}
	}
	pub fn extract_referent_payload(&self) -> Option<&GLASS> {
		self.payload.as_ref()
	}
	pub fn extract_owned_payload(self) -> Option<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
	}
}
pub const DRIFT_TOPOLOGY: daemonic::TopologySegment =
	daemonic::TopologySegment::new("Daemonic::Glass::States::Drift<GLASS>");
impl<GLASS> daemonic::Anchor for Drift<GLASS> {
	fn anchor_domains(&self) -> daemonic::AnchorDomainSet {
		daemonic::AnchorDomainSet::SPATIAL
			.union(daemonic::AnchorDomainSet::STRUCTURAL)
			.union(daemonic::AnchorDomainSet::SYMBOLIC)
	}
}
impl<GLASS> daemonic::SymbolicAnchor for Drift<GLASS> {
	type Anchor = daemonic::TopologySegment;
	fn meaningful_within(&self) -> Self::Anchor {
		DRIFT_TOPOLOGY.clone()
	}
}
impl<GLASS> daemonic::SemanticAnchor for Drift<GLASS> {}
impl<GLASS> daemonic::SpatialAnchor for Drift<GLASS> {
	fn anchor_position(&self) -> &daemonic::TopologySegment {
		&DRIFT_TOPOLOGY
	}
	fn neighbors(&self) -> daemonic::TopologySegment {
		DRIFT_TOPOLOGY.clone()
	}
}
impl<GLASS> daemonic::StructuralAnchor for Drift<GLASS> {
	fn contract_description(&self) -> &str {
		"Drift: auto-derived Daemonic anchor via #[daemonic] proc macro"
	}
}
impl<GLASS> ::daemonic_error::TopologyAnchor for Drift<GLASS> {
	fn parent(&self) -> Option<daemonic::TopologySegment> {
		let label = DRIFT_TOPOLOGY.label;
		label.rsplit_once("::").map(|(parent, _)| {
			<::daemonic_error::TopologySegment as ::daemonic_error::TopologyAnchor>::new_anchor(
				parent,
			)
		})
	}
	fn children(&self) -> &[daemonic::TopologySegment] {
		&[]
	}
	fn segments(&self) -> &daemonic::TopologySegment {
		&DRIFT_TOPOLOGY
	}
	fn depth(&self) -> usize {
		DRIFT_TOPOLOGY.depth as usize
	}
}
impl<GLASS: Glass<GLASS> + Glass<Drift<GLASS>>> GlassDrift<GLASS> for Drift<GLASS>
	where
		GLASS: for<'error> DaemonicError<'error>,
{
	fn from_severity(&self) -> Severity {
		todo!()
	}
	fn to_severity(&self) -> Severity {
		todo!()
	}
	
	fn set_retry(&self) -> bool {
		false
	}
}
impl<GLASS> crate::DaemonicError<'_> for Drift<GLASS>
	where
		GLASS: for<'error> DaemonicError<'error>,
{
	fn emit(self) -> DaemonicEmission {
		todo!()
	}
	
	fn position(&self) -> &TopologySegment {
		self.extract_position()
	}
}
impl<GLASS> Glass<GLASS> for Drift<GLASS> {
	type Anchor = TopologySegment;
	
	fn position(&self) -> &TopologySegment {
		self.extract_position()
	}
	
	fn severity(&self) -> Severity {
		self.extract_severity()
	}
	
	fn payload(&self) -> Option<&GLASS> {
		self.extract_referent_payload()
	}
	
	fn into_payload(self) -> Option<GLASS>
		where
			Self: Sized,
			GLASS: Sized,
	{
		self.extract_owned_payload()
	}
}