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(
12 try_from = "OntologyPatchInput<L, B>",
13 bound(deserialize = "L: serde::Deserialize<'de>, B: serde::Deserialize<'de>")
14)]
15pub struct OntologyPatch<L = crate::OwlLiteral, B = crate::BlankNodeId> {
16 target: OntologyId,
17 operations: Vec<OntologyPatchOperation<L, B>>,
19}
20
21impl<L, B> OntologyPatch<L, B> {
22 pub fn try_new(
29 target: OntologyId,
30 operations: Vec<OntologyPatchOperation<L, B>>,
31 ) -> Result<Self, StructuralModelError> {
32 if operations.is_empty() {
33 return Err(StructuralModelError::InvalidFormat {
34 kind: KIND_ONTOLOGY_PATCH,
35 value: "0 operations".to_owned(),
36 message: "requires at least one operation",
37 });
38 }
39 Ok(Self { target, operations })
40 }
41
42 #[must_use]
44 pub fn target(&self) -> &OntologyId {
45 &self.target
46 }
47
48 #[must_use]
50 pub fn operations(&self) -> &[OntologyPatchOperation<L, B>] {
51 &self.operations
52 }
53}
54
55#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
57pub enum OntologyPatchOperation<L = crate::OwlLiteral, B = crate::BlankNodeId> {
58 AddImport {
60 import_iri: Iri<String>,
62 },
63 RemoveImport {
65 import_iri: Iri<String>,
67 },
68 AddAnnotation {
70 annotation: Annotation<L, B>,
72 },
73 RemoveAnnotation {
75 annotation: Annotation<L, B>,
77 },
78 AddAxiom {
80 axiom: Axiom<L, B>,
82 },
83 RemoveAxiom {
85 axiom: Axiom<L, B>,
87 },
88}
89
90#[derive(serde::Deserialize)]
91#[serde(rename = "OntologyPatch<L, B>")]
92struct OntologyPatchInput<L, B> {
93 target: OntologyId,
94 operations: Vec<OntologyPatchOperation<L, B>>,
96}
97
98impl<L, B> TryFrom<OntologyPatchInput<L, B>> for OntologyPatch<L, B> {
99 type Error = StructuralModelError;
100
101 fn try_from(input: OntologyPatchInput<L, B>) -> Result<Self, Self::Error> {
102 Self::try_new(input.target, input.operations)
103 }
104}