libtea-core 0.1.8

Independent Rust types and protocol rules for the CycloneDX Transparency Exchange API. No network I/O.
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
//! Transparency Exchange Identifier parsing and type-specific validation.
//!
//! Parsing accepts a structurally valid TEI and retains the original spelling.
//! Registry checks are separate and do not rewrite the identifier. Equality is
//! exact string equality, as required by the discovery document.

use std::fmt;

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

use crate::enums::ChecksumAlgorithm;
use crate::error::TeiError;
use crate::ids::TeaUuid;

const SCHEME: &str = "tei://";

/// A parsed TEI that still carries its original character sequence.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Tei {
    original: String,
    domain: String,
    tei_type: String,
    identifier: String,
}

impl Tei {
    /// Parse `tei://<domain-name>/<type>/<unique-identifier>`.
    ///
    /// The returned value displays as the input. This function does not
    /// lowercase the domain, decode Base64URL, or rewrite percent-escapes.
    ///
    /// # Errors
    ///
    /// Returns an error when the text is not a structurally valid TEI.
    pub fn parse(input: &str) -> Result<Self, TeiError> {
        if input.is_empty() {
            return Err(TeiError::Empty);
        }
        if input
            .get(..8)
            .is_some_and(|prefix| prefix.eq_ignore_ascii_case("urn:tei:"))
        {
            return Err(TeiError::LegacyUrn);
        }
        let rest = input.strip_prefix(SCHEME).ok_or(TeiError::Scheme)?;
        if rest.contains(['?', '#', ' ']) {
            return Err(TeiError::Structure);
        }
        let mut parts = rest.split('/');
        let domain = parts.next().ok_or(TeiError::Structure)?;
        let tei_type = parts.next().ok_or(TeiError::Structure)?;
        let identifier = parts.next().ok_or(TeiError::Structure)?;
        if parts.next().is_some()
            || domain.is_empty()
            || tei_type.is_empty()
            || identifier.is_empty()
        {
            return Err(TeiError::Structure);
        }
        validate_domain(domain)?;
        validate_type(tei_type)?;
        validate_identifier_segment(identifier)?;
        Ok(Self {
            original: input.to_owned(),
            domain: domain.to_owned(),
            tei_type: tei_type.to_owned(),
            identifier: identifier.to_owned(),
        })
    }

    /// Original spelling, used for equality and for the `tei` query parameter.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.original
    }

    /// DNS name carried by the TEI. This is not normalized.
    #[must_use]
    pub fn domain(&self) -> &str {
        &self.domain
    }

    /// Registered type segment.
    #[must_use]
    pub fn tei_type(&self) -> &str {
        &self.tei_type
    }

    /// Unique-identifier segment, still in its TEI spelling.
    #[must_use]
    pub fn identifier(&self) -> &str {
        &self.identifier
    }

    /// `https://<domain>/.well-known/tea`, using the domain spelling as written.
    #[must_use]
    pub fn well_known_url(&self) -> String {
        format!("https://{}/.well-known/tea", self.domain)
    }

    /// The Package URL inside a `purl` TEI, or `None` for any other TEI type.
    ///
    /// **Experimental:** may change in a 0.x minor release.
    ///
    /// The identifier is decoded from unpadded Base64URL and parsed with
    /// [`crate::purl::Purl::parse`]. The TEI itself is not changed: a TEI is
    /// used exactly as received, whatever the canonical form of its PURL.
    ///
    /// ```
    /// use libtea_core::Tei;
    ///
    /// let tei = Tei::parse("tei://example.com/purl/cGtnOm1hdmVuL2NvbW1vbnMtaW8vY29tbW9ucy1pb0AyLjIyLjA").unwrap();
    /// let purl = tei.purl().unwrap().unwrap();
    /// assert_eq!(purl.as_str(), "pkg:maven/commons-io/commons-io@2.22.0");
    /// assert_eq!(Tei::parse("tei://example.com/uuid/2ff3ae7c-8eab-4e0b-9b8b-1a0c4c4c1c2d").unwrap().purl(), None);
    /// ```
    #[must_use]
    pub fn purl(&self) -> Option<Result<crate::purl::Purl, crate::purl::PurlError>> {
        if self.tei_type != "purl" {
            return None;
        }
        let decoded = if self.identifier.contains('=') {
            None
        } else {
            base64::engine::general_purpose::URL_SAFE_NO_PAD
                .decode(&self.identifier)
                .ok()
                .and_then(|bytes| String::from_utf8(bytes).ok())
        };
        Some(match decoded {
            Some(text) => crate::purl::Purl::parse(&text),
            None => Err(crate::purl::PurlError::TeiIdentifier),
        })
    }

    /// Check the unique identifier against the TEI type registry at the pinned commit.
    ///
    /// Unknown types are structurally valid and return `Ok(())`. Their
    /// identifiers are not given extra syntax beyond the single path segment.
    ///
    /// # Errors
    ///
    /// Returns an error when a registered type's identifier syntax is not met.
    pub fn validate_registry(&self) -> Result<(), TeiError> {
        match self.tei_type.as_str() {
            "purl" => validate_purl(&self.identifier),
            "hash" => validate_hash(&self.identifier),
            "uuid" => validate_uuid_id(&self.identifier),
            "eanupc" => validate_eanupc(&self.identifier),
            "gtin" => validate_gtin(&self.identifier),
            "asin" => validate_asin(&self.identifier),
            "udi" => validate_udi(&self.identifier),
            _ => Ok(()),
        }
    }

    /// Publication-form issues that do not make the TEI unparsable.
    ///
    /// The discovery document requires vendors to publish a canonical spelling
    /// and requires clients not to rewrite a TEI they received. These issues
    /// are reported separately from [`Self::parse`].
    #[must_use]
    pub fn publication_issues(&self) -> Vec<String> {
        let mut issues = Vec::new();
        if self.domain.chars().any(|c| c.is_ascii_uppercase()) {
            issues.push("domain name is not lowercase ASCII".into());
        }
        if self.identifier.contains('%') && !uppercase_percent_hex(&self.identifier) {
            issues.push("percent-escapes are not uppercase hexadecimal".into());
        }
        issues
    }
}

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

impl Serialize for Tei {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(&self.original)
    }
}

impl<'de> Deserialize<'de> for Tei {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let value = String::deserialize(deserializer)?;
        Self::parse(&value).map_err(serde::de::Error::custom)
    }
}

fn validate_domain(domain: &str) -> Result<(), TeiError> {
    if domain.len() > 253
        || domain.starts_with('.')
        || domain.ends_with('.')
        || domain.contains(':')
        || domain.contains('@')
    {
        return Err(TeiError::Domain);
    }
    let labels: Vec<&str> = domain.split('.').collect();
    if labels.iter().any(|label| !valid_dns_label(label)) {
        return Err(TeiError::Domain);
    }
    Ok(())
}

fn valid_dns_label(label: &str) -> bool {
    let bytes = label.as_bytes();
    if bytes.is_empty() || bytes.len() > 63 {
        return false;
    }
    if bytes[0] == b'-' || bytes[bytes.len() - 1] == b'-' {
        return false;
    }
    bytes
        .iter()
        .all(|b| b.is_ascii_alphanumeric() || *b == b'-')
}

fn validate_type(tei_type: &str) -> Result<(), TeiError> {
    let mut chars = tei_type.chars();
    let Some(first) = chars.next() else {
        return Err(TeiError::Type);
    };
    if !first.is_ascii_lowercase() {
        return Err(TeiError::Type);
    }
    if chars.any(|c| !c.is_ascii_lowercase() && !c.is_ascii_digit()) {
        return Err(TeiError::Type);
    }
    Ok(())
}

fn validate_identifier_segment(identifier: &str) -> Result<(), TeiError> {
    let bytes = identifier.as_bytes();
    let mut index = 0;
    while index < bytes.len() {
        if bytes[index] == b'/' {
            return Err(TeiError::Identifier);
        }
        if bytes[index] == b'%' {
            if index + 2 >= bytes.len() {
                return Err(TeiError::PercentEncoding);
            }
            // Compare bytes, not a `str` slice: a multi-byte character after
            // `%` would put `index + 3` inside a character and panic.
            let (high, low) = (bytes[index + 1], bytes[index + 2]);
            if !high.is_ascii_hexdigit() || !low.is_ascii_hexdigit() {
                return Err(TeiError::PercentEncoding);
            }
            if high == b'2' && low.eq_ignore_ascii_case(&b'f') {
                return Err(TeiError::Identifier);
            }
            index += 3;
            continue;
        }
        if bytes[index].is_ascii_control() || bytes[index] == b' ' {
            return Err(TeiError::Identifier);
        }
        index += 1;
    }
    Ok(())
}

fn uppercase_percent_hex(identifier: &str) -> bool {
    let bytes = identifier.as_bytes();
    let mut index = 0;
    while index < bytes.len() {
        if bytes[index] == b'%' && index + 2 < bytes.len() {
            if bytes[index + 1..index + 3]
                .iter()
                .any(u8::is_ascii_lowercase)
            {
                return false;
            }
            index += 3;
        } else {
            index += 1;
        }
    }
    true
}

fn validate_purl(identifier: &str) -> Result<(), TeiError> {
    if identifier.contains('=')
        || !identifier
            .bytes()
            .all(|b| matches!(b, b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_'))
    {
        return Err(registry("purl", "must be unpadded Base64URL"));
    }
    let decoded = base64::engine::general_purpose::URL_SAFE_NO_PAD
        .decode(identifier)
        .map_err(|_| registry("purl", "is not valid Base64URL"))?;
    let text =
        std::str::from_utf8(&decoded).map_err(|_| registry("purl", "does not decode to UTF-8"))?;
    if !text.starts_with("pkg:") || text.is_empty() {
        return Err(registry("purl", "does not decode to a Package URL"));
    }
    Ok(())
}

fn validate_hash(identifier: &str) -> Result<(), TeiError> {
    let Some((alg, hex_value)) = identifier.split_once(':') else {
        return Err(registry("hash", "must be `<hashtype>:<hex>`"));
    };
    let algorithm = ChecksumAlgorithm::from_wire(alg);
    let Some(len) = algorithm.digest_len() else {
        return Err(registry(
            "hash",
            "hashtype must be a checksum-type value such as SHA-256",
        ));
    };
    if hex_value.len() != len * 2
        || !hex_value.bytes().all(|b| b.is_ascii_hexdigit())
        || hex_value.bytes().any(|b| b.is_ascii_uppercase())
    {
        return Err(registry(
            "hash",
            "digest must be lowercase hexadecimal of the algorithm length",
        ));
    }
    Ok(())
}

fn validate_uuid_id(identifier: &str) -> Result<(), TeiError> {
    TeaUuid::parse(identifier)
        .map(|_| ())
        .map_err(|_| registry("uuid", "must be the lowercase hyphenated RFC 9562 form"))
}

fn validate_eanupc(identifier: &str) -> Result<(), TeiError> {
    if !matches!(identifier.len(), 8 | 12 | 13) {
        return Err(registry(
            "eanupc",
            "must be 8, 12, or 13 digits with its check digit",
        ));
    }
    gs1(identifier, "eanupc")
}

fn validate_gtin(identifier: &str) -> Result<(), TeiError> {
    if !matches!(identifier.len(), 8 | 12 | 13 | 14) {
        return Err(registry(
            "gtin",
            "must be a GTIN-8, GTIN-12, GTIN-13, or GTIN-14",
        ));
    }
    gs1(identifier, "gtin")
}

fn validate_asin(identifier: &str) -> Result<(), TeiError> {
    if identifier.len() != 10
        || !identifier
            .bytes()
            .all(|b| matches!(b, b'A'..=b'Z' | b'0'..=b'9'))
    {
        return Err(registry("asin", "must be 10 uppercase letters or digits"));
    }
    Ok(())
}

fn validate_udi(identifier: &str) -> Result<(), TeiError> {
    if identifier.is_empty() {
        return Err(registry("udi", "device identifier must not be empty"));
    }
    if identifier.starts_with("(01)") || identifier.starts_with('+') {
        return Err(registry(
            "udi",
            "must not include an application identifier or data identifier prefix",
        ));
    }
    Ok(())
}

fn gs1(identifier: &str, tei_type: &str) -> Result<(), TeiError> {
    if !identifier.bytes().all(|b| b.is_ascii_digit()) {
        return Err(registry(tei_type, "must be digits"));
    }
    let digits: Vec<u32> = identifier.bytes().map(|b| u32::from(b - b'0')).collect();
    let check = digits[digits.len() - 1];
    let mut sum = 0u32;
    for (index, digit) in digits[..digits.len() - 1].iter().rev().enumerate() {
        let weight = if index % 2 == 0 { 3 } else { 1 };
        sum += digit * weight;
    }
    let calculated = (10 - (sum % 10)) % 10;
    if calculated != check {
        return Err(registry(tei_type, "check digit is not valid"));
    }
    Ok(())
}

fn registry(tei_type: &str, reason: &str) -> TeiError {
    TeiError::Registry {
        tei_type: tei_type.to_owned(),
        reason: reason.to_owned(),
    }
}

/// Percent-encode a query component using uppercase hex, leaving unreserved characters.
#[must_use]
pub fn encode_query_component(value: &str) -> String {
    let mut out = String::with_capacity(value.len());
    for byte in value.bytes() {
        match byte {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' => {
                out.push(char::from(byte));
            }
            _ => out.push_str(&format!("%{byte:02X}")),
        }
    }
    out
}

#[cfg(test)]
mod tests {
    #[test]
    fn non_ascii_after_percent_is_an_error_not_a_panic() {
        for text in [
            "tei://example.com/uuid/%a\u{e9}",
            "tei://example.com/purl/x%a\u{e9}",
            "tei://example.com/purl/%\u{e9}a",
            "tei://example.com/hash/%4\u{1f600}",
            "tei://example.com/x/%\u{e9}",
        ] {
            assert!(Tei::parse(text).is_err(), "{text}");
        }
        // A valid escape followed by non-ASCII text is still checked byte-wise.
        let tei = Tei::parse("tei://example.com/x/%41\u{e9}").unwrap();
        assert!(tei.publication_issues().is_empty());
        let lower = Tei::parse("tei://example.com/x/%4a\u{e9}").unwrap();
        assert_eq!(lower.publication_issues().len(), 1);
    }

    use super::*;

    const UUID_TEI: &str = "tei://example.com/uuid/d4d9f54a-abcf-11ee-ac79-1a52914d44b1";
    const PURL_TEI: &str =
        "tei://example.com/purl/cGtnOm1hdmVuL2NvbW1vbnMtaW8vY29tbW9ucy1pb0AyLjIyLjA";
    const HASH_TEI: &str = "tei://example.com/hash/SHA-256:fd44efd601f651c8865acf0dfeacb0df19a2b50ec69ead0262096fd2f67197b9";

    #[test]
    fn parses_registry_examples() {
        for tei in [
            UUID_TEI,
            PURL_TEI,
            HASH_TEI,
            "tei://example.com/eanupc/1234567890128",
            "tei://example.com/gtin/0234567890129",
            "tei://example.com/asin/B07FZ8S74R",
            "tei://example.com/udi/00123456789012",
        ] {
            let parsed = Tei::parse(tei).unwrap();
            parsed.validate_registry().unwrap();
            assert_eq!(parsed.as_str(), tei);
            assert_eq!(parsed.to_string(), tei);
        }
    }

    #[test]
    fn does_not_treat_case_variants_as_equal() {
        let lower = Tei::parse(UUID_TEI).unwrap();
        let upper_domain =
            Tei::parse("tei://Example.com/uuid/d4d9f54a-abcf-11ee-ac79-1a52914d44b1").unwrap();
        assert_ne!(lower, upper_domain);
        assert!(!upper_domain.publication_issues().is_empty());
        assert!(Tei::parse("TEI://example.com/uuid/d4d9f54a-abcf-11ee-ac79-1a52914d44b1").is_err());
        assert!(Tei::parse("tei://example.com/UUID/d4d9f54a-abcf-11ee-ac79-1a52914d44b1").is_err());
    }

    #[test]
    fn rejects_slash_and_port() {
        assert!(Tei::parse("tei://example.com/uuid/a/b").is_err());
        assert!(Tei::parse("tei://example.com/uuid/a%2Fb").is_err());
        assert!(Tei::parse("tei://example.com/uuid/a%2fb").is_err());
        assert!(
            Tei::parse("tei://example.com:443/uuid/d4d9f54a-abcf-11ee-ac79-1a52914d44b1").is_err()
        );
    }

    #[test]
    fn unknown_type_parses_without_registry_rules() {
        let tei = Tei::parse("tei://example.com/sku/ABC-1").unwrap();
        assert!(tei.validate_registry().is_ok());
    }

    #[test]
    fn padded_purl_is_rejected() {
        let tei =
            Tei::parse("tei://example.com/purl/cGtnOnB5cGkvY3ljbG9uZWR4LXB5dGhvbi1saWI=").unwrap();
        assert!(tei.validate_registry().is_err());
    }

    #[test]
    fn query_encoding_matches_openapi_example_shape() {
        let encoded = encode_query_component(
            "tei://products.example.com/uuid/47f51950-b0e2-11f1-adc2-1a52914d44b2",
        );
        assert_eq!(
            encoded,
            "tei%3A%2F%2Fproducts.example.com%2Fuuid%2F47f51950-b0e2-11f1-adc2-1a52914d44b2"
        );
    }
}