flams_ontology/content/declarations/
structures.rs

1use crate::{
2    content::ModuleTrait, uris::{ContentURIRef, SymbolURI}, Checked, CheckingState, Resolvable
3};
4
5use super::{Declaration, DeclarationTrait, OpenDeclaration};
6
7
8#[derive(Debug)]
9//#[cfg_attr(feature="serde", derive(serde::Serialize))]
10pub struct MathStructure<State:CheckingState> {
11    pub uri: SymbolURI,
12    pub elements: State::Seq<OpenDeclaration<State>>,
13    pub macroname: Option<Box<str>>,
14}
15impl Resolvable for MathStructure<Checked> {
16    type From = SymbolURI;
17    fn id(&self) -> std::borrow::Cow<'_,Self::From> {
18        std::borrow::Cow::Borrowed(&self.uri)
19    }
20}
21impl super::private::Sealed for MathStructure<Checked> {}
22impl DeclarationTrait for MathStructure<Checked> {
23    #[inline]
24    fn from_declaration(decl: &Declaration) -> Option<&Self> {
25        match decl {
26            Declaration::MathStructure(m) => Some(m),
27            _ => None,
28        }
29    }
30}
31impl ModuleTrait for MathStructure<Checked> {
32    #[inline]
33    fn declarations(&self) -> &[Declaration] {
34        &self.elements
35    }
36    #[inline]
37    fn content_uri(&self) -> ContentURIRef {
38        ContentURIRef::Symbol(&self.uri)
39    }
40}
41
42
43#[derive(Debug)]
44pub struct Extension<State:CheckingState> {
45    pub uri: SymbolURI,
46    pub target: State::Decl<MathStructure<Checked>>,
47    pub elements: State::Seq<OpenDeclaration<State>>,
48}
49impl Resolvable for Extension<Checked> {
50    type From = SymbolURI;
51    fn id(&self) -> std::borrow::Cow<'_,Self::From> {
52        std::borrow::Cow::Borrowed(&self.uri)
53    }
54}
55impl super::private::Sealed for Extension<Checked> {}
56impl DeclarationTrait for Extension<Checked> {
57    #[inline]
58    fn from_declaration(decl: &Declaration) -> Option<&Self> {
59        match decl {
60            Declaration::Extension(m) => Some(m),
61            _ => None,
62        }
63    }
64}
65impl ModuleTrait for Extension<Checked> {
66    #[inline]
67    fn declarations(&self) -> &[Declaration] {
68        &self.elements
69    }
70    #[inline]
71    fn content_uri(&self) -> ContentURIRef {
72        ContentURIRef::Symbol(&self.uri)
73    }
74}
75
76crate::serde_impl!{mod serde_impl_struct =
77    struct MathStructure[uri,elements,macroname]
78}
79crate::serde_impl!{mod serde_impl_ext =
80    struct Extension[uri,target,elements]
81}
82
83/*
84crate::serde_impl!{
85    MathStructure : slf
86    s => {
87        let mut s = s.serialize_struct("MathStructure", 3)?;
88        s.serialize_field("uri", &slf.uri);
89        s.serialize_field("elements", &slf.elements);
90        s.serialize_field("macroname", &slf.macroname);
91        s.end()
92    } 
93    de => {
94        #[derive(serde::Deserialize)]
95        #[allow(non_camel_case_types)]
96        enum Field {uri,elements,macroname}
97
98        struct Visitor;
99        impl<'de> serde::de::Visitor<'de> for Visitor {
100            type Value = MathStructure<Unchecked>;
101            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
102                formatter.write_str("struct MathStructure")
103            }
104            fn visit_seq<V>(self, mut seq: V) -> Result<MathStructure<Unchecked>, V::Error>
105            where
106                V: serde::de::SeqAccess<'de>,
107            {
108                let uri = seq.next_element()?
109                    .ok_or_else(|| serde::de::Error::invalid_length(0, &self))?;
110                let elements = seq.next_element()?
111                    .ok_or_else(|| serde::de::Error::invalid_length(1, &self))?;
112                let macroname = seq.next_element()?
113                    .ok_or_else(|| serde::de::Error::invalid_length(1, &self))?;
114                Ok(MathStructure{ uri, elements, macroname })
115            }
116
117            fn visit_map<V>(self, mut map: V) -> Result<MathStructure<Unchecked>, V::Error>
118            where
119                V: serde::de::MapAccess<'de>,
120            {
121                let mut uri = None;
122                let mut elements = None;
123                let mut macroname = None;
124                while let Some(key) = map.next_key()? {
125                    match key {
126                        Field::uri => {
127                            if uri.is_some() {
128                                return Err(serde::de::Error::duplicate_field("uri"));
129                            }
130                            uri = Some(map.next_value()?);
131                        }
132                        Field::elements => {
133                            if elements.is_some() {
134                                return Err(serde::de::Error::duplicate_field("elements"));
135                            }
136                            elements = Some(map.next_value()?);
137                        }
138                        Field::macroname => {
139                            if macroname.is_some() {
140                                return Err(serde::de::Error::duplicate_field("macroname"));
141                            }
142                            macroname = Some(map.next_value()?);
143                        }
144                    }
145                }
146                let uri = uri.ok_or_else(|| serde::de::Error::missing_field("uri"))?;
147                let elements = elements.ok_or_else(|| serde::de::Error::missing_field("elements"))?;
148                let macroname = macroname.ok_or_else(|| serde::de::Error::missing_field("macroname"))?;
149                Ok(MathStructure { uri, elements, macroname })
150            }
151        }
152        de.deserialize_struct(stringify!(MathStructure), &["uri","elements","macroname"], Visitor)
153    }
154}
155     */