From 51f2d4e3fc17736740db6b7ea1f7d5fc31e90ed7 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Thu, 22 Sep 2022 16:31:58 +0200 Subject: [PATCH] Move identity types to project Signed-off-by: Alexis Sellier --- radicle/src/identity.rs | 97 +-------------------------- radicle/src/identity/project.rs | 5 +- radicle/src/identity/project/id.rs | 101 +++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 96 deletions(-) create mode 100644 radicle/src/identity/project/id.rs diff --git a/radicle/src/identity.rs b/radicle/src/identity.rs index 4c6a46b0..8bff7dbb 100644 --- a/radicle/src/identity.rs +++ b/radicle/src/identity.rs @@ -1,108 +1,15 @@ pub mod project; use std::ops::Deref; -use std::{ffi::OsString, fmt, str::FromStr}; +use std::{fmt, str::FromStr}; use serde::{Deserialize, Serialize}; use thiserror::Error; use crate::crypto; -use crate::git; -use crate::serde_ext; pub use crypto::PublicKey; -pub use project::{Delegate, Doc}; - -#[derive(Error, Debug)] -pub enum IdError { - #[error("invalid git object id: {0}")] - InvalidOid(#[from] git2::Error), - #[error(transparent)] - Multibase(#[from] multibase::Error), -} - -#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct Id(git::Oid); - -impl fmt::Display for Id { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str(&self.to_human()) - } -} - -impl fmt::Debug for Id { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "Id({})", self) - } -} - -impl Id { - pub fn to_human(&self) -> String { - multibase::encode(multibase::Base::Base58Btc, self.0.as_bytes()) - } - - pub fn from_human(s: &str) -> Result { - let (_, bytes) = multibase::decode(s)?; - let array: git::Oid = bytes.as_slice().try_into()?; - - Ok(Self(array)) - } -} - -impl FromStr for Id { - type Err = IdError; - - fn from_str(s: &str) -> Result { - Self::from_human(s) - } -} - -impl TryFrom for Id { - type Error = IdError; - - fn try_from(value: OsString) -> Result { - let string = value.to_string_lossy(); - Self::from_str(&string) - } -} - -impl From for Id { - fn from(oid: git::Oid) -> Self { - Self(oid) - } -} - -impl From for Id { - fn from(oid: git2::Oid) -> Self { - Self(oid.into()) - } -} - -impl Deref for Id { - type Target = git::Oid; - - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl serde::Serialize for Id { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - serde_ext::string::serialize(self, serializer) - } -} - -impl<'de> serde::Deserialize<'de> for Id { - fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - serde_ext::string::deserialize(deserializer) - } -} +pub use project::{Delegate, Doc, Id, IdError}; #[derive(Error, Debug)] pub enum DidError { diff --git a/radicle/src/identity/project.rs b/radicle/src/identity/project.rs index 1bef65ec..153b7175 100644 --- a/radicle/src/identity/project.rs +++ b/radicle/src/identity/project.rs @@ -1,3 +1,5 @@ +mod id; + use std::collections::{BTreeMap, HashMap}; use std::fmt::Write as _; use std::io; @@ -14,11 +16,12 @@ use thiserror::Error; use crate::crypto; use crate::crypto::{Signature, Unverified, Verified}; use crate::git; -use crate::identity::{Did, Id}; +use crate::identity::Did; use crate::storage::git::trailers; use crate::storage::{BranchName, ReadRepository, RemoteId, WriteRepository, WriteStorage}; pub use crypto::PublicKey; +pub use id::*; /// Untrusted, well-formed input. #[derive(Clone, Copy, Debug)] diff --git a/radicle/src/identity/project/id.rs b/radicle/src/identity/project/id.rs new file mode 100644 index 00000000..56a6aad1 --- /dev/null +++ b/radicle/src/identity/project/id.rs @@ -0,0 +1,101 @@ +use std::ops::Deref; +use std::{ffi::OsString, fmt, str::FromStr}; + +use thiserror::Error; + +use crate::crypto; +use crate::git; +use crate::serde_ext; + +pub use crypto::PublicKey; + +#[derive(Error, Debug)] +pub enum IdError { + #[error("invalid git object id: {0}")] + InvalidOid(#[from] git2::Error), + #[error(transparent)] + Multibase(#[from] multibase::Error), +} + +#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct Id(git::Oid); + +impl fmt::Display for Id { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(&self.to_human()) + } +} + +impl fmt::Debug for Id { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Id({})", self) + } +} + +impl Id { + pub fn to_human(&self) -> String { + multibase::encode(multibase::Base::Base58Btc, self.0.as_bytes()) + } + + pub fn from_human(s: &str) -> Result { + let (_, bytes) = multibase::decode(s)?; + let array: git::Oid = bytes.as_slice().try_into()?; + + Ok(Self(array)) + } +} + +impl FromStr for Id { + type Err = IdError; + + fn from_str(s: &str) -> Result { + Self::from_human(s) + } +} + +impl TryFrom for Id { + type Error = IdError; + + fn try_from(value: OsString) -> Result { + let string = value.to_string_lossy(); + Self::from_str(&string) + } +} + +impl From for Id { + fn from(oid: git::Oid) -> Self { + Self(oid) + } +} + +impl From for Id { + fn from(oid: git2::Oid) -> Self { + Self(oid.into()) + } +} + +impl Deref for Id { + type Target = git::Oid; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl serde::Serialize for Id { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serde_ext::string::serialize(self, serializer) + } +} + +impl<'de> serde::Deserialize<'de> for Id { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + serde_ext::string::deserialize(deserializer) + } +}