radicle-core 0.3.0

Radicle core data type definitions
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
use alloc::fmt;
use alloc::string::String;
use alloc::string::ToString as _;
use alloc::vec::Vec;

use radicle_oid::Oid;
use thiserror::Error;

/// Radicle identifier prefix.
pub const RAD_PREFIX: &str = "rad:";

#[non_exhaustive]
#[derive(Error, Debug)]
pub enum IdError {
    #[error(transparent)]
    Multibase(#[from] multibase::Error),
    #[error("invalid length: expected {} bytes, got {actual} bytes", Oid::LEN_SHA1)]
    Length { actual: usize },
    #[error(fmt = fmt_mismatched_base_encoding)]
    MismatchedBaseEncoding {
        input: String,
        expected: Vec<multibase::Base>,
        found: multibase::Base,
    },
}

fn fmt_mismatched_base_encoding(
    input: &String,
    expected: &[multibase::Base],
    found: &multibase::Base,
    formatter: &mut fmt::Formatter,
) -> fmt::Result {
    write!(
        formatter,
        "invalid multibase encoding '{}' for '{}', expected one of {:?}",
        found.code(),
        input,
        expected.iter().map(|base| base.code()).collect::<Vec<_>>()
    )
}

/// A repository identifier.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct RepoId(
    #[cfg_attr(feature = "schemars", schemars(
        with = "String",
        description = "A repository identifier. Starts with \"rad:\", followed by a multibase Base58 encoded Git object identifier.",
        regex(pattern = r"rad:z[1-9a-km-zA-HJ-NP-Z]+"),
        length(min = 5),
        example = &"rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5",
    ))]
    Oid,
);

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

impl core::fmt::Debug for RepoId {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "RepoId({self})")
    }
}

impl RepoId {
    const ALLOWED_BASES: [multibase::Base; 1] = [multibase::Base::Base58Btc];

    /// Format the identifier as a human-readable URN.
    ///
    /// Eg. `rad:z3XncAdkZjeK9mQS5Sdc4qhw98BUX`.
    ///
    #[must_use]
    pub fn urn(&self) -> String {
        RAD_PREFIX.to_string() + &self.canonical()
    }

    /// Parse an identifier from the human-readable URN format.
    /// Accepts strings without the prefix [`RAD_PREFIX`] as well,
    /// for convenience.
    pub fn from_urn(s: &str) -> Result<Self, IdError> {
        let s = s.strip_prefix(RAD_PREFIX).unwrap_or(s);
        let id = Self::from_canonical(s)?;

        Ok(id)
    }

    /// Format the identifier as a multibase string.
    ///
    /// Eg. `z3XncAdkZjeK9mQS5Sdc4qhw98BUX`.
    ///
    #[must_use]
    pub fn canonical(&self) -> String {
        multibase::encode(multibase::Base::Base58Btc, AsRef::<[u8]>::as_ref(&self.0))
    }

    /// Decode the input string into a [`RepoId`].
    ///
    /// # Errors
    ///
    /// - The [multibase] decoding fails
    /// - The decoded [multibase] code does not match any expected multibase code
    /// - The input exceeds the expected number of bytes, post multibase decoding
    ///
    /// [multibase]: https://github.com/multiformats/multibase?tab=readme-ov-file#multibase-table
    pub fn from_canonical(input: &str) -> Result<Self, IdError> {
        let (base, bytes) = multibase::decode(input)?;
        Self::guard_base_encoding(input, base)?;
        let bytes: [u8; Oid::LEN_SHA1] =
            bytes.try_into().map_err(|bytes: Vec<u8>| IdError::Length {
                actual: bytes.len(),
            })?;
        Ok(Self(Oid::from_sha1(bytes)))
    }

    fn guard_base_encoding(input: &str, base: multibase::Base) -> Result<(), IdError> {
        if !Self::ALLOWED_BASES.contains(&base) {
            Err(IdError::MismatchedBaseEncoding {
                input: input.to_string(),
                expected: Self::ALLOWED_BASES.to_vec(),
                found: base,
            })
        } else {
            Ok(())
        }
    }
}

impl core::str::FromStr for RepoId {
    type Err = IdError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::from_urn(s)
    }
}

#[cfg(feature = "std")]
mod std_impls {
    extern crate std;

    use super::{IdError, RepoId};

    use std::ffi::OsString;

    impl TryFrom<OsString> for RepoId {
        type Error = IdError;

        fn try_from(value: OsString) -> Result<Self, Self::Error> {
            let string = value.to_string_lossy();
            Self::from_canonical(&string)
        }
    }

    impl std::hash::Hash for RepoId {
        fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
            self.0.hash(state)
        }
    }
}

impl From<Oid> for RepoId {
    fn from(oid: Oid) -> Self {
        Self(oid)
    }
}

impl core::ops::Deref for RepoId {
    type Target = Oid;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

#[cfg(feature = "git2")]
mod git2_impls {
    use super::RepoId;

    impl From<git2::Oid> for RepoId {
        fn from(oid: git2::Oid) -> Self {
            Self(oid.into())
        }
    }
}

#[cfg(feature = "gix")]
mod gix_impls {
    use super::RepoId;

    impl From<gix_hash::ObjectId> for RepoId {
        fn from(oid: gix_hash::ObjectId) -> Self {
            Self(oid.into())
        }
    }
}

#[cfg(feature = "radicle-git-ref-format")]
mod radicle_git_ref_format_impls {
    use alloc::string::ToString;

    use radicle_git_ref_format::{Component, RefString};

    use super::RepoId;

    impl From<&RepoId> for Component<'_> {
        fn from(id: &RepoId) -> Self {
            let refstr = RefString::try_from(id.0.to_string())
                .expect("repository id's are valid ref strings");
            Component::from_refstr(refstr).expect("repository id's are valid refname components")
        }
    }
}

#[cfg(feature = "serde")]
mod serde_impls {
    use alloc::string::String;

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

    use super::RepoId;

    impl Serialize for RepoId {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: serde::Serializer,
        {
            serializer.collect_str(&self.urn())
        }
    }

    impl<'de> Deserialize<'de> for RepoId {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: Deserializer<'de>,
        {
            String::deserialize(deserializer)?
                .parse()
                .map_err(de::Error::custom)
        }
    }

    #[cfg(test)]
    mod test {
        use proptest::proptest;

        use super::super::*;

        fn prop_roundtrip_serde_json(rid: RepoId) {
            let encoded = serde_json::to_string(&rid).unwrap();
            let decoded = serde_json::from_str(&encoded).unwrap();

            assert_eq!(rid, decoded);
        }

        proptest! {
            #[test]
            fn assert_prop_roundtrip_serde_json(rid in arbitrary::rid()) {
                prop_roundtrip_serde_json(rid)
            }
        }
    }
}

#[cfg(feature = "sqlite")]
mod sqlite_impls {
    use alloc::format;
    use alloc::string::ToString;

    use super::RepoId;

    use sqlite::{BindableWithIndex, Error, ParameterIndex, Statement, Value};

    impl TryFrom<&Value> for RepoId {
        type Error = Error;

        fn try_from(value: &Value) -> Result<Self, Self::Error> {
            match value {
                Value::String(id) => RepoId::from_urn(id).map_err(|e| Error {
                    code: None,
                    message: Some(e.to_string()),
                }),
                Value::Binary(_) | Value::Float(_) | Value::Integer(_) | Value::Null => {
                    Err(Error {
                        code: None,
                        message: Some(format!("sql: invalid type `{:?}` for id", value.kind())),
                    })
                }
            }
        }
    }

    impl BindableWithIndex for &RepoId {
        fn bind<I: ParameterIndex>(self, stmt: &mut Statement<'_>, i: I) -> sqlite::Result<()> {
            self.urn().as_str().bind(stmt, i)
        }
    }
}

#[cfg(any(test, feature = "proptest"))]
pub mod arbitrary {
    use proptest::prelude::Strategy;

    use super::RepoId;

    pub fn rid() -> impl Strategy<Value = RepoId> {
        proptest::array::uniform20(proptest::num::u8::ANY)
            .prop_map(|bytes| RepoId::from(radicle_oid::Oid::from_sha1(bytes)))
    }
}

#[cfg(feature = "qcheck")]
impl qcheck::Arbitrary for RepoId {
    fn arbitrary(g: &mut qcheck::Gen) -> Self {
        RepoId::from(radicle_oid::Oid::arbitrary(g))
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod test {
    use super::*;
    use proptest::proptest;

    fn prop_roundtrip_parse(rid: RepoId) {
        use core::str::FromStr as _;
        let encoded = rid.to_string();
        let decoded = RepoId::from_str(&encoded).unwrap();

        assert_eq!(rid, decoded);
    }

    proptest! {
        #[test]
        fn assert_prop_roundtrip_parse(rid in arbitrary::rid()) {
            prop_roundtrip_parse(rid)
        }
    }

    #[test]
    fn invalid() {
        assert!("".parse::<RepoId>().is_err());
        assert!("not-a-valid-rid".parse::<RepoId>().is_err());
        assert!(
            "xyz:z3gqcJUoA1n9HaHKufZs5FCSGazv5"
                .parse::<RepoId>()
                .is_err()
        );
        assert!(
            "RAD:z3gqcJUoA1n9HaHKufZs5FCSGazv5"
                .parse::<RepoId>()
                .is_err()
        );
        assert!("rad:".parse::<RepoId>().is_err());
        assert!(
            "rad:z3gqcJUoA1n9HaHKufZs5FCSG0zv5"
                .parse::<RepoId>()
                .is_err()
        );
        assert!(
            "rad:z3gqcJUoA1n9HaHKufZs5FCSGOzv5"
                .parse::<RepoId>()
                .is_err()
        );
        assert!(
            "rad:z3gqcJUoA1n9HaHKufZs5FCSGIzv5"
                .parse::<RepoId>()
                .is_err()
        );
        assert!(
            "rad:z3gqcJUoA1n9HaHKufZs5FCSGlzv5"
                .parse::<RepoId>()
                .is_err()
        );
        assert!(
            "rad:z3gqcJUoA1n9HaHKufZs5FCSGázv5"
                .parse::<RepoId>()
                .is_err()
        );
        assert!(
            "rad:z3gqcJUoA1n9HaHKufZs5FCSG@zv5"
                .parse::<RepoId>()
                .is_err()
        );
        assert!(
            "rad:Z3gqcJUoA1n9HaHKufZs5FCSGazv5"
                .parse::<RepoId>()
                .is_err()
        );
        assert!("rad:z3gqcJUoA1n9HaHKuf".parse::<RepoId>().is_err());
        assert!(
            "rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5abcdef"
                .parse::<RepoId>()
                .is_err()
        );
        assert!(
            "rad: z3gqcJUoA1n9HaHKufZs5FCSGazv5"
                .parse::<RepoId>()
                .is_err()
        );
    }

    #[test]
    fn valid() {
        assert!(
            "rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5"
                .parse::<RepoId>()
                .is_ok()
        );
        assert!("z3gqcJUoA1n9HaHKufZs5FCSGazv5".parse::<RepoId>().is_ok());
        assert!("z3XncAdkZjeK9mQS5Sdc4qhw98BUX".parse::<RepoId>().is_ok());
    }
}