fastobo/ast/instance/
clause.rs

1use fastobo_derive_internal::FromStr;
2use fastobo_derive_internal::OboClause;
3
4use crate::ast::*;
5use crate::error::SyntaxError;
6use crate::parser::Cache;
7use crate::parser::FromPair;
8use crate::semantics::OboClause;
9use crate::syntax::pest::iterators::Pair;
10use crate::syntax::Rule;
11
12/// A clause appearing in an instance frame.
13#[derive(Clone, Debug, Eq, FromStr, Hash, Ord, OboClause, PartialEq, PartialOrd)]
14pub enum InstanceClause {
15    #[clause(cardinality = "ZeroOrOne")]
16    IsAnonymous(bool),
17    #[clause(cardinality = "ZeroOrOne")]
18    Name(Box<UnquotedString>),
19    #[clause(cardinality = "One")]
20    Namespace(Box<NamespaceIdent>),
21    AltId(Box<Ident>),
22    #[clause(cardinality = "ZeroOrOne")]
23    Def(Box<Definition>),
24    Comment(Box<UnquotedString>),
25    Subset(Box<SubsetIdent>),
26    Synonym(Box<Synonym>),
27    Xref(Box<Xref>),
28    PropertyValue(Box<PropertyValue>),
29    InstanceOf(Box<ClassIdent>),
30    Relationship(Box<RelationIdent>, Box<Ident>), // QUESTION(@althonos): InstanceId ?
31    #[clause(cardinality = "ZeroOrOne")]
32    CreatedBy(Box<UnquotedString>),
33    #[clause(cardinality = "ZeroOrOne")]
34    CreationDate(Box<CreationDate>),
35    #[clause(cardinality = "ZeroOrOne")]
36    IsObsolete(bool),
37    ReplacedBy(Box<InstanceIdent>),
38    Consider(Box<Ident>),
39}
40
41clause_impl_from!(InstanceClause);
42
43impl<'i> FromPair<'i> for InstanceClause {
44    const RULE: Rule = Rule::InstanceClause;
45    unsafe fn from_pair_unchecked(
46        pair: Pair<'i, Rule>,
47        cache: &Cache,
48    ) -> Result<Self, SyntaxError> {
49        let mut inner = pair.into_inner();
50        match inner.next().unwrap().as_rule() {
51            Rule::IsAnonymousTag => {
52                let b = bool::from_pair_unchecked(inner.next().unwrap(), cache)?;
53                Ok(InstanceClause::IsAnonymous(b))
54            }
55            Rule::NameTag => {
56                let n = UnquotedString::from_pair_unchecked(inner.next().unwrap(), cache)?;
57                Ok(InstanceClause::Name(Box::new(n)))
58            }
59            Rule::NamespaceTag => {
60                let ns = NamespaceIdent::from_pair_unchecked(inner.next().unwrap(), cache)?;
61                Ok(InstanceClause::Namespace(Box::new(ns)))
62            }
63            Rule::AltIdTag => {
64                let id = Ident::from_pair_unchecked(inner.next().unwrap(), cache)?;
65                Ok(InstanceClause::AltId(Box::new(id)))
66            }
67            Rule::DefTag => {
68                let def = Definition::from_pair_unchecked(inner.next().unwrap(), cache)?;
69                Ok(InstanceClause::Def(Box::new(def)))
70            }
71            Rule::CommentTag => {
72                let s = UnquotedString::from_pair_unchecked(inner.next().unwrap(), cache)?;
73                Ok(InstanceClause::Comment(Box::new(s)))
74            }
75            Rule::SubsetTag => {
76                let id = SubsetIdent::from_pair_unchecked(inner.next().unwrap(), cache)?;
77                Ok(InstanceClause::Subset(Box::new(id)))
78            }
79            Rule::SynonymTag => {
80                let syn = Synonym::from_pair_unchecked(inner.next().unwrap(), cache)?;
81                Ok(InstanceClause::Synonym(Box::new(syn)))
82            }
83            Rule::XrefTag => {
84                let xref = Xref::from_pair_unchecked(inner.next().unwrap(), cache)?;
85                Ok(InstanceClause::Xref(Box::new(xref)))
86            }
87            Rule::PropertyValueTag => {
88                let pv = PropertyValue::from_pair_unchecked(inner.next().unwrap(), cache)?;
89                Ok(InstanceClause::PropertyValue(Box::new(pv)))
90            }
91            Rule::InstanceOfTag => {
92                let id = ClassIdent::from_pair_unchecked(inner.next().unwrap(), cache)?;
93                Ok(InstanceClause::InstanceOf(Box::new(id)))
94            }
95            Rule::RelationshipTag => {
96                let r = RelationIdent::from_pair_unchecked(inner.next().unwrap(), cache)?;
97                let id = Ident::from_pair_unchecked(inner.next().unwrap(), cache)?;
98                Ok(InstanceClause::Relationship(Box::new(r), Box::new(id)))
99            }
100            Rule::CreatedByTag => {
101                let s = UnquotedString::from_pair_unchecked(inner.next().unwrap(), cache)?;
102                Ok(InstanceClause::CreatedBy(Box::new(s)))
103            }
104            Rule::CreationDateTag => {
105                let dt = CreationDate::from_pair_unchecked(inner.next().unwrap(), cache)?;
106                Ok(InstanceClause::CreationDate(Box::new(dt)))
107            }
108            Rule::IsObsoleteTag => {
109                let b = bool::from_pair_unchecked(inner.next().unwrap(), cache)?;
110                Ok(InstanceClause::IsObsolete(b))
111            }
112            Rule::ReplacedByTag => {
113                let id = InstanceIdent::from_pair_unchecked(inner.next().unwrap(), cache)?;
114                Ok(InstanceClause::ReplacedBy(Box::new(id)))
115            }
116            Rule::ConsiderTag => {
117                let id = Ident::from_pair_unchecked(inner.next().unwrap(), cache)?;
118                Ok(InstanceClause::Consider(Box::new(id)))
119            }
120            _ => unreachable!(),
121        }
122    }
123}
124
125impl<'i> FromPair<'i> for Line<InstanceClause> {
126    const RULE: Rule = Rule::InstanceClauseLine;
127    unsafe fn from_pair_unchecked(
128        pair: Pair<'i, Rule>,
129        cache: &Cache,
130    ) -> Result<Self, SyntaxError> {
131        let mut inner = pair.into_inner();
132        let clause = InstanceClause::from_pair_unchecked(inner.next().unwrap(), cache)?;
133        let eol = inner.next().unwrap();
134        Ok(Eol::from_pair_unchecked(eol, cache)?.and_inner(clause))
135    }
136}