Skip to main content

owl_structural_model/
patch.rs

1//! Structural ontology patch values.
2
3use iri_rfc3987::Iri;
4
5use crate::{Annotation, Axiom, OntologyId, StructuralModelError};
6
7const KIND_ONTOLOGY_PATCH: &str = "ontology patch";
8
9/// A non-empty collection of structural changes for one ontology identity.
10#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
11pub struct OntologyPatch {
12    target: OntologyId,
13    // bounded: one submitted patch/change request, never a workspace stream.
14    operations: Vec<OntologyPatchOperation>,
15}
16
17impl OntologyPatch {
18    /// Create a non-empty ontology patch.
19    ///
20    /// # Errors
21    ///
22    /// Returns [`StructuralModelError::InvalidFormat`] when no operations are
23    /// supplied.
24    pub fn try_new(
25        target: OntologyId,
26        operations: Vec<OntologyPatchOperation>,
27    ) -> Result<Self, StructuralModelError> {
28        if operations.is_empty() {
29            return Err(StructuralModelError::InvalidFormat {
30                kind: KIND_ONTOLOGY_PATCH,
31                value: "0 operations".to_owned(),
32                message: "requires at least one operation",
33            });
34        }
35        Ok(Self { target, operations })
36    }
37
38    /// Return the patch target ontology ID.
39    #[must_use]
40    pub fn target(&self) -> &OntologyId { &self.target }
41
42    /// Return patch operations.
43    #[must_use]
44    pub fn operations(&self) -> &[OntologyPatchOperation] { &self.operations }
45}
46
47/// One structural ontology patch operation.
48#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
49pub enum OntologyPatchOperation {
50    /// Add an import declaration.
51    AddImport {
52        /// Imported ontology IRI.
53        import_iri: Iri<String>,
54    },
55    /// Remove an import declaration.
56    RemoveImport {
57        /// Imported ontology IRI.
58        import_iri: Iri<String>,
59    },
60    /// Add an ontology annotation.
61    AddAnnotation {
62        /// Ontology annotation.
63        annotation: Annotation,
64    },
65    /// Remove an ontology annotation.
66    RemoveAnnotation {
67        /// Ontology annotation.
68        annotation: Annotation,
69    },
70    /// Add a structural axiom.
71    AddAxiom {
72        /// Structural axiom.
73        axiom: Axiom,
74    },
75    /// Remove a structural axiom.
76    RemoveAxiom {
77        /// Structural axiom.
78        axiom: Axiom,
79    },
80}