owl_structural_model/
patch.rs1use iri_rfc3987::Iri;
4
5use crate::{Annotation, Axiom, OntologyId, StructuralModelError};
6
7const KIND_ONTOLOGY_PATCH: &str = "ontology patch";
8
9#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
11#[serde(deny_unknown_fields)]
12#[serde(
13 try_from = "OntologyPatchInput<L, B>",
14 bound(deserialize = "L: serde::Deserialize<'de>, B: serde::Deserialize<'de>")
15)]
16pub struct OntologyPatch<L = crate::OwlLiteral, B = crate::BlankNodeId> {
17 target: OntologyId,
18 operations: Vec<OntologyPatchOperation<L, B>>,
20}
21
22impl<L, B> OntologyPatch<L, B> {
23 pub fn try_new(
30 target: OntologyId,
31 operations: Vec<OntologyPatchOperation<L, B>>,
32 ) -> Result<Self, StructuralModelError> {
33 if operations.is_empty() {
34 return Err(StructuralModelError::InvalidFormat {
35 kind: KIND_ONTOLOGY_PATCH,
36 value: "0 operations".to_owned(),
37 message: "requires at least one operation",
38 });
39 }
40 Ok(Self { target, operations })
41 }
42
43 #[must_use]
45 pub fn target(&self) -> &OntologyId {
46 &self.target
47 }
48
49 #[must_use]
51 pub fn operations(&self) -> &[OntologyPatchOperation<L, B>] {
52 &self.operations
53 }
54}
55
56#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
58#[serde(deny_unknown_fields)]
59pub enum OntologyPatchOperation<L = crate::OwlLiteral, B = crate::BlankNodeId> {
60 AddImport {
62 import_iri: Iri<String>,
64 },
65 RemoveImport {
67 import_iri: Iri<String>,
69 },
70 AddAnnotation {
72 annotation: Annotation<L, B>,
74 },
75 RemoveAnnotation {
77 annotation: Annotation<L, B>,
79 },
80 AddAxiom {
82 axiom: Axiom<L, B>,
84 },
85 RemoveAxiom {
87 axiom: Axiom<L, B>,
89 },
90}
91
92#[derive(serde::Deserialize)]
93#[serde(deny_unknown_fields)]
94#[serde(rename = "OntologyPatch<L, B>")]
95struct OntologyPatchInput<L, B> {
96 target: OntologyId,
97 operations: Vec<OntologyPatchOperation<L, B>>,
99}
100
101impl<L, B> TryFrom<OntologyPatchInput<L, B>> for OntologyPatch<L, B> {
102 type Error = StructuralModelError;
103
104 fn try_from(input: OntologyPatchInput<L, B>) -> Result<Self, Self::Error> {
105 Self::try_new(input.target, input.operations)
106 }
107}