Skip to content
This repository was archived by the owner on Feb 28, 2021. It is now read-only.

Commit 5239376

Browse files
committed
cli: Use thiserror #[from] instead of derive_more
Plus some other improvements.
1 parent 87764b1 commit 5239376

File tree

2 files changed

+22
-27
lines changed

2 files changed

+22
-27
lines changed

cli/src/account_storage.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,37 +45,36 @@ pub enum Error {
4545

4646
/// Failed to write to the accounts file
4747
#[error("Failed to write to the accounts file: {0}")]
48-
FailedWrite(WritingError),
48+
FailedWrite(#[from] WritingError),
4949

5050
/// Failed to read the accounts file
5151
#[error("Failed to read the accounts file: {0}")]
52-
FailedRead(ReadingError),
52+
FailedRead(#[from] ReadingError),
5353
}
5454

55-
5655
/// Possible errors when writing to the accounts file.
5756
#[derive(Debug, ThisError)]
5857
pub enum WritingError {
59-
#[error("{0}")]
58+
#[error(transparent)]
6059
IO(IOError),
6160

62-
#[error("{0}")]
61+
#[error(transparent)]
6362
Serialization(serde_json::Error),
6463
}
6564

6665
/// Possible errors when reading the accounts file.
6766
#[derive(Debug, ThisError)]
6867
pub enum ReadingError {
69-
#[error("{0}")]
68+
#[error(transparent)]
7069
IO(IOError),
7170

72-
#[error("{0}")]
71+
#[error(transparent)]
7372
Deserialization(serde_json::Error),
7473
}
7574

7675
/// Add an account to the storage.
7776
///
78-
/// Fails if an account with the given `name` already exists_.
77+
/// Fails if an account with the given `name` already exists.
7978
/// It can also fail from IO and Serde Json errors.
8079
pub fn add(name: String, data: AccountData) -> Result<(), Error> {
8180
let mut accounts = list()?;
@@ -87,25 +86,22 @@ pub fn add(name: String, data: AccountData) -> Result<(), Error> {
8786
update(accounts)
8887
}
8988

90-
/// List all the stored accountsr
89+
/// List all the stored accounts.
9190
///
9291
/// It can fail from IO and Serde Json errors.
9392
pub fn list() -> Result<HashMap<String, AccountData>, Error> {
9493
let path_buf = get_or_create_path()?;
95-
let file = File::open(path_buf.as_path())
96-
.map_err(|e| Error::FailedRead(ReadingError::IO(e)))?;
94+
let file = File::open(path_buf.as_path()).map_err(ReadingError::IO)?;
9795
let accounts: HashMap<String, AccountData> =
98-
serde_json::from_reader(&file)
99-
.map_err(|e| Error::FailedRead(ReadingError::Deserialization(e)))?;
96+
serde_json::from_reader(&file).map_err(ReadingError::Deserialization)?;
10097
Ok(accounts)
10198
}
10299

103100
fn update(accounts: HashMap<String, AccountData>) -> Result<(), Error> {
104101
let path_buf = get_or_create_path()?;
105-
let new_content = serde_json::to_string(&accounts)
106-
.map_err(|e| Error::FailedWrite(WritingError::Serialization(e)))?;
107-
std::fs::write(path_buf.as_path(), new_content.as_bytes())
108-
.map_err(|e| Error::FailedWrite(WritingError::IO(e)))
102+
let new_content = serde_json::to_string(&accounts).map_err(WritingError::Serialization)?;
103+
std::fs::write(path_buf.as_path(), new_content.as_bytes()).map_err(WritingError::IO)?;
104+
Ok(())
109105
}
110106

111107
const FILE: &str = "accounts.json";
@@ -121,8 +117,7 @@ fn get_or_create_path() -> Result<PathBuf, Error> {
121117
let path = path_buf.as_path();
122118

123119
if !path.exists() {
124-
std::fs::write(path, b"{}")
125-
.map_err(|e| Error::FailedWrite(WritingError::IO(e)))?;
120+
std::fs::write(path, b"{}").map_err(WritingError::IO)?;
126121
}
127122

128123
Ok(path_buf)
@@ -133,6 +128,6 @@ fn dir() -> Result<PathBuf, Error> {
133128
.unwrap()
134129
.data_dir()
135130
.join("radicle-registry-cli");
136-
std::fs::create_dir_all(&dir).map_err(|e| Error::FailedRead(ReadingError::IO(e)))?;
131+
std::fs::create_dir_all(&dir).map_err(ReadingError::IO)?;
137132
Ok(dir)
138133
}

cli/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,27 +119,27 @@ pub trait CommandT {
119119

120120
/// Error returned by [CommandT::run].
121121
///
122-
/// Implements [From] for client errors.
123-
#[derive(Debug, derive_more::From, ThisError)]
122+
/// Implements [From] for client errors and [account_storage] errors.
123+
#[derive(Debug, ThisError)]
124124
pub enum CommandError {
125125
#[error("Client error: {0}")]
126-
ClientError(Error),
126+
ClientError(#[from] Error),
127127

128-
#[error("Transaction {tx_hash:?} failed in block {block_hash:?}")]
128+
#[error("Transaction {tx_hash} failed in block {block_hash}")]
129129
FailedTransaction {
130130
tx_hash: TxHash,
131131
block_hash: BlockHash,
132132
},
133133

134-
#[error("Cannot find org {org_id:?}")]
134+
#[error("Cannot find org {org_id}")]
135135
OrgNotFound { org_id: OrgId },
136136

137-
#[error("Cannot find project {project_name:?}.{org_id:?}")]
137+
#[error("Cannot find project {project_name}.{org_id}")]
138138
ProjectNotFound {
139139
project_name: ProjectName,
140140
org_id: OrgId,
141141
},
142142

143143
#[error("{0}")]
144-
AccountStorageError(account_storage::Error),
144+
AccountStorageError(#[from] account_storage::Error),
145145
}

0 commit comments

Comments
 (0)