pub mod profile {
use std::path::PathBuf;
use radicle::crypto::ssh::Keystore;
use radicle::crypto::PublicKey;
use radicle::git::UserInfo;
use radicle::profile::{Config, Home};
use radicle::Storage;
use snafu::{OptionExt, ResultExt};
#[derive(Clone, Debug)]
pub struct Profile {
pub home: Home,
pub storage: Storage,
pub keystore: Keystore,
pub public_key: PublicKey,
pub config: Config,
}
impl Profile {
pub fn load(home_path: PathBuf) -> Result<Self, snafu::Whatever> {
let home = Home::try_from(home_path).whatever_context("Unable to load home")?;
let keystore = Keystore::new(&home.keys());
let config = Config::load(&home.config()).whatever_context("Unable to load config")?;
let public_key = keystore
.public_key()
.whatever_context("Unable to load keystore")?
.whatever_context("Not able to find public key")?;
let storage = Storage::open(
home.storage(),
UserInfo {
alias: config.node.alias.clone(),
key: public_key,
},
)
.whatever_context("Unable to open storage")?;
Ok(Self {
home,
storage,
keystore,
public_key,
config,
})
}
}
impl From<radicle::Profile> for Profile {
fn from(profile: radicle::Profile) -> Self {
Self {
home: profile.home,
storage: profile.storage,
keystore: profile.keystore,
public_key: profile.public_key,
config: profile.config,
}
}
}
impl From<Profile> for radicle::Profile {
fn from(profile: Profile) -> Self {
Self {
home: profile.home,
storage: profile.storage,
keystore: profile.keystore,
public_key: profile.public_key,
config: profile.config,
}
}
}
}
pub mod sql {
use std::str::FromStr;
use postgres_types::FromSql;
#[derive(Clone, Copy)]
pub struct Oid(radicle::git::Oid);
impl std::fmt::Display for Oid {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl<'a> FromSql<'a> for Oid {
fn from_sql(
ty: &postgres_types::Type,
raw: &'a [u8],
) -> Result<Oid, Box<dyn std::error::Error + Sync + Send>> {
radicle::git::Oid::from_str(&<String as FromSql>::from_sql(ty, raw)?)
.map(Oid)
.map_err(|e| e.into())
}
fn accepts(ty: &postgres_types::Type) -> bool {
<String as FromSql>::accepts(ty)
}
}
impl From<Oid> for radicle::git::Oid {
fn from(value: Oid) -> Self {
value.0
}
}
impl From<radicle::git::Oid> for Oid {
fn from(value: radicle::git::Oid) -> Self {
Oid(value)
}
}
}
pub mod cob {
use radicle::cob::ObjectId;
pub struct CobInfo {
pub id: ObjectId,
pub title: String,
pub status: String,
}
}