nizaam-core 1.0.1

Domain-agnostic Rust infrastructure core for building secure, observable, reliable, and extensible Nizaam engines.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! One exact immutable logical content state of an artifact.
//!
//! An `ArtifactVersion` represents one exact logical content state of an
//! artifact. It contains the artifact identity, version identity, content
//! reference, integrity information, content size, descriptive metadata,
//! and lifecycle state.
//!
//! Published versions are immutable with respect to their content,
//! content identity, version identity, and integrity information.
//! Persistence uses [`ArtifactVersionRecord`] so serialized state cannot
//! directly construct an executable `ArtifactVersion`.

use crate::identity::ArtifactId;

use super::content::ContentReference;
use super::integrity::ContentDigest;
use super::lifecycle::{LifecycleState, transition};

/// Persisted representation of an artifact version.
///
/// This type is intentionally separate from [`ArtifactVersion`]. It may be
/// serialized and deserialized, but deserialized data must pass through
/// [`ArtifactVersion::restore`] before becoming a live artifact version.
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ArtifactVersionRecord {
    artifact_id: ArtifactId,
    version: String,
    content: ContentReference,
    digest: ContentDigest,
    size: u64,
    metadata: Vec<(String, String)>,
    lifecycle: LifecycleState,
}

/// Failure while restoring a persisted artifact version.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ArtifactVersionRestoreError {
    /// The persisted content reference is structurally invalid.
    InvalidContentReference,

    /// The persisted version identifier is empty or whitespace-only.
    InvalidVersion,

    /// The persisted lifecycle state could not be reached through valid
    /// lifecycle transitions.
    InvalidLifecycleTransition {
        from: LifecycleState,
        to: LifecycleState,
    },
}

impl std::fmt::Display for ArtifactVersionRestoreError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InvalidContentReference => {
                write!(formatter, "artifact content reference is invalid")
            }
            Self::InvalidVersion => write!(formatter, "artifact version is invalid"),
            Self::InvalidLifecycleTransition { from, to } => write!(
                formatter,
                "invalid artifact lifecycle transition from {:?} to {:?} during restoration",
                from, to
            ),
        }
    }
}

impl std::error::Error for ArtifactVersionRestoreError {}

impl ArtifactVersionRecord {
    /// Returns the persisted artifact identity.
    pub fn artifact_id(&self) -> &ArtifactId {
        &self.artifact_id
    }

    /// Returns the persisted version identity.
    pub fn version(&self) -> &str {
        &self.version
    }

    /// Returns the persisted content reference.
    pub fn content(&self) -> &ContentReference {
        &self.content
    }

    /// Returns the persisted content digest.
    pub fn digest(&self) -> &ContentDigest {
        &self.digest
    }

    /// Returns the persisted content size.
    pub fn size(&self) -> u64 {
        self.size
    }

    /// Returns persisted descriptive metadata.
    pub fn metadata(&self) -> &[(String, String)] {
        &self.metadata
    }

    /// Returns the persisted lifecycle state.
    pub fn lifecycle(&self) -> LifecycleState {
        self.lifecycle
    }
}

/// One exact logical content state of an artifact.
///
/// The artifact identity remains stable across versions, while each
/// `ArtifactVersion` identifies one exact immutable logical state.
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)]
pub struct ArtifactVersion {
    artifact_id: ArtifactId,
    version: String,
    content: ContentReference,
    digest: ContentDigest,
    size: u64,
    metadata: Vec<(String, String)>,
    lifecycle: LifecycleState,
}

impl ArtifactVersion {
    /// Creates a new artifact version in the `Created` lifecycle state.
    ///
    /// The supplied `ArtifactId` must already be a valid Core identity.
    /// Version ordering is intentionally not interpreted by this type.
    pub fn new(
        artifact_id: ArtifactId,
        version: impl Into<String>,
        content: ContentReference,
        digest: ContentDigest,
        size: u64,
    ) -> Self {
        Self {
            artifact_id,
            version: version.into(),
            content,
            digest,
            size,
            metadata: Vec::new(),
            lifecycle: LifecycleState::Created,
        }
    }

    /// Restores a live artifact version from trusted persisted data.
    ///
    /// Restoration validates the content reference and reconstructs the
    /// persisted lifecycle only through the approved lifecycle transitions.
    /// In particular, entering `Published` always goes through
    /// `Validated → Published`; persisted data cannot assign that state
    /// directly through serde deserialization.
    pub fn restore(record: ArtifactVersionRecord) -> Result<Self, ArtifactVersionRestoreError> {
        if record.version.trim().is_empty() {
            return Err(ArtifactVersionRestoreError::InvalidVersion);
        }

        if !record.content.is_valid() {
            return Err(ArtifactVersionRestoreError::InvalidContentReference);
        }

        let mut version = Self::new(
            record.artifact_id,
            record.version,
            record.content,
            record.digest,
            record.size,
        );
        version.metadata = record.metadata;

        restore_lifecycle(&mut version, record.lifecycle)?;
        Ok(version)
    }

    /// Converts this version into its persistence representation.
    pub fn to_record(&self) -> ArtifactVersionRecord {
        ArtifactVersionRecord {
            artifact_id: self.artifact_id.clone(),
            version: self.version.clone(),
            content: self.content.clone(),
            digest: self.digest.clone(),
            size: self.size,
            metadata: self.metadata.clone(),
            lifecycle: self.lifecycle,
        }
    }

    /// Returns the stable logical artifact identifier.
    pub fn artifact_id(&self) -> &ArtifactId {
        &self.artifact_id
    }

    /// Returns the version identifier.
    ///
    /// The version identifier is an identity value only. `ArtifactVersion`
    /// does not assign ordering or semantic-version meaning to it.
    pub fn version(&self) -> &str {
        &self.version
    }

    /// Returns the physical content reference.
    pub fn content(&self) -> &ContentReference {
        &self.content
    }

    /// Returns the recorded content digest.
    pub fn digest(&self) -> &ContentDigest {
        &self.digest
    }

    /// Returns the recorded content size in bytes.
    pub fn size(&self) -> u64 {
        self.size
    }

    /// Returns descriptive artifact metadata.
    ///
    /// This metadata is not part of the content identity represented by the
    /// recorded digest.
    pub fn metadata(&self) -> &[(String, String)] {
        &self.metadata
    }

    /// Returns the current lifecycle state.
    pub fn lifecycle(&self) -> &LifecycleState {
        &self.lifecycle
    }

    /// Adds descriptive metadata without changing the content identity.
    ///
    /// Metadata can be changed only before publication. Once a version is
    /// `Published`, `Superseded`, `Archived`, or `Revoked`, the metadata is
    /// part of the preserved historical state and cannot be mutated.
    pub fn with_metadata(
        mut self,
        key: impl Into<String>,
        value: impl Into<String>,
    ) -> Result<Self, ArtifactVersionError> {
        match self.lifecycle {
            LifecycleState::Created | LifecycleState::Validating | LifecycleState::Validated => {
                self.metadata.push((key.into(), value.into()));
                Ok(self)
            }
            state => Err(ArtifactVersionError::MetadataMutationNotAllowed { state }),
        }
    }

    /// Updates the lifecycle state internally within the artifact module.
    ///
    /// Lifecycle validity must be enforced by the lifecycle mechanism rather
    /// than by arbitrary external callers. This method is therefore crate
    /// visible and is not part of the public artifact API.
    pub(crate) fn set_lifecycle(&mut self, state: LifecycleState) {
        self.lifecycle = state;
    }
}

/// Errors produced by invalid artifact-version mutations.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ArtifactVersionError {
    /// Metadata cannot be mutated after the version enters a preserved state.
    MetadataMutationNotAllowed { state: LifecycleState },
}

impl std::fmt::Display for ArtifactVersionError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::MetadataMutationNotAllowed { state } => write!(
                formatter,
                "artifact version metadata cannot be changed in {:?} state",
                state
            ),
        }
    }
}

impl std::error::Error for ArtifactVersionError {}

fn restore_lifecycle(
    version: &mut ArtifactVersion,
    target: LifecycleState,
) -> Result<(), ArtifactVersionRestoreError> {
    let transitions = match target {
        LifecycleState::Created => &[][..],
        LifecycleState::Validating => &[LifecycleState::Validating][..],
        LifecycleState::Validated => &[LifecycleState::Validating, LifecycleState::Validated][..],
        LifecycleState::Published => &[
            LifecycleState::Validating,
            LifecycleState::Validated,
            LifecycleState::Published,
        ][..],
        LifecycleState::Superseded => &[
            LifecycleState::Validating,
            LifecycleState::Validated,
            LifecycleState::Published,
            LifecycleState::Superseded,
        ][..],
        LifecycleState::Archived => &[
            LifecycleState::Validating,
            LifecycleState::Validated,
            LifecycleState::Published,
            LifecycleState::Superseded,
            LifecycleState::Archived,
        ][..],
        LifecycleState::Revoked => &[
            LifecycleState::Validating,
            LifecycleState::Validated,
            LifecycleState::Published,
            LifecycleState::Revoked,
        ][..],
    };

    for next in transitions {
        let current = *version.lifecycle();
        let state = transition(current, *next).map_err(|error| {
            ArtifactVersionRestoreError::InvalidLifecycleTransition {
                from: error.from(),
                to: error.to(),
            }
        })?;
        version.set_lifecycle(state);
    }

    Ok(())
}

impl From<&ArtifactVersion> for ArtifactVersionRecord {
    fn from(version: &ArtifactVersion) -> Self {
        version.to_record()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::identity::ArtifactId;

    fn sample_version() -> ArtifactVersion {
        ArtifactVersion::new(
            ArtifactId::new("test-artifact").unwrap(),
            "v1",
            ContentReference::new("provider", "location-1"),
            ContentDigest::new(b"digest-data"),
            1024,
        )
    }

    #[test]
    fn artifact_version_contains_identity_and_content() {
        let version = sample_version();

        assert_eq!(version.artifact_id().as_str(), "test-artifact");
        assert_eq!(version.version(), "v1");
        assert_eq!(version.size(), 1024);
    }

    #[test]
    fn artifact_version_starts_in_created_state() {
        let version = sample_version();

        assert_eq!(version.lifecycle(), &LifecycleState::Created);
    }

    #[test]
    fn artifact_version_metadata_is_descriptive_only() {
        let version = sample_version().with_metadata("author", "test").unwrap();

        assert_eq!(version.metadata().len(), 1);
        assert_eq!(
            version.metadata()[0],
            ("author".to_string(), "test".to_string())
        );
    }

    #[test]
    fn artifact_version_metadata_rejects_preserved_lifecycle_states() {
        for state in [
            LifecycleState::Published,
            LifecycleState::Superseded,
            LifecycleState::Archived,
            LifecycleState::Revoked,
        ] {
            let mut version = sample_version();
            version.set_lifecycle(state);

            assert_eq!(
                version.with_metadata("author", "test"),
                Err(ArtifactVersionError::MetadataMutationNotAllowed { state })
            );
        }
    }

    #[test]
    fn artifact_version_lifecycle_can_be_updated_within_artifact_module() {
        let mut version = sample_version();

        version.set_lifecycle(LifecycleState::Validated);

        assert_eq!(version.lifecycle(), &LifecycleState::Validated);
    }

    #[test]
    fn artifact_version_supports_clone_and_eq() {
        let version = sample_version();
        let cloned = version.clone();

        assert_eq!(version, cloned);
    }

    #[test]
    fn persistence_record_round_trips_through_trusted_restoration() {
        let mut original = sample_version().with_metadata("author", "test").unwrap();
        original.set_lifecycle(LifecycleState::Validated);
        original.set_lifecycle(LifecycleState::Published);

        let record = original.to_record();
        let serialized = serde_json::to_string(&record).unwrap();
        let deserialized = serde_json::from_str::<ArtifactVersionRecord>(&serialized).unwrap();
        let restored = ArtifactVersion::restore(deserialized).unwrap();

        assert_eq!(restored, original);
    }

    #[test]
    fn restoration_rejects_invalid_version() {
        let mut record = sample_version().to_record();
        record.version = "   ".to_string();

        assert_eq!(
            ArtifactVersion::restore(record),
            Err(ArtifactVersionRestoreError::InvalidVersion)
        );
    }

    #[test]
    fn restoration_rejects_invalid_content_reference() {
        let mut record = sample_version().to_record();
        record.content = ContentReference::new("", "");

        assert_eq!(
            ArtifactVersion::restore(record),
            Err(ArtifactVersionRestoreError::InvalidContentReference)
        );
    }
}