1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;

use super::EntityFrame;
use super::HeaderFrame;
use super::InstanceFrame;
use super::TermFrame;
use super::TypedefFrame;

use crate::semantics::Orderable;

/// Any kind of OBO frame.
///
/// This is used by the `crate::parser::FrameReader`, since they iterate on
/// all the frames of the OBO document. This type does however not appear in
/// the `OboDoc` syntax tree since the `HeaderFrame` and `EntityFrame` are
/// properly separated there.
///
/// # Ordering
/// [Serializer conventions](https://owlcollab.github.io/oboformat/doc/GO.format.obo-1_4.html#S.3.5.2)
/// dictate that frames should be Serialized first with `[Typedef]` frames, then
/// `[Term]`, and then `[Instance]`, which is reflected here in the order of the
/// variants.
#[derive(Debug, Clone, PartialEq)]
pub enum Frame {
    Header(Box<HeaderFrame>),
    Typedef(Box<TypedefFrame>),
    Term(Box<TermFrame>),
    Instance(Box<InstanceFrame>),
}

impl Frame {
    /// Return a reference to the [`HeaderFrame`] if the frame is one, or `None`.
    ///
    /// [`HeaderFrame`]: ./struct.HeaderFrame.html
    pub fn as_header(&self) -> Option<&HeaderFrame> {
        if let Frame::Header(frame) = self {
            Some(frame.as_ref())
        } else {
            None
        }
    }

    /// Return a mutable reference to the [`HeaderFrame`] if the frame is one, or `None`.
    ///
    /// [`HeaderFrame`]: ./struct.HeaderFrame.html
    pub fn as_header_mut(&mut self) -> Option<&mut HeaderFrame> {
        if let Frame::Header(frame) = self {
            Some(frame.as_mut())
        } else {
            None
        }
    }

    /// Return a reference to the [`TermFrame`] if the frame is one, or `None`.
    ///
    /// [`TermFrame`]: ./struct.TermFrame.html
    pub fn as_term(&self) -> Option<&TermFrame> {
        if let Frame::Term(frame) = self {
            Some(frame.as_ref())
        } else {
            None
        }
    }

    /// Return a mutable reference to the [`TermFrame`] if the frame is one, or `None`.
    ///
    /// [`TermFrame`]: ./struct.TermFrame.html
    pub fn as_term_mut(&mut self) -> Option<&mut TermFrame> {
        if let Frame::Term(frame) = self {
            Some(frame.as_mut())
        } else {
            None
        }
    }

    /// Return a reference to the [`TypedefFrame`] if the frame is one, or `None`.
    ///
    /// [`TypedefFrame`]: ./struct.TypedefFrame.html
    pub fn as_typedef(&self) -> Option<&TypedefFrame> {
        if let Frame::Typedef(frame) = self {
            Some(frame.as_ref())
        } else {
            None
        }
    }

    /// Return a reference to the [`TypedefFrame`] if the frame is one, or `None`.
    ///
    /// [`TypedefFrame`]: ./struct.TypedefFrame.html
    pub fn as_typedef_mut(&mut self) -> Option<&mut TypedefFrame> {
        if let Frame::Typedef(frame) = self {
            Some(frame.as_mut())
        } else {
            None
        }
    }

    /// Return a reference to the [`InstanceFrame`] if the frame is one, or `None`.
    ///
    /// [`InstanceFrame`]: ./struct.InstanceFrame.html
    pub fn as_instance(&self) -> Option<&InstanceFrame> {
        if let Frame::Instance(frame) = self {
            Some(frame.as_ref())
        } else {
            None
        }
    }

    /// Return a mutable reference to the [`InstanceFrame`] if the frame is one, or `None`.
    ///
    /// [`InstanceFrame`]: ./struct.InstanceFrame.html
    pub fn as_instance_mut(&mut self) -> Option<&mut InstanceFrame> {
        if let Frame::Instance(frame) = self {
            Some(frame.as_mut())
        } else {
            None
        }
    }

    /// Attempt to convert the frame into a `HeaderFrame`.
    pub fn into_header(self) -> Option<HeaderFrame> {
        if let Frame::Header(h) = self {
            Some(*h)
        } else {
            None
        }
    }

    /// Attempt to convert the frame into an `EntityFrame`.
    pub fn into_entity(self) -> Option<EntityFrame> {
        match self {
            Frame::Term(f) => Some(EntityFrame::Term(f)),
            Frame::Typedef(f) => Some(EntityFrame::Typedef(f)),
            Frame::Instance(f) => Some(EntityFrame::Instance(f)),
            Frame::Header(_) => None,
        }
    }
}

impl Display for Frame {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        use self::Frame::*;
        match self {
            Header(h) => h.fmt(f),
            Term(t) => t.fmt(f),
            Typedef(t) => t.fmt(f),
            Instance(i) => i.fmt(f),
        }
    }
}

impl From<HeaderFrame> for Frame {
    fn from(frame: HeaderFrame) -> Self {
        Frame::Header(Box::new(frame))
    }
}

impl From<TermFrame> for Frame {
    fn from(frame: TermFrame) -> Self {
        Frame::Term(Box::new(frame))
    }
}

impl From<TypedefFrame> for Frame {
    fn from(frame: TypedefFrame) -> Self {
        Frame::Typedef(Box::new(frame))
    }
}

impl From<InstanceFrame> for Frame {
    fn from(frame: InstanceFrame) -> Self {
        Frame::Instance(Box::new(frame))
    }
}

impl From<EntityFrame> for Frame {
    fn from(frame: EntityFrame) -> Self {
        match frame {
            EntityFrame::Term(f) => Frame::Term(f),
            EntityFrame::Instance(f) => Frame::Instance(f),
            EntityFrame::Typedef(f) => Frame::Typedef(f),
        }
    }
}

impl Orderable for Frame {
    fn sort(&mut self) {
        use self::Frame::*;
        match self {
            Header(h) => h.sort(),
            Term(t) => t.sort(),
            Typedef(t) => t.sort(),
            Instance(i) => i.sort(),
        }
    }
    fn is_sorted(&self) -> bool {
        use self::Frame::*;
        match self {
            Header(h) => h.is_sorted(),
            Term(t) => t.is_sorted(),
            Typedef(t) => t.is_sorted(),
            Instance(i) => i.is_sorted(),
        }
    }
}