Skip to main content

postproject_core/
conflict.rs

1//! Semantic optimistic-concurrency values.
2
3use crate::{
4    ExternalIdentifier, MediaRootId, MetadataProperty, ObjectRef, RepresentationId, ResourceId,
5    RevisionId,
6};
7
8/// Stable category of a semantic conflict key.
9#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
10#[non_exhaustive]
11pub enum ConflictKeyKind {
12    /// The complete current locator set of one resource.
13    LocatorSet,
14    /// One metadata property on one object.
15    MetadataProperty,
16    /// The complete dependency observation of one representation.
17    DependencySet,
18    /// One configured media root.
19    MediaRoot,
20    /// One exact external-identifier attachment.
21    ExternalIdentifier,
22    /// One current resource-fingerprint domain.
23    ResourceFingerprint,
24    /// One current representation-fingerprint domain.
25    RepresentationFingerprint,
26    /// The current size and modification time of one resource.
27    ResourceFileFacts,
28}
29
30impl ConflictKeyKind {
31    /// Returns the stable `snake_case` category name.
32    #[must_use]
33    pub const fn as_str(self) -> &'static str {
34        match self {
35            Self::LocatorSet => "locator_set",
36            Self::MetadataProperty => "metadata_property",
37            Self::DependencySet => "dependency_set",
38            Self::MediaRoot => "media_root",
39            Self::ExternalIdentifier => "external_identifier",
40            Self::ResourceFingerprint => "resource_fingerprint",
41            Self::RepresentationFingerprint => "representation_fingerprint",
42            Self::ResourceFileFacts => "resource_file_facts",
43        }
44    }
45}
46
47/// One non-mergeable semantic fact touched by a transaction.
48#[derive(Clone, Debug, Eq, Hash, PartialEq)]
49#[non_exhaustive]
50pub enum SemanticConflictKey {
51    /// The current size and modification time of one resource.
52    ResourceFileFacts(ResourceId),
53    /// The complete current locator set of one resource.
54    LocatorSet(ResourceId),
55    /// One metadata property on one object.
56    MetadataProperty {
57        /// Object whose property is replaced or removed.
58        target: ObjectRef,
59        /// Exact vocabulary and property identity.
60        property: MetadataProperty,
61    },
62    /// The complete dependency observation of one representation.
63    DependencySet(RepresentationId),
64    /// One configured media root.
65    MediaRoot(MediaRootId),
66    /// One exact external-identifier attachment on one object.
67    ExternalIdentifier {
68        /// Object carrying the attachment.
69        target: ObjectRef,
70        /// Exact external identifier.
71        identifier: ExternalIdentifier,
72    },
73    /// One current resource-fingerprint algorithm and version.
74    ResourceFingerprint {
75        /// Resource carrying the fingerprint.
76        resource_id: ResourceId,
77        /// Exact fingerprint algorithm identifier.
78        algorithm: String,
79        /// Fingerprint algorithm format version.
80        version: u16,
81    },
82    /// One current representation-fingerprint algorithm and version.
83    RepresentationFingerprint {
84        /// Representation carrying the fingerprint.
85        representation_id: RepresentationId,
86        /// Exact fingerprint algorithm identifier.
87        algorithm: String,
88        /// Fingerprint algorithm format version.
89        version: u16,
90    },
91}
92
93impl SemanticConflictKey {
94    /// Returns this key's stable category.
95    #[must_use]
96    pub const fn kind(&self) -> ConflictKeyKind {
97        match self {
98            Self::ResourceFileFacts(_) => ConflictKeyKind::ResourceFileFacts,
99            Self::LocatorSet(_) => ConflictKeyKind::LocatorSet,
100            Self::MetadataProperty { .. } => ConflictKeyKind::MetadataProperty,
101            Self::DependencySet(_) => ConflictKeyKind::DependencySet,
102            Self::MediaRoot(_) => ConflictKeyKind::MediaRoot,
103            Self::ExternalIdentifier { .. } => ConflictKeyKind::ExternalIdentifier,
104            Self::ResourceFingerprint { .. } => ConflictKeyKind::ResourceFingerprint,
105            Self::RepresentationFingerprint { .. } => ConflictKeyKind::RepresentationFingerprint,
106        }
107    }
108}
109
110/// Machine-readable details for one optimistic transaction conflict.
111#[derive(Clone, Debug, Eq, PartialEq)]
112pub struct TransactionConflict {
113    key: SemanticConflictKey,
114    base_revision: Option<RevisionId>,
115    base_sequence: u64,
116    superseding_revision: RevisionId,
117    superseding_sequence: u64,
118}
119
120impl TransactionConflict {
121    /// Creates conflict details from the compared revisions.
122    #[doc(hidden)]
123    #[must_use]
124    pub const fn new(
125        key: SemanticConflictKey,
126        base_revision: Option<RevisionId>,
127        base_sequence: u64,
128        superseding_revision: RevisionId,
129        superseding_sequence: u64,
130    ) -> Self {
131        Self {
132            key,
133            base_revision,
134            base_sequence,
135            superseding_revision,
136            superseding_sequence,
137        }
138    }
139
140    /// Returns the semantic fact that changed.
141    #[must_use]
142    pub const fn key(&self) -> &SemanticConflictKey {
143        &self.key
144    }
145
146    /// Returns the base revision, or `None` for a decision from an empty journal.
147    #[must_use]
148    pub const fn base_revision(&self) -> Option<RevisionId> {
149        self.base_revision
150    }
151
152    /// Returns the monotonic sequence of the supplied base revision.
153    #[must_use]
154    pub const fn base_sequence(&self) -> u64 {
155        self.base_sequence
156    }
157
158    /// Returns the revision that changed the semantic fact after the base.
159    #[must_use]
160    pub const fn superseding_revision(&self) -> RevisionId {
161        self.superseding_revision
162    }
163
164    /// Returns the superseding revision's monotonic sequence.
165    #[must_use]
166    pub const fn superseding_sequence(&self) -> u64 {
167        self.superseding_sequence
168    }
169}
170
171/// Concurrency policy assigned to a semantic revision event type.
172#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
173#[non_exhaustive]
174pub enum ConcurrencyClassification {
175    /// Independent changes may merge from a stale base.
176    Merge,
177    /// The staging operation records a semantic conflict key.
178    Conflict,
179    /// A more specific state, token, lease, uniqueness, or existence guard applies.
180    ExistingGuard,
181    /// The event covers operations with different policies.
182    OperationDependent,
183}