use alloc::string::{String, ToString};
use core::fmt::Pointer;
use core::ops::{ControlFlow, FromResidual, Try};
use crate::daemonic::{
Daemonic, SymbolicAnchor, TopologySegment};
use crate::daemonic::{Anchor, AnchorDomainSet, TemporalAnchor};
use crate::{const_daemonic_hash, DaemonicError, AXIOM_OFFSET};
pub mod error;
pub mod states;
pub mod primitive_implementations;
pub mod daemonic_path_buffer;
pub use error::*;
use crate::daemonic::glass::daemonic_system_call::DaemonicSystemCall;
use crate::daemonic::TopologyAnchor;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Severity {
Stable,
Cracked,
Fracture,
Drift,
Warp,
Shattered,
Impossible,
Paradox,
Opaque,
Echo,
Unknown,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ObservationTier {
Absolute,
Primitive,
Composed,
GlyphicExecutable,
}
#[derive(Debug, Clone)]
pub enum Annotation {
None,
Note(String),
Help(String),
Suggestion(String),
Sensitive(String),
}
#[derive(Debug, Clone)]
pub enum Temporal {
Current,
Echo { original_tick: u64 },
Drift { from_severity: Severity, to_severity: Severity },
}
impl Anchor for Temporal {
fn anchor_domains(&self) -> AnchorDomainSet {
AnchorDomainSet::TEMPORAL
}
}
impl TemporalAnchor for Temporal {
fn anchor_tick(&self) -> u64 {
1
}
}
#[derive(Debug, Clone)]
pub enum Accessibility {
Clear,
Opaque { reason: String },
}
#[derive(Debug, Clone, Copy)]
pub enum Consistency {
Singular,
Paradox,
}
#[derive(Debug, Clone, Copy)]
pub enum Assessment {
Assessed,
Unknown,
}
#[derive(Debug, Clone, Copy)]
pub enum FidelityAction {
Continue,
Attend,
Warn,
Halt,
Abort,
Drop,
}
pub trait Glass<GLASS: ?Sized>:
{
type Anchor: TopologyAnchor;
fn position(&self) -> &TopologySegment;
fn severity(&self) -> Severity;
fn annotation(&self) -> Annotation {
Annotation::None
}
fn temporal(&self) -> Temporal {
Temporal::Current
}
fn accessibility(&self) -> Accessibility {
Accessibility::Clear
}
fn consistency(&self) -> Consistency {
Consistency::Singular
}
fn assessment(&self) -> Assessment {
Assessment::Unknown
}
fn tier(&self) -> ObservationTier {
ObservationTier::Composed
}
fn payload(&self) -> Option<&GLASS>;
fn into_payload(self) -> Option<GLASS>
where
Self: Sized,
GLASS: Sized;
fn fidelity_action(&self) -> FidelityAction {
severity_to_fidelity(self.severity())
}
fn has_payload(&self) -> bool {
self.payload().is_some()
}
fn is_clear(&self) -> bool {
matches!(self.severity(), Severity::Stable)
&& matches!(self.assessment(), Assessment::Assessed)
}
fn as_report(&self) -> GlassReport {
GlassReport {
position: self.position().clone(),
severity: self.severity(),
tier: self.tier(),
annotation: self.annotation(),
temporal: self.temporal(),
accessibility: self.accessibility(),
consistency: self.consistency(),
assessment: self.assessment(),
fidelity_action: self.fidelity_action(),
}
}
}
pub fn severity_to_fidelity(severity: Severity) -> FidelityAction {
match severity {
Severity::Stable => FidelityAction::Continue,
Severity::Cracked => FidelityAction::Warn,
Severity::Fracture => FidelityAction::Halt,
Severity::Warp => FidelityAction::Halt,
Severity::Shattered => FidelityAction::Halt,
Severity::Impossible => FidelityAction::Abort,
Severity::Unknown => FidelityAction::Attend,
Severity::Drift => FidelityAction::Attend,
Severity::Paradox => FidelityAction::Warn,
Severity::Opaque => FidelityAction::Halt,
Severity::Echo => FidelityAction::Attend,
}
}
pub struct GlassReport {
pub position: TopologySegment,
pub severity: Severity,
pub tier: ObservationTier,
pub annotation: Annotation,
pub temporal: Temporal,
pub accessibility: Accessibility,
pub consistency: Consistency,
pub assessment: Assessment,
pub fidelity_action: FidelityAction,
}
impl GlassReport {
pub fn is_clear(&self) -> bool {
matches!(self.severity, Severity::Stable)
&& matches!(self.assessment, Assessment::Assessed)
}
pub fn needs_attention(&self) -> bool {
!matches!(self.fidelity_action, FidelityAction::Continue)
}
pub fn category(&self) -> &str {
todo!(
"GlassReport::category() is not yet implemented"
)
}
}
pub use states::*;
pub use {GlassStable, StableAnnotation};
pub use {GlassCracked, CrackedAnnotation};
pub use {GlassFracture, FractureAnnotation};
pub use {GlassShattered, ShatteredAnnotation};
pub use {GlassUnknown, UnknownAnnotation};
pub use {GlassEcho, EchoAnnotation};
pub use {GlassImpossible, ImpossibleAnnotation};
pub use {GlassOpaque, OpaqueAnnotation};
pub use {GlassWarp, WarpAnnotation};
pub use {GlassDrift, DriftAnnotation};
use crate::daemonic::topology::TOPOLOGY_ANCHOR;
pub struct Observation<GLASS> {
pub position: &'static TopologySegment,
pub severity: Severity,
pub tier: ObservationTier,
pub annotation: Annotation,
pub temporal: Temporal,
pub payload: Option<GLASS>,
}
impl<GLASS> Observation<GLASS> {
pub(crate) fn to_errno(&self) -> i32 {
0i32
}
}
impl<GLASS> Try for Observation<GLASS> {
type Output = GLASS;
type Residual = Observation<core::convert::Infallible>;
fn from_output(output: Self::Output) -> Self {
Observation::stable(output, &TopologySegment::new(&"Daemonic::Glass::Try::stable"))
}
fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {
match self.severity {
Severity::Stable | Severity::Cracked => {
match self.payload {
Some(value) => ControlFlow::Continue(value),
None => ControlFlow::Break(Observation {
position: self.position,
severity: self.severity,
tier: self.tier,
annotation: self.annotation,
temporal: self.temporal,
payload: None,
}),
}
}
_ => ControlFlow::Break(Observation {
position: self.position,
severity: self.severity,
tier: self.tier,
annotation: self.annotation,
temporal: self.temporal,
payload: None,
}),
}
}
}
impl<GLASS> FromResidual<Observation<core::convert::Infallible>> for Observation<GLASS> {
fn from_residual(residual: Observation<core::convert::Infallible>) -> Self {
Observation {
position: residual.position,
severity: residual.severity,
tier: residual.tier,
annotation: residual.annotation,
temporal: residual.temporal,
payload: None,
}
}
}
impl<GLASS> FromResidual<Result<core::convert::Infallible, core::fmt::Error>> for Observation<GLASS> {
fn from_residual(_residual: Result<core::convert::Infallible, core::fmt::Error>) -> Self {
Observation::shattered(
&TopologySegment::new(&"Daemonic::Glass::Try::fmt_error")
).with_note("std::fmt::Error converted to Glass")
}
}
static OBSERVATION_TOPOLOGY_ANCH0R: TopologySegment = TopologySegment {
label: "Daemonic::Glass::Observation<()>",
hash: const_daemonic_hash(
"Daemonic::Glass::Observation<()>".as_bytes(),
AXIOM_OFFSET,
),
crypto_id: const_daemonic_hash("Daemonic::Glass::Observation<()>".as_bytes(), TOPOLOGY_ANCHOR),
depth: 3u16,
};
impl<GLASS> Observation<GLASS> {
pub fn stable(value: GLASS, position: &TopologySegment) -> Self {
Self {
position: &OBSERVATION_TOPOLOGY_ANCH0R,
severity: Severity::Stable,
tier: ObservationTier::Composed,
annotation: Annotation::None,
temporal: Temporal::Current,
payload: Some(value),
}
}
pub fn cracked(value: Option<GLASS>, position: &TopologySegment) -> Self {
Self {
position: &OBSERVATION_TOPOLOGY_ANCH0R,
severity: Severity::Cracked,
tier: ObservationTier::Composed,
annotation: Annotation::None,
temporal: Temporal::Current,
payload: value,
}
}
pub fn warp(value: Option<GLASS>, position: &TopologySegment) -> Self
where
Self: Sized,
{
Self {
position: &OBSERVATION_TOPOLOGY_ANCH0R,
severity: Severity::Warp,
tier: ObservationTier::Composed,
annotation: Annotation::None,
temporal: Temporal::Current,
payload: value,
}
}
pub fn drift(value: Option<GLASS>, position: &TopologySegment) -> Self
where
Self: Sized,
{
Self {
position: &OBSERVATION_TOPOLOGY_ANCH0R,
severity: Severity::Drift,
tier: ObservationTier::Composed,
annotation: Annotation::None,
temporal: Temporal::Current,
payload: value,
}
}
pub fn opaque(position: &TopologySegment) -> Self
where
Self: Sized,
{
Self {
position: &OBSERVATION_TOPOLOGY_ANCH0R,
severity: Severity::Opaque,
tier: ObservationTier::Composed,
annotation: Annotation::None,
temporal: Temporal::Current,
payload: None,
}
}
pub fn shattered(position: &TopologySegment) -> Self
where
Self: Sized,
{
Self {
position: &OBSERVATION_TOPOLOGY_ANCH0R,
severity: Severity::Shattered,
tier: ObservationTier::Composed,
annotation: Annotation::None,
temporal: Temporal::Current,
payload: None,
}
}
pub fn unknown(position: &TopologySegment) -> Self {
Self {
position: &OBSERVATION_TOPOLOGY_ANCH0R,
severity: Severity::Unknown,
tier: ObservationTier::Composed,
annotation: Annotation::None,
temporal: Temporal::Current,
payload: None,
}
}
pub fn with_severity(severity: Severity, position: &TopologySegment) -> Self {
Self {
position: &OBSERVATION_TOPOLOGY_ANCH0R,
severity,
tier: ObservationTier::Composed,
annotation: Annotation::None,
temporal: Temporal::Current,
payload: None,
}
}
pub fn with_note(mut self, note: impl Into<String>) -> Self {
self.annotation = Annotation::Note(note.into());
self
}
pub fn at_tier(mut self, tier: ObservationTier) -> Self {
self.tier = tier;
self
}
pub fn with_temporal(mut self, temporal: Temporal) -> Self {
self.temporal = temporal;
self
}
}
#[allow(unsafe_code)]
unsafe impl<GLASS> DaemonicSystemCall for Observation<GLASS> {}
impl<GLASS> Glass<Observation<GLASS>> for Observation<GLASS>
where
GLASS: Glass<GLASS>,
{
type Anchor = TopologySegment;
fn position(&self) -> &TopologySegment {
&self.position
}
fn severity(&self) -> Severity {
self.severity
}
fn annotation(&self) -> Annotation {
self.annotation.clone()
}
fn temporal(&self) -> Temporal {
self.temporal.clone()
}
fn tier(&self) -> ObservationTier {
self.tier
}
fn payload(&self) -> Option<&Observation<GLASS>> {
Some(self)
}
fn into_payload(self) -> Option<Observation<GLASS>> {
Some(self)
}
}
impl<T, E: core::fmt::Display> From<Result<T, E>> for Observation<T> {
fn from(result: Result<T, E>) -> Self {
match result {
Ok(value) => Observation::stable(value, &TopologySegment::new("Daemonic::Glass::Boundary::Ok")),
Err(e) => Observation::shattered(&TopologySegment::new("Daemonic::Glass::Boundary::Err"))
.with_note(e.to_string()),
}
}
}
impl<T> From<Option<T>> for Observation<T> {
fn from(opt: Option<T>) -> Self {
match opt {
Some(value) => Observation::stable(value, &TopologySegment::new("Daemonic::Glass::Boundary::Option::Some")),
None => Observation::unknown(&TopologySegment::new("Daemonic::Glass::Boundary::Option::None")),
}
}
}
pub(crate) mod daemonic_system_call;