Skip to content

Commit

Permalink
librad: Replace RadHome's new with default
Browse files Browse the repository at this point in the history
It expresses the behaviour more correctly as the implementation of new
is really about finding a sensible default in the presenece of env vars
and other factors.

Signed-off-by: Alexander Simmerl <[email protected]>
  • Loading branch information
xla committed Sep 14, 2021
1 parent 3547538 commit 482e794
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
20 changes: 12 additions & 8 deletions librad/src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use std::{
env,
fmt,
io,
path::{Path, PathBuf},
};
Expand Down Expand Up @@ -37,6 +38,7 @@ pub struct Profile {
}

/// An enumeration of where the root directory for a `Profile` lives.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum RadHome {
/// The system specific directories given by [`directories::ProjectDirs`].
ProjectDirs,
Expand All @@ -45,23 +47,25 @@ pub enum RadHome {
}

impl Default for RadHome {
fn default() -> Self {
Self::new()
}
}

impl RadHome {
/// If `RAD_HOME` is defined then the path supplied there is used and
/// [`RadHome::Root`] is constructed. Otherwise, [`RadHome::ProjectDirs`] is
/// constructed.
pub fn new() -> Self {
fn default() -> Self {
if let Ok(root) = env::var(RAD_HOME) {
Self::Root(Path::new(&root).to_path_buf())
} else {
Self::ProjectDirs
}
}
}

impl fmt::Display for RadHome {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}

impl RadHome {
fn config(&self) -> Result<PathBuf, io::Error> {
Ok(match self {
Self::ProjectDirs => project_dirs()?.config_dir().to_path_buf(),
Expand Down Expand Up @@ -151,7 +155,7 @@ impl Profile {
/// `ProjectDirs_DATA_HOME/radicle-link/<profile-id>`.
pub fn load() -> Result<Self, Error> {
let env_profile_id = ProfileId::from_env()?;
let home = RadHome::new();
let home = RadHome::default();
Self::from_home(&home, env_profile_id)
}

Expand Down
14 changes: 7 additions & 7 deletions rad-profile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ where
C::Error: fmt::Debug + fmt::Display + Send + Sync + 'static,
C::SecretBox: Serialize + DeserializeOwned,
{
let home = RadHome::new();
let home = RadHome::default();
let profile = Profile::new(&home)?;
Profile::set(&home, profile.id().clone())?;
let key = SecretKey::new();
Expand All @@ -82,7 +82,7 @@ where

/// Get the current active `ProfileId`.
pub fn get(id: Option<ProfileId>) -> Result<Option<Profile>, Error> {
let home = RadHome::new();
let home = RadHome::default();
match id {
Some(id) => Profile::get(&home, id).map_err(Error::from),
None => Profile::active(&home).map_err(Error::from),
Expand All @@ -91,13 +91,13 @@ pub fn get(id: Option<ProfileId>) -> Result<Option<Profile>, Error> {

/// Set the active profile to the given `ProfileId`.
pub fn set(id: ProfileId) -> Result<(), Error> {
let home = RadHome::new();
let home = RadHome::default();
Profile::set(&home, id).map_err(Error::from).map(|_| ())
}

/// List the set of active profiles that exist.
pub fn list() -> Result<Vec<Profile>, Error> {
let home = RadHome::new();
let home = RadHome::default();
Profile::list(&home).map_err(Error::from)
}

Expand All @@ -106,7 +106,7 @@ pub fn peer_id<P>(id: P) -> Result<PeerId, Error>
where
P: Into<Option<ProfileId>>,
{
let home = RadHome::new();
let home = RadHome::default();
let profile = get_or_active(&home, id)?;
let read = ReadOnly::open(profile.paths())?;
Ok(*read.peer_id())
Expand All @@ -116,7 +116,7 @@ pub fn paths<P>(id: P) -> Result<Paths, Error>
where
P: Into<Option<ProfileId>>,
{
let home = RadHome::new();
let home = RadHome::default();
get_or_active(&home, id).map(|p| p.paths().clone())
}

Expand All @@ -133,7 +133,7 @@ where
P: Into<Option<ProfileId>>,
S: ClientStream + Unpin + 'static,
{
let home = RadHome::new();
let home = RadHome::default();
let profile = get_or_active(&home, id)?;
let store = keys::file_storage(&profile, crypto);
let key = store.get_key()?;
Expand Down

0 comments on commit 482e794

Please sign in to comment.