daemonic_error 0.1.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 daemonic_system_call::DaemonicSystemCall;
use crate::{AXIOMS, AXIOM_OFFSET};
use crate::{const_daemonic_hash, Observation};
use crate::DaemonicError;
use crate::GlassStable;
use crate::daemonic::{Anchor, AnchorDomainSet, SemanticAnchor, SpatialAnchor, StructuralAnchor, SymbolicAnchor};
use crate::daemonic::daemonic_hasher::{DaemonicHashable, DaemonicHasher};
use crate::daemonic::frame::{Frame, FrameType, ReferenceFrame};
use crate::daemonic::glass::{daemonic_system_call, Glass, Severity};
// use crate::daemonic::observation::debug::DaemonicDebug;
// use crate::daemonic::observation::display::{DaemonicDisplay, DaemonicFormatter};
pub(crate) const TOPOLOGY_ANCHOR: u64 = {
	let raw = const_daemonic_hash(
		"trait TopologyAnchor<TOP = TopologySegment>: SpatialAnchor + StructuralAnchor".as_bytes(),
		0x474C415353_u64, // "GLASS" seed
	);
	// Ensure it's odd (primes are odd except 2)
	raw | 1
};
pub trait TopologyAnchor<TOP = TopologySegment>: SpatialAnchor + StructuralAnchor
	where
		TOP: TopologyAnchor,
{
	type Segment: TopologyAnchor = TOP;
	fn new_anchor(s: &'static str) -> TopologySegment {
		TopologySegment {
			label:  s, // note: this requires s to be 'static
			hash: const_daemonic_hash(s.as_bytes(), AXIOM_OFFSET),
			crypto_id: const_daemonic_hash(s.as_bytes(), TOPOLOGY_ANCHOR),
			depth: s.matches("::").count() as u16, // count :: separators
		}
	}
	
	
	/// Parent anchor (required — something had to spawn this)
	/// None only for root topology anchors
	fn parent(&self) -> Option<Self::Segment>;
	
	/// Child anchors (optional — leaves have none)
	fn children(&self) -> &[Self::Segment];
	
	/// Am I a leaf (no children) or a branch (has children)?
	fn is_leaf(&self) -> bool {
		self.children().is_empty()
	}
	
	/// Segments: the label of this node
	fn segments(&self) -> &Self::Segment;
	
	/// Depth from root (Daemonic is relative root)
	fn depth(&self) -> usize;
}
//todo: TopologyWalker that parses Grammar in a given label or hash segment
#[derive(Clone, Eq, PartialEq, Debug, Copy)]
pub struct TopologySegment {
	/// Human-readable path
	pub(crate) label: &'static str,
	/// DaemonicHashable of label (for fast comparison)
	pub(crate) hash: u64,
	/// Salted identifier from spawning trait
	/// IE, Trait Salt, represented as a String input
	pub(crate) crypto_id: u64,
	/// Depth in the topology
	pub(crate) depth: u16,
}
impl DaemonicHashable for TopologySegment {
	fn declare_hashable<GLASS: DaemonicHasher<GLASS> + Glass<GLASS>>(&self, state: &mut GLASS) {
		DaemonicHashable::declare_hashable(&self, state);
		DaemonicHashable::declare_hashable(&self.label, state);
		DaemonicHashable::declare_hashable(&self.hash, state);
		DaemonicHashable::declare_hashable(&self.crypto_id, state);
		DaemonicHashable::declare_hashable(&self.depth, state);
	}
}
impl TopologySegment {
	pub fn new(s: &'static str) -> TopologySegment {
		TopologySegment {
			label: s, // note: this requires s to be 'static
			hash: const_daemonic_hash(s.as_bytes(), AXIOM_OFFSET),
			crypto_id: const_daemonic_hash(s.as_bytes(), TOPOLOGY_ANCHOR),
			depth: s.matches("::").count() as u16, // count :: separators
		}
	}
	pub fn iter() {todo!()}
}

impl Frame for TopologySegment {
	fn frame_type(&self) -> FrameType {
		todo!()
	}
}

impl ReferenceFrame for TopologySegment {
	fn origin(&self) -> &TopologySegment {
		todo!()
	}
	
	fn transform_to<GLASS>(&self, observation: &impl Glass<GLASS>, target_frame: &dyn ReferenceFrame) -> Option<Observation<GLASS>>
		where
			GLASS: Clone,
			Self: Sized,
	{
		todo!()
	}
}

impl SymbolicAnchor for TopologySegment {
	type Anchor = TopologySegment;
	
	fn meaningful_within(&self) -> impl ReferenceFrame {
		TopologySegment::new("Daemonic::Anchor::Spatial::Topology::TopologySegment")
	}
}

impl Anchor for TopologySegment {
	fn anchor_domains(&self) -> AnchorDomainSet {
		todo!()
	}
}

impl SpatialAnchor for TopologySegment {
	fn anchor_position(&self) -> &TopologySegment {
		todo!()
	}
	
	fn neighbors(&self) -> TopologySegment {
		todo!()
	}
}

impl StructuralAnchor for TopologySegment {
	fn contract_description(&self) -> &str {
		todo!()
	}
}

impl TopologyAnchor for TopologySegment {
	fn parent(&self) -> Option<TopologySegment> {
		todo!()
	}
	
	fn children(&self) -> &[TopologySegment] {
		todo!()
	}
	
	fn segments(&self) -> &Self::Segment {
		todo!()
	}
	
	fn depth(&self) -> usize {
		todo!()
	}
}
impl SemanticAnchor for TopologySegment {}

unsafe impl DaemonicSystemCall for TopologySegment {}

impl Glass<TopologySegment> for TopologySegment {
	type Anchor = TopologySegment;
	
	fn position(&self) -> &TopologySegment {
		self
	}
	
	fn severity(&self) -> Severity {
		Severity::Stable
	}
	
	fn tier(&self) -> crate::daemonic::glass::ObservationTier {
		crate::daemonic::glass::ObservationTier::Composed
	}
	
	fn payload(&self) -> Option<&TopologySegment> {
		Some(self)
	}
	
	fn into_payload(self) -> Option<TopologySegment>
		where
			Self: Sized,
	{
		Some(self)
	}
}

impl GlassStable<TopologySegment> for TopologySegment
 {
	fn value(&self) -> Observation<&TopologySegment> {
		let pos = TopologySegment::new("Daemonic::Anchor::Spatial::TopologySegment");
		Observation::stable(&self.payload().unwrap(), &pos)
	}
	
	fn into_value(self) -> Observation<TopologySegment>
		where
			Self: Sized,
	{
		let pos = TopologySegment::new("Daemonic::Anchor::Spatial::TopologySegment");
		Observation::stable(self.into_payload().unwrap(), &pos)
	}
}
// Each trait/struct gets a const segment
pub const DAEMONIC_SEG: TopologySegment = TopologySegment {
	label: "Daemonic",
	hash: const_daemonic_hash("Daemonic".as_bytes(), 0x4441454D4F4E4943),
	crypto_id: const_daemonic_hash("Daemonic".as_bytes(), AXIOM_OFFSET),
	depth: 0,
};

pub const GLASS_SEG: TopologySegment = TopologySegment {
	label: "Daemonic::Glass",
	hash: const_daemonic_hash("Glass".as_bytes(), 0x4441454D4F4E4943),
	crypto_id: const_daemonic_hash("Daemonic::Glass".as_bytes(), AXIOM_OFFSET),
	depth: 1,
};