mirror of
https://seed.radicle.dev/rad:z6cFWeWpnZNHh9rUW8phgA3b5yGt.git
synced 2026-10-18 19:10:31 +08:00
The main dependant of `radicle-git`, and its crates, is [`heartwood`]. It is licensed under MIT or Apache-2.0, which has been incompatible with the GPL-3.0 license. Since the intellectual property of this code base is with the Better Internet Foundation, it was decided to re-license code in this repository to make the upstream repository compatible. - Remove the license headers (and instructions to add them) - Add MIT and Apache-2.0 licenses - Remove the GPL-3.0 license - Change the license declarations in the `Cargo.toml` files [`heartwood`]: https://app.radicle.xyz/nodes/seed.radicle.xyz/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5
114 lines
2.4 KiB
Rust
114 lines
2.4 KiB
Rust
use std::{
|
|
convert::TryFrom,
|
|
fmt::{self, Display},
|
|
ops::Deref,
|
|
str::FromStr,
|
|
};
|
|
|
|
/// Serializable [`git2::Oid`]
|
|
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
|
pub struct Oid(git2::Oid);
|
|
|
|
#[cfg(feature = "serde")]
|
|
mod serde_impls {
|
|
use super::*;
|
|
use serde::{de::Visitor, Deserialize, Deserializer, Serialize, Serializer};
|
|
|
|
impl Serialize for Oid {
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
where
|
|
S: Serializer,
|
|
{
|
|
self.0.to_string().serialize(serializer)
|
|
}
|
|
}
|
|
|
|
impl<'de> Deserialize<'de> for Oid {
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
where
|
|
D: Deserializer<'de>,
|
|
{
|
|
struct OidVisitor;
|
|
|
|
impl Visitor<'_> for OidVisitor {
|
|
type Value = Oid;
|
|
|
|
fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "a hexidecimal git2::Oid")
|
|
}
|
|
|
|
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
|
|
where
|
|
E: serde::de::Error,
|
|
{
|
|
s.parse().map_err(serde::de::Error::custom)
|
|
}
|
|
}
|
|
|
|
deserializer.deserialize_str(OidVisitor)
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Deref for Oid {
|
|
type Target = git2::Oid;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
impl AsRef<git2::Oid> for Oid {
|
|
fn as_ref(&self) -> &git2::Oid {
|
|
self
|
|
}
|
|
}
|
|
|
|
impl AsRef<[u8]> for Oid {
|
|
fn as_ref(&self) -> &[u8] {
|
|
self.as_bytes()
|
|
}
|
|
}
|
|
|
|
impl From<git2::Oid> for Oid {
|
|
fn from(oid: git2::Oid) -> Self {
|
|
Self(oid)
|
|
}
|
|
}
|
|
|
|
impl From<Oid> for git2::Oid {
|
|
fn from(oid: Oid) -> Self {
|
|
oid.0
|
|
}
|
|
}
|
|
|
|
impl Display for Oid {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
self.0.fmt(f)
|
|
}
|
|
}
|
|
|
|
impl TryFrom<&str> for Oid {
|
|
type Error = git2::Error;
|
|
|
|
fn try_from(s: &str) -> Result<Self, Self::Error> {
|
|
s.parse().map(Self)
|
|
}
|
|
}
|
|
|
|
impl FromStr for Oid {
|
|
type Err = git2::Error;
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
Self::try_from(s)
|
|
}
|
|
}
|
|
|
|
impl TryFrom<&[u8]> for Oid {
|
|
type Error = git2::Error;
|
|
|
|
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
|
|
git2::Oid::from_bytes(bytes).map(Self)
|
|
}
|
|
}
|