stow-types 0.4.1

Shared API and artifact-identity types for the stow artifact cache
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
//! Newtype wrappers for the five-element artifact identity carried over the
//! wire and persisted in D1.
//!
//! The canonical identity tuple is `(crate_name, version, features_json,
//! target, rustc_version)`. Historically each was a bare `String` on the wire
//! and the edge worker re-validated them per request via hand-rolled
//! `validate_*` helpers. These newtypes move the validation into
//! `serde::Deserialize`, so:
//!
//! * Every wire payload either parses successfully into a structured value or
//!   produces a precise deserialize error at the protocol boundary.
//! * Edge handlers receive types that are correct-by-construction.
//! * Adding or renaming a constraint becomes a compile-time edit, not a
//!   per-call-site audit.
//!
//! All wrappers serialize as the same primitive (string for most, JSON-encoded
//! string for [`FeaturesJson`] and [`DependencyCMetadataJson`]) so the wire
//! format is byte-for-byte identical to the previous stringly-typed shape.

use std::ffi::OsStr;
use std::fmt;
use std::path::Path;
use std::str::FromStr;

use serde::{Deserialize, Deserializer, Serialize, Serializer};

/// Errors produced while parsing wire identity values.
#[derive(Debug, thiserror::Error)]
pub enum IdentityError {
    /// The crate name violates crates.io's allowed character / length rules.
    #[error("invalid crate_name `{0}`: must be 1..=128 ASCII alphanumeric, `-` or `_`")]
    InvalidCrateName(String),
    /// The cargo `-C metadata` value is not a hex hash of length 1..=64.
    #[error("invalid c_metadata `{0}`: must be 1..=64 ASCII hex digits")]
    InvalidCMetadata(String),
    /// The target triple has unexpected characters or length.
    #[error("invalid target `{0}`: must be 1..=128 alphanumeric, `-` or `_`")]
    InvalidTarget(String),
    /// The crate version is not semver.
    #[error("invalid version `{0}`: must be a semver release like 1.0.219")]
    InvalidCrateVersion(String),
    /// The rustc version string failed shape validation.
    #[error("invalid rustc_version `{0}`: must be 1..=64 alphanumeric, `.`, `-`, or `_`")]
    InvalidRustcVersion(String),
    /// One feature name is empty, too long, or contains invalid characters.
    #[error("invalid feature name `{0}`: must be 1..=128 alphanumeric, `-`, `_`, or `.`")]
    InvalidFeatureName(String),
    /// A feature list was not strictly sorted and deduplicated.
    #[error("features must be strictly sorted and deduplicated")]
    UnsortedFeatures,
    /// An emit entry is empty, too long, or contains invalid characters.
    #[error("invalid emit entry `{0}`: must be 1..=32 alphanumeric, `-`, or `_`")]
    InvalidEmitEntry(String),
    /// An emit list was not strictly sorted and deduplicated.
    #[error("emit entries must be strictly sorted and deduplicated")]
    UnsortedEmit,
    /// Failed to parse a JSON wrapper string.
    #[error("invalid JSON wrapper: {0}")]
    InvalidJsonWrapper(String),
    /// Dependency identities not sorted by `(crate_name, c_metadata)`.
    #[error("dependency_c_metadata_json must be sorted by (crate_name, c_metadata)")]
    UnsortedDependencyIdentities,
}

fn validate_crate_name(value: &str) -> Result<(), IdentityError> {
    if value.is_empty()
        || value.len() > 128
        || !value
            .chars()
            .all(|ch| ch.is_ascii_alphanumeric() || ch == '-' || ch == '_')
    {
        return Err(IdentityError::InvalidCrateName(value.to_owned()));
    }
    Ok(())
}

fn validate_c_metadata(value: &str) -> Result<(), IdentityError> {
    if value.is_empty() || value.len() > 64 || !value.chars().all(|ch| ch.is_ascii_hexdigit()) {
        return Err(IdentityError::InvalidCMetadata(value.to_owned()));
    }
    Ok(())
}

fn validate_target(value: &str) -> Result<(), IdentityError> {
    if value.is_empty()
        || value.len() > 128
        || !value
            .chars()
            .all(|ch| ch.is_ascii_alphanumeric() || ch == '-' || ch == '_')
    {
        return Err(IdentityError::InvalidTarget(value.to_owned()));
    }
    Ok(())
}

fn validate_rustc_version(value: &str) -> Result<(), IdentityError> {
    if value.is_empty()
        || value.len() > 64
        || !value
            .chars()
            .all(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '.' | '-' | '_'))
    {
        return Err(IdentityError::InvalidRustcVersion(value.to_owned()));
    }
    Ok(())
}

fn validate_feature_name(value: &str) -> Result<(), IdentityError> {
    if value.is_empty()
        || value.len() > 128
        || !value
            .chars()
            .all(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '-' | '_' | '.'))
    {
        return Err(IdentityError::InvalidFeatureName(value.to_owned()));
    }
    Ok(())
}

fn validate_features_sorted(features: &[String]) -> Result<(), IdentityError> {
    let mut previous: Option<&str> = None;
    for feature in features {
        validate_feature_name(feature)?;
        if previous.is_some_and(|last| last >= feature.as_str()) {
            return Err(IdentityError::UnsortedFeatures);
        }
        previous = Some(feature.as_str());
    }
    Ok(())
}

fn validate_emit_entry(value: &str) -> Result<(), IdentityError> {
    if value.is_empty()
        || value.len() > 32
        || !value
            .chars()
            .all(|ch| ch.is_ascii_alphanumeric() || ch == '-' || ch == '_')
    {
        return Err(IdentityError::InvalidEmitEntry(value.to_owned()));
    }
    Ok(())
}

/// Validate that `emit` is strictly sorted, deduplicated, and each entry
/// matches the rustc emit-mode shape.
///
/// # Errors
/// Returns [`IdentityError::InvalidEmitEntry`] for a malformed entry, or
/// [`IdentityError::UnsortedEmit`] when the list is not strictly increasing.
pub fn validate_emit_sorted(emit: &[String]) -> Result<(), IdentityError> {
    let mut previous: Option<&str> = None;
    for entry in emit {
        validate_emit_entry(entry)?;
        if previous.is_some_and(|last| last >= entry.as_str()) {
            return Err(IdentityError::UnsortedEmit);
        }
        previous = Some(entry.as_str());
    }
    Ok(())
}

macro_rules! string_newtype {
    (
        $(#[$meta:meta])*
        $name:ident, $validate:ident, $error:ident
    ) => {
        $(#[$meta])*
        #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, utoipa::ToSchema)]
        pub struct $name(String);

        impl $name {
            /// Construct, validating the input.
            ///
            /// # Errors
            /// Returns the [`IdentityError`] variant for this newtype's
            /// validation rule when `value` violates it.
            pub fn parse<S: Into<String>>(value: S) -> Result<Self, IdentityError> {
                let value = value.into();
                $validate(&value)?;
                Ok(Self(value))
            }

            /// Borrow the underlying string.
            pub fn as_str(&self) -> &str {
                &self.0
            }

            /// Consume and return the inner `String`.
            pub fn into_inner(self) -> String {
                self.0
            }
        }

        impl AsRef<str> for $name {
            fn as_ref(&self) -> &str {
                &self.0
            }
        }

        impl AsRef<OsStr> for $name {
            fn as_ref(&self) -> &OsStr {
                self.0.as_ref()
            }
        }

        impl AsRef<Path> for $name {
            fn as_ref(&self) -> &Path {
                self.0.as_ref()
            }
        }

        impl PartialEq<str> for $name {
            fn eq(&self, other: &str) -> bool {
                self.0.as_str() == other
            }
        }

        impl PartialEq<&str> for $name {
            fn eq(&self, other: &&str) -> bool {
                self.0.as_str() == *other
            }
        }

        impl PartialEq<$name> for &str {
            fn eq(&self, other: &$name) -> bool {
                *self == other.0.as_str()
            }
        }

        impl PartialEq<$name> for str {
            fn eq(&self, other: &$name) -> bool {
                self == other.0.as_str()
            }
        }

        impl fmt::Display for $name {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.write_str(&self.0)
            }
        }

        impl FromStr for $name {
            type Err = IdentityError;
            fn from_str(value: &str) -> Result<Self, Self::Err> {
                Self::parse(value)
            }
        }

        impl Serialize for $name {
            fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
                self.0.serialize(s)
            }
        }

        impl<'de> Deserialize<'de> for $name {
            fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
                let raw = String::deserialize(d)?;
                Self::parse(raw).map_err(serde::de::Error::custom)
            }
        }
    };
}

string_newtype!(
    /// A crates.io crate name. ASCII alphanumeric plus `-` and `_`, 1..=128 chars.
    CrateName,
    validate_crate_name,
    InvalidCrateName
);
string_newtype!(
    /// Cargo `-C metadata` value: hex digits, 1..=64 chars.
    CMetadata,
    validate_c_metadata,
    InvalidCMetadata
);
string_newtype!(
    /// A compilation target triple (free-form, validated for shape only).
    TargetTriple,
    validate_target,
    InvalidTarget
);
string_newtype!(
    /// Wire form of a rustc version string (e.g. `"1.83.0"`). Shape only.
    WireRustcVersion,
    validate_rustc_version,
    InvalidRustcVersion
);

/// A semver crate version (no shape constraints beyond what semver requires).
///
/// We delegate validation to the `semver` crate but still expose this newtype
/// so call sites can switch wire types without hopping back into raw strings.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize)]
#[serde(transparent)]
pub struct CrateVersion(pub semver::Version);

// Hand-written so a malformed version answers with the shape it wanted
// rather than semver's parser position ("unexpected character 's' while
// parsing major version number") — the request form shows the deserialize
// error to whoever typed it.
impl<'de> Deserialize<'de> for CrateVersion {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let raw = String::deserialize(deserializer)?;
        raw.parse().map_err(serde::de::Error::custom)
    }
}

// The wire shape is a string; `semver::Version` has no `PartialSchema` impl.
impl utoipa::PartialSchema for CrateVersion {
    fn schema() -> utoipa::openapi::RefOr<utoipa::openapi::schema::Schema> {
        <String as utoipa::PartialSchema>::schema()
    }
}

impl utoipa::ToSchema for CrateVersion {}

impl CrateVersion {
    /// Construct from the embedded semver value.
    #[must_use]
    pub const fn new(version: semver::Version) -> Self {
        Self(version)
    }

    /// Borrow the underlying semver value.
    #[must_use]
    pub const fn as_semver(&self) -> &semver::Version {
        &self.0
    }

    /// Consume and return the inner `semver::Version`.
    #[must_use]
    pub fn into_inner(self) -> semver::Version {
        self.0
    }
}

impl fmt::Display for CrateVersion {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl FromStr for CrateVersion {
    type Err = IdentityError;
    fn from_str(value: &str) -> Result<Self, Self::Err> {
        semver::Version::parse(value)
            .map(Self)
            .map_err(|_| IdentityError::InvalidCrateVersion(value.to_owned()))
    }
}

/// A canonical features list: strictly sorted, deduplicated.
///
/// On the wire this serializes as a JSON-encoded string (e.g.
/// `"[\"default\",\"std\"]"`) so it is byte-compatible with the legacy
/// `features_json: String` shape used by `BuildTaskPayload` and friends.
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct FeaturesJson(Vec<String>);

// The wire shape is a JSON-encoded string, not an array.
impl utoipa::PartialSchema for FeaturesJson {
    fn schema() -> utoipa::openapi::RefOr<utoipa::openapi::schema::Schema> {
        <String as utoipa::PartialSchema>::schema()
    }
}

impl utoipa::ToSchema for FeaturesJson {}

impl FeaturesJson {
    /// Construct from an already-sorted, deduplicated list of features.
    ///
    /// # Errors
    /// Returns [`IdentityError::InvalidFeatureName`] for a malformed feature,
    /// or [`IdentityError::UnsortedFeatures`] when the list violates the
    /// sorted/deduplicated invariant.
    pub fn from_sorted(features: Vec<String>) -> Result<Self, IdentityError> {
        validate_features_sorted(&features)?;
        Ok(Self(features))
    }

    /// Sort, deduplicate, and validate a raw feature list, then construct.
    ///
    /// # Errors
    /// Returns [`IdentityError::InvalidFeatureName`] when a feature name is
    /// malformed.
    pub fn canonicalize(mut features: Vec<String>) -> Result<Self, IdentityError> {
        features.sort();
        features.dedup();
        Self::from_sorted(features)
    }

    /// Borrow the canonical features.
    #[must_use]
    pub fn features(&self) -> &[String] {
        &self.0
    }

    /// Render the canonical JSON-encoded string used in D1 column storage.
    ///
    /// # Panics
    /// Panics only if `serde_json` fails to serialize a `Vec<String>`, which
    /// cannot happen.
    #[must_use]
    pub fn raw(&self) -> String {
        serde_json::to_string(&self.0).expect("Vec<String> always serializes")
    }
}

impl fmt::Display for FeaturesJson {
    /// Display as the canonical JSON-encoded array string.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.raw())
    }
}

impl Serialize for FeaturesJson {
    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        self.raw().serialize(s)
    }
}

impl<'de> Deserialize<'de> for FeaturesJson {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let raw = String::deserialize(d)?;
        let features: Vec<String> = serde_json::from_str(&raw).map_err(|error| {
            serde::de::Error::custom(IdentityError::InvalidJsonWrapper(error.to_string()))
        })?;
        Self::from_sorted(features).map_err(serde::de::Error::custom)
    }
}

/// One entry in `dependency_compile_keys_json`: a dependency crate and the
/// full compile key of the artifact this build resolved it to.
///
/// Producers must emit entries sorted by `(crate_name, compile_key)` and
/// deduplicated; [`Self::canonicalize_list`] does exactly that.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct DependencyCompileKeyIdentity {
    /// Crate name as known to crates.io.
    pub crate_name: CrateName,
    /// Blake3 compile key of the dependency artifact.
    pub compile_key: String,
}

impl DependencyCompileKeyIdentity {
    /// Sort and deduplicate a list of identities into canonical order.
    #[must_use]
    pub fn canonicalize_list(mut identities: Vec<Self>) -> Vec<Self> {
        identities.sort();
        identities.dedup();
        identities
    }
}

/// One entry in `dependency_c_metadata_json`: `(crate_name, c_metadata)`
/// identifying a dependency artifact already produced by stow.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct DependencyCMetadataIdentity {
    /// Crate name as known to crates.io.
    pub crate_name: CrateName,
    /// Cargo `-C metadata` value of the dependency artifact.
    pub c_metadata: CMetadata,
}

/// A canonical dependency identity list: strictly sorted by
/// `(crate_name, c_metadata)`, deduplicated.
///
/// On the wire this serializes as a JSON-encoded string (matching the legacy
/// `dependency_c_metadata_json: String` field shape).
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct DependencyCMetadataJson(Vec<DependencyCMetadataIdentity>);

// The wire shape is a JSON-encoded string, not an array.
impl utoipa::PartialSchema for DependencyCMetadataJson {
    fn schema() -> utoipa::openapi::RefOr<utoipa::openapi::schema::Schema> {
        <String as utoipa::PartialSchema>::schema()
    }
}

impl utoipa::ToSchema for DependencyCMetadataJson {}

impl DependencyCMetadataJson {
    /// Construct from an already-sorted, deduplicated list.
    ///
    /// # Errors
    /// Returns [`IdentityError::UnsortedDependencyIdentities`] when the list
    /// is not strictly increasing by `(crate_name, c_metadata)`.
    pub fn from_sorted(
        identities: Vec<DependencyCMetadataIdentity>,
    ) -> Result<Self, IdentityError> {
        let mut previous: Option<(&str, &str)> = None;
        for identity in &identities {
            let current = (identity.crate_name.as_str(), identity.c_metadata.as_str());
            if previous.is_some_and(|last| last >= current) {
                return Err(IdentityError::UnsortedDependencyIdentities);
            }
            previous = Some(current);
        }
        Ok(Self(identities))
    }

    /// Sort, deduplicate, then construct.
    ///
    /// # Errors
    /// Never fails after sorting and deduplication; the `Result` shape
    /// mirrors [`Self::from_sorted`].
    pub fn canonicalize(
        mut identities: Vec<DependencyCMetadataIdentity>,
    ) -> Result<Self, IdentityError> {
        identities.sort();
        identities.dedup();
        Self::from_sorted(identities)
    }

    /// Borrow the canonical identities.
    #[must_use]
    pub fn entries(&self) -> &[DependencyCMetadataIdentity] {
        &self.0
    }

    /// Render the canonical JSON-encoded string used in D1 column storage.
    ///
    /// # Panics
    /// Panics only if `serde_json` fails to serialize the identity list,
    /// which cannot happen.
    #[must_use]
    pub fn raw(&self) -> String {
        serde_json::to_string(&self.0).expect("dependency identity list always serializes")
    }
}

impl fmt::Display for DependencyCMetadataJson {
    /// Display as the canonical JSON-encoded array string.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.raw())
    }
}

impl Serialize for DependencyCMetadataJson {
    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        self.raw().serialize(s)
    }
}

impl<'de> Deserialize<'de> for DependencyCMetadataJson {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let raw = String::deserialize(d)?;
        let entries: Vec<DependencyCMetadataIdentity> =
            serde_json::from_str(&raw).map_err(|error| {
                serde::de::Error::custom(IdentityError::InvalidJsonWrapper(error.to_string()))
            })?;
        Self::from_sorted(entries).map_err(serde::de::Error::custom)
    }
}

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

    #[test]
    fn crate_name_accepts_typical() {
        CrateName::parse("serde_json").unwrap();
        CrateName::parse("proc-macro2").unwrap();
    }

    #[test]
    fn crate_name_rejects_empty_and_overlong() {
        assert!(CrateName::parse("").is_err());
        assert!(CrateName::parse("x".repeat(129)).is_err());
        assert!(CrateName::parse("a b").is_err());
    }

    #[test]
    fn c_metadata_must_be_hex() {
        CMetadata::parse("deadbeef").unwrap();
        assert!(CMetadata::parse("xyz").is_err());
    }

    #[test]
    fn rustc_version_shape() {
        WireRustcVersion::parse("1.83.0").unwrap();
        WireRustcVersion::parse("1.91.0-nightly").unwrap();
        assert!(WireRustcVersion::parse("").is_err());
    }

    #[test]
    fn features_json_round_trip() {
        let raw = "[\"default\",\"std\"]";
        let features: FeaturesJson =
            serde_json::from_value(serde_json::Value::String(raw.to_owned())).unwrap();
        assert_eq!(features.features(), &["default", "std"]);
        let serialized = serde_json::to_value(&features).unwrap();
        assert_eq!(serialized, serde_json::Value::String(raw.to_owned()));
    }

    #[test]
    fn features_json_rejects_unsorted() {
        let raw = "[\"std\",\"default\"]";
        let result: Result<FeaturesJson, _> =
            serde_json::from_value(serde_json::Value::String(raw.to_owned()));
        assert!(result.is_err());
    }

    #[test]
    fn dependency_identities_round_trip() {
        let raw = "[{\"crate_name\":\"a\",\"c_metadata\":\"deadbeef\"}]";
        let value: DependencyCMetadataJson =
            serde_json::from_value(serde_json::Value::String(raw.to_owned())).unwrap();
        assert_eq!(value.entries().len(), 1);
        let serialized = serde_json::to_value(&value).unwrap();
        assert_eq!(serialized, serde_json::Value::String(raw.to_owned()));
    }
}