guion/util/
error.rs

1use std::error::Error;
2use std::fmt::{Debug,Display};
3
4use super::*;
5
6#[derive(Clone)]
7#[non_exhaustive]
8pub enum GuionError<E> where E: Env {
9    TraitcastError(Box<TraitcastError>),
10    ResolveError(Box<ResolveError<E>>),
11    Empty,
12}
13
14#[derive(Clone)]
15pub struct ResolveError<E> where E: Env {
16    pub op: &'static str,
17    pub sub_path: E::WidgetPath,
18    pub widget_type: Vec<&'static str>,
19    pub child_info: Vec<GuionResolveErrorChildInfo<E>>,
20}
21#[derive(Clone)]
22pub struct TraitcastError {
23    pub op: &'static str,
24    pub src_type: Vec<&'static str>,
25    pub dest_trait_type: &'static str,
26}
27
28#[derive(Clone)]
29pub struct GuionResolveErrorChildInfo<E> where E: Env {
30    pub child_idx: usize,
31    pub widget_type: Vec<&'static str>,
32    pub widget_path_if_path: Option<E::WidgetPath>,
33    pub widget_id: Option<E::WidgetID>,
34}
35
36impl<E> Error for GuionError<E> where E: Env {
37    
38}
39
40impl<E> From<()> for GuionError<E> where E: Env {
41    fn from(_: ()) -> Self {
42        Self::Empty
43    }
44}
45
46impl<E> Display for GuionError<E> where E: Env {
47    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48        match self {
49            Self::TraitcastError(e) => {
50                write!(f,"\n\nFailed to {}\n\n",e.op)?;
51                for v in &e.src_type {
52                    write!(f,"\tsrc  = {}\n",v)?;
53                }
54                write!(f,"\n\tdest = {}\n\n",e.dest_trait_type)?;
55            }
56            Self::ResolveError(e) => {
57                write!(f,"\n\nFailed to {} child in Widget\n\n",e.op)?;
58                write!(f,"\tsub_path = {:?}\n",e.sub_path)?;
59                for v in &e.widget_type {
60                    write!(f,"\ttype     = {}\n",v)?;
61                }
62                write!(f,"\n")?;
63                for c in &e.child_info {
64                    if let Some(v) = &c.widget_id {
65                        write!(f,"\tChild #{} id   = {:?}\n",c.child_idx,v)?;
66                    }
67                    if let Some(v) = &c.widget_path_if_path {
68                        write!(f,"\tChild #{} path = {:?}\n",c.child_idx,v)?;
69                    }
70                    for v in &c.widget_type {
71                        write!(f,"\tChild #{} type = {}\n",c.child_idx,v)?;
72                    }
73                    write!(f,"\n")?;
74                }
75                
76            }
77            Self::Empty => {}, //TODO
78        }
79        Ok(())
80    }
81}
82
83impl<E> Debug for GuionError<E> where E: Env {
84    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85        Display::fmt(self,f)
86    }
87}