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 {
41        &self.target
42    }
43
44    /// Return patch operations.
45    #[must_use]
46    pub fn operations(&self) -> &[OntologyPatchOperation] {
47        &self.operations
48    }
49}
50
51/// One structural ontology patch operation.
52#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
53pub enum OntologyPatchOperation {
54    /// Add an import declaration.
55    AddImport {
56        /// Imported ontology IRI.
57        import_iri: Iri<String>,
58    },
59    /// Remove an import declaration.
60    RemoveImport {
61        /// Imported ontology IRI.
62        import_iri: Iri<String>,
63    },
64    /// Add an ontology annotation.
65    AddAnnotation {
66        /// Ontology annotation.
67        annotation: Annotation,
68    },
69    /// Remove an ontology annotation.
70    RemoveAnnotation {
71        /// Ontology annotation.
72        annotation: Annotation,
73    },
74    /// Add a structural axiom.
75    AddAxiom {
76        /// Structural axiom.
77        axiom: Axiom,
78    },
79    /// Remove a structural axiom.
80    RemoveAxiom {
81        /// Structural axiom.
82        axiom: Axiom,
83    },
84}