radicle_source/
oid.rs

1// This file is part of radicle-surf
2// <https://github.com/radicle-dev/radicle-surf>
3//
4// Copyright (C) 2019-2020 The Radicle Team <dev@radicle.xyz>
5//
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License version 3 or
8// later as published by the Free Software Foundation.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17
18use std::convert::TryFrom;
19
20use serde::{Deserialize, Serialize};
21
22#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
23#[serde(try_from = "&str", into = "String")]
24pub struct Oid(pub git2::Oid);
25
26impl TryFrom<&str> for Oid {
27    type Error = git2::Error;
28
29    fn try_from(value: &str) -> Result<Self, Self::Error> {
30        value.parse().map(Oid)
31    }
32}
33
34impl From<Oid> for String {
35    fn from(oid: Oid) -> Self {
36        oid.0.to_string()
37    }
38}
39
40impl From<Oid> for git2::Oid {
41    fn from(oid: Oid) -> Self {
42        oid.0
43    }
44}