1use serde::{Deserialize, Serialize};
2use std::collections::BTreeMap;
3use std::path::PathBuf;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
7#[serde(rename_all = "snake_case")]
8pub enum OntologyFormat {
9 Turtle,
10 RdfXml,
11 Owl,
12 OwlXml,
13 JsonLd,
14 NTriples,
15 NQuads,
16 TriG,
17 Obo,
18 Unknown,
19}
20
21impl OntologyFormat {
22 pub fn from_extension(ext: &str) -> Self {
23 match ext.to_ascii_lowercase().as_str() {
24 "ttl" => Self::Turtle,
25 "rdf" => Self::RdfXml,
26 "owl" => Self::Owl,
27 "owx" => Self::OwlXml,
28 "jsonld" | "json-ld" => Self::JsonLd,
29 "nt" => Self::NTriples,
30 "nq" => Self::NQuads,
31 "trig" => Self::TriG,
32 "obo" => Self::Obo,
33 _ => Self::Unknown,
34 }
35 }
36
37 pub fn as_str(&self) -> &'static str {
38 match self {
39 Self::Turtle => "turtle",
40 Self::RdfXml => "rdf_xml",
41 Self::Owl => "owl",
42 Self::OwlXml => "owl_xml",
43 Self::JsonLd => "json_ld",
44 Self::NTriples => "n_triples",
45 Self::NQuads => "n_quads",
46 Self::TriG => "trig",
47 Self::Obo => "obo",
48 Self::Unknown => "unknown",
49 }
50 }
51}
52
53#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
54#[serde(rename_all = "snake_case")]
55pub enum ParseStatus {
56 Ok,
57 Warning,
58 Error,
59}
60
61impl ParseStatus {
62 pub fn as_str(&self) -> &'static str {
63 match self {
64 Self::Ok => "ok",
65 Self::Warning => "warning",
66 Self::Error => "error",
67 }
68 }
69}
70
71#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
72#[serde(rename_all = "snake_case")]
73pub enum EntityKind {
74 Class,
75 ObjectProperty,
76 DataProperty,
77 AnnotationProperty,
78 Individual,
79 Ontology,
80 Other,
81}
82
83impl EntityKind {
84 pub fn as_str(&self) -> &'static str {
85 match self {
86 Self::Class => "class",
87 Self::ObjectProperty => "object_property",
88 Self::DataProperty => "data_property",
89 Self::AnnotationProperty => "annotation_property",
90 Self::Individual => "individual",
91 Self::Ontology => "ontology",
92 Self::Other => "other",
93 }
94 }
95}
96
97#[derive(Debug, Clone, Default, Serialize, Deserialize)]
98pub struct SourceLocation {
99 pub line: Option<u64>,
100 pub column: Option<u64>,
101 #[serde(skip_serializing_if = "Option::is_none")]
102 pub start_byte: Option<u64>,
103 #[serde(skip_serializing_if = "Option::is_none")]
104 pub end_byte: Option<u64>,
105}
106
107impl SourceLocation {
108 pub fn is_empty(&self) -> bool {
109 self.line.is_none()
110 && self.column.is_none()
111 && self.start_byte.is_none()
112 && self.end_byte.is_none()
113 }
114
115 pub fn at_line_col(line: u64, column: u64) -> Self {
116 Self { line: Some(line), column: Some(column), start_byte: None, end_byte: None }
117 }
118}
119
120#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
121#[serde(rename_all = "snake_case")]
122pub enum DiagnosticSeverity {
123 Error,
124 Warning,
125 Info,
126}
127
128impl DiagnosticSeverity {
129 pub fn as_str(&self) -> &'static str {
130 match self {
131 Self::Error => "error",
132 Self::Warning => "warning",
133 Self::Info => "info",
134 }
135 }
136}
137
138#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
139#[serde(rename_all = "snake_case")]
140pub enum DiagnosticCode {
141 ParseError,
142 BrokenImport,
143 UndefinedPrefix,
144 DuplicateLabel,
145 MissingLabel,
146 OrphanClass,
147 OwlBridgeFailed,
148 IoReadError,
149}
150
151impl DiagnosticCode {
152 pub fn as_str(&self) -> &'static str {
153 match self {
154 Self::ParseError => "parse_error",
155 Self::BrokenImport => "broken_import",
156 Self::UndefinedPrefix => "undefined_prefix",
157 Self::DuplicateLabel => "duplicate_label",
158 Self::MissingLabel => "missing_label",
159 Self::OrphanClass => "orphan_class",
160 Self::OwlBridgeFailed => "owl_bridge_failed",
161 Self::IoReadError => "io_read_error",
162 }
163 }
164}
165
166#[derive(Debug, Clone, Serialize, Deserialize)]
167pub struct Diagnostic {
168 pub code: DiagnosticCode,
169 pub severity: DiagnosticSeverity,
170 pub message: String,
171 pub file: PathBuf,
172 pub range: SourceLocation,
173 #[serde(skip_serializing_if = "Option::is_none")]
174 pub entity_iri: Option<String>,
175 #[serde(skip_serializing_if = "Option::is_none")]
176 pub quick_fix: Option<String>,
177}
178
179#[derive(Debug, Clone, Serialize, Deserialize)]
180pub struct OntologyDocument {
181 pub id: String,
182 pub path: PathBuf,
183 pub format: OntologyFormat,
184 pub base_iri: Option<String>,
185 pub imports: Vec<String>,
186 pub namespaces: BTreeMap<String, String>,
187 pub parse_status: ParseStatus,
188 pub content_hash: String,
189 pub modified_time: u64,
190 pub parse_message: Option<String>,
191 pub parse_error_location: Option<SourceLocation>,
192}
193
194#[derive(Debug, Clone, Serialize, Deserialize)]
195pub struct Entity {
196 pub iri: String,
197 pub short_name: String,
198 pub kind: EntityKind,
199 pub ontology_id: String,
200 pub source_location: SourceLocation,
201 pub labels: Vec<String>,
202 pub comments: Vec<String>,
203 pub deprecated: bool,
204 #[serde(default, skip_serializing_if = "Option::is_none")]
205 pub obo_id: Option<String>,
206 #[serde(default, skip_serializing_if = "PropertyCharacteristics::is_empty")]
207 pub characteristics: PropertyCharacteristics,
208}
209
210#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
211pub struct PropertyCharacteristics {
212 pub functional: bool,
213 pub inverse_functional: bool,
214 pub transitive: bool,
215 pub symmetric: bool,
216 pub asymmetric: bool,
217 pub reflexive: bool,
218 pub irreflexive: bool,
219}
220
221impl PropertyCharacteristics {
222 pub fn is_empty(&self) -> bool {
223 !self.functional
224 && !self.inverse_functional
225 && !self.transitive
226 && !self.symmetric
227 && !self.asymmetric
228 && !self.reflexive
229 && !self.irreflexive
230 }
231}
232
233impl Default for Entity {
234 fn default() -> Self {
235 Self {
236 iri: String::new(),
237 short_name: String::new(),
238 kind: EntityKind::Class,
239 ontology_id: String::new(),
240 source_location: SourceLocation::default(),
241 labels: Vec::new(),
242 comments: Vec::new(),
243 deprecated: false,
244 obo_id: None,
245 characteristics: PropertyCharacteristics::default(),
246 }
247 }
248}
249
250#[derive(Debug, Clone, Serialize, Deserialize)]
251pub struct Annotation {
252 pub subject: String,
253 pub predicate: String,
254 pub object: String,
255 pub ontology_id: String,
256 #[serde(default, skip_serializing_if = "SourceLocation::is_empty")]
257 pub source_location: SourceLocation,
258}
259
260#[derive(Debug, Clone, Serialize, Deserialize)]
261pub struct Axiom {
262 pub id: String,
263 pub ontology_id: String,
264 pub subject: String,
265 pub predicate: String,
266 pub object: String,
267 pub axiom_kind: String,
268 #[serde(default, skip_serializing_if = "SourceLocation::is_empty")]
269 pub source_location: SourceLocation,
270}
271
272#[derive(Debug, Clone, Serialize, Deserialize)]
273pub struct Namespace {
274 pub prefix: String,
275 pub iri: String,
276 pub ontology_id: String,
277}
278
279#[derive(Debug, Clone, Serialize, Deserialize)]
280pub struct Import {
281 pub ontology_id: String,
282 pub import_iri: String,
283}
284
285pub const AXIOM_KIND_SUB_CLASS_OF: &str = "sub_class_of";
287pub const AXIOM_KIND_EQUIVALENT_CLASS: &str = "equivalent_class";
288pub const AXIOM_KIND_DISJOINT_CLASS: &str = "disjoint_class";
289pub const AXIOM_KIND_DOMAIN: &str = "domain";
290pub const AXIOM_KIND_RANGE: &str = "range";
291pub const AXIOM_KIND_PROPERTY_CHAIN: &str = "property_chain";
292pub const AXIOM_KIND_CLASS_ASSERTION: &str = "class_assertion";
293pub const AXIOM_KIND_OBJECT_PROPERTY_ASSERTION: &str = "object_property_assertion";
294pub const AXIOM_KIND_DATA_PROPERTY_ASSERTION: &str = "data_property_assertion";
295
296#[cfg(test)]
297mod tests {
298 use super::ParseStatus;
299
300 #[test]
301 fn parse_status_as_str_matches_serde() {
302 assert_eq!(ParseStatus::Ok.as_str(), "ok");
303 assert_eq!(ParseStatus::Warning.as_str(), "warning");
304 assert_eq!(ParseStatus::Error.as_str(), "error");
305 }
306}