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
// Copyright © 2019-2020 The Radicle Foundation <hello@radicle.foundation>

use git_ext::commit::trailers::{OwnedTrailer, Token, Trailer};
use std::ops::Deref as _;

pub mod error {
    use thiserror::Error;

    #[derive(Debug, Error)]
    pub enum InvalidResourceTrailer {
        #[error("found wrong token for Rad-Resource tailer")]
        WrongToken,
        #[error("no value for Rad-Resource")]
        NoValue,
        #[error("invalid git OID")]
        InvalidOid,
    }
}

/// Commit trailer for COB commits.
pub enum CommitTrailer {
    /// Points to the owning resource.
    Resource(git2::Oid),
    /// Points to a related change.
    Related(git2::Oid),
}

impl CommitTrailer {
    pub fn oid(&self) -> git2::Oid {
        match self {
            Self::Resource(oid) => *oid,
            Self::Related(oid) => *oid,
        }
    }
}

impl TryFrom<&Trailer<'_>> for CommitTrailer {
    type Error = error::InvalidResourceTrailer;

    fn try_from(Trailer { value, token }: &Trailer<'_>) -> Result<Self, Self::Error> {
        let ext_oid =
            git_ext::Oid::try_from(value.as_ref()).map_err(|_| Self::Error::InvalidOid)?;
        if token.deref() == "Rad-Resource" {
            Ok(CommitTrailer::Resource(ext_oid.into()))
        } else if token.deref() == "Rad-Related" {
            Ok(CommitTrailer::Related(ext_oid.into()))
        } else {
            Err(Self::Error::WrongToken)
        }
    }
}

impl TryFrom<&OwnedTrailer> for CommitTrailer {
    type Error = error::InvalidResourceTrailer;

    fn try_from(trailer: &OwnedTrailer) -> Result<Self, Self::Error> {
        Self::try_from(&Trailer::from(trailer))
    }
}

impl From<CommitTrailer> for Trailer<'_> {
    fn from(t: CommitTrailer) -> Self {
        match t {
            #[allow(clippy::unwrap_used)]
            CommitTrailer::Related(oid) => {
                Trailer {
                    // SAFETY: "Rad-Related" is a valid `Token`.
                    token: Token::try_from("Rad-Related").unwrap(),
                    value: oid.to_string().into(),
                }
            }
            #[allow(clippy::unwrap_used)]
            CommitTrailer::Resource(oid) => {
                Trailer {
                    // SAFETY: "Rad-Resource" is a valid `Token`.
                    token: Token::try_from("Rad-Resource").unwrap(),
                    value: oid.to_string().into(),
                }
            }
        }
    }
}

impl From<CommitTrailer> for OwnedTrailer {
    fn from(containing: CommitTrailer) -> Self {
        Trailer::from(containing).to_owned()
    }
}