Skip to content

Commit 552b248

Browse files
authored
Merge pull request #139 from input-output-hk/color-eyre
Replace thiserror with color-eyre
2 parents eaf0c3b + 098f7e7 commit 552b248

File tree

37 files changed

+215
-344
lines changed

37 files changed

+215
-344
lines changed

Cargo.lock

Lines changed: 60 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

catalyst-toolbox/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ serde_json = "1.0"
4747
structopt = "0.3"
4848
serde_yaml = "0.8.17"
4949
sscanf = "0.1"
50+
color-eyre = "0.6"
5051
thiserror = "1.0"
5152
tokio = { version = "1.8", features = ["rt", "macros"] }
5253
url = "2.2"
Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
1-
use std::error::Error;
2-
3-
use structopt::StructOpt as _;
1+
use structopt::StructOpt;
42

53
pub mod cli;
64

7-
fn main() {
8-
env_logger::init();
9-
cli::Cli::from_args().exec().unwrap_or_else(report_error)
10-
}
11-
12-
fn report_error(error: Box<dyn Error>) {
13-
eprintln!("{}", error);
14-
let mut source = error.source();
15-
while let Some(sub_error) = source {
16-
eprintln!(" |-> {}", sub_error);
17-
source = sub_error.source();
18-
}
19-
std::process::exit(1)
5+
fn main() -> color_eyre::Result<()> {
6+
env_logger::try_init()?;
7+
color_eyre::install()?;
8+
cli::Cli::from_args().exec()?;
9+
Ok(())
2010
}

catalyst-toolbox/src/bin/cli/advisor_reviews/mod.rs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,14 @@
11
use catalyst_toolbox::utils;
2-
use catalyst_toolbox::vca_reviews::{read_vca_reviews_aggregated_file, Error as ReviewsError};
2+
use catalyst_toolbox::vca_reviews::read_vca_reviews_aggregated_file;
33

4+
use color_eyre::eyre::bail;
5+
use color_eyre::Report;
46
use jcli_lib::utils::io::open_file_write;
57
use std::fmt;
68
use std::path::PathBuf;
79
use std::str::FromStr;
810
use structopt::StructOpt;
911

10-
#[derive(thiserror::Error, Debug)]
11-
pub enum Error {
12-
#[error(transparent)]
13-
Review(#[from] ReviewsError),
14-
15-
#[error("Error while serializing reviews to json")]
16-
SerializeToJson(#[from] serde_json::Error),
17-
18-
#[error("Error while serializing reviews to csv")]
19-
SerializeToCsv(#[from] csv::Error),
20-
21-
#[error("Invalid output format {0}. Only 'csv' and 'json' are supported")]
22-
InvalidFormat(String),
23-
24-
#[error(transparent)]
25-
Io(#[from] std::io::Error),
26-
}
27-
2812
#[derive(Debug)]
2913
pub enum OutputFormat {
3014
Csv,
@@ -50,13 +34,13 @@ pub struct Export {
5034
}
5135

5236
impl FromStr for OutputFormat {
53-
type Err = Error;
37+
type Err = Report;
5438

5539
fn from_str(s: &str) -> Result<Self, Self::Err> {
5640
match s.to_lowercase().as_str() {
5741
"csv" => Ok(Self::Csv),
5842
"json" => Ok(Self::Json),
59-
other => Err(Error::InvalidFormat(other.to_string())),
43+
other => bail!("invalid format: {other}"),
6044
}
6145
}
6246
}
@@ -68,7 +52,7 @@ impl fmt::Display for OutputFormat {
6852
}
6953

7054
impl Reviews {
71-
pub fn exec(self) -> Result<(), Error> {
55+
pub fn exec(self) -> Result<(), Report> {
7256
match self {
7357
Reviews::Export(transform) => transform.exec()?,
7458
};
@@ -77,7 +61,7 @@ impl Reviews {
7761
}
7862

7963
impl Export {
80-
pub fn exec(self) -> Result<(), Error> {
64+
pub fn exec(self) -> Result<(), Report> {
8165
let Self { from, to, format } = self;
8266

8367
let reviews = read_vca_reviews_aggregated_file(&from)?;

catalyst-toolbox/src/bin/cli/archive/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use catalyst_toolbox::archive::{generate_archive_files, Error};
1+
use catalyst_toolbox::archive::generate_archive_files;
22

3+
use color_eyre::Report;
34
use structopt::StructOpt;
45

56
use std::path::PathBuf;
@@ -14,7 +15,8 @@ pub struct Archive {
1415
}
1516

1617
impl Archive {
17-
pub fn exec(self) -> Result<(), Error> {
18-
generate_archive_files(&self.jormungandr_database, &self.output_dir)
18+
pub fn exec(self) -> Result<(), Report> {
19+
generate_archive_files(&self.jormungandr_database, &self.output_dir)?;
20+
Ok(())
1921
}
2022
}

catalyst-toolbox/src/bin/cli/ideascale/mod.rs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use catalyst_toolbox::ideascale::{
2-
build_challenges, build_fund, build_proposals, fetch_all, CustomFieldTags,
3-
Error as IdeascaleError, Scores, Sponsors,
2+
build_challenges, build_fund, build_proposals, fetch_all, CustomFieldTags, Scores, Sponsors,
43
};
4+
use color_eyre::Report;
55
use jcli_lib::utils::io as io_utils;
66
use jormungandr_lib::interfaces::VotePrivacy;
77
use std::collections::HashSet;
@@ -12,21 +12,6 @@ use serde::de::DeserializeOwned;
1212
use serde::Serialize;
1313
use std::path::{Path, PathBuf};
1414

15-
#[derive(thiserror::Error, Debug)]
16-
pub enum Error {
17-
#[error(transparent)]
18-
Ideascale(#[from] IdeascaleError),
19-
20-
#[error(transparent)]
21-
Io(#[from] std::io::Error),
22-
23-
#[error(transparent)]
24-
Csv(#[from] csv::Error),
25-
26-
#[error(transparent)]
27-
Serde(#[from] serde_json::Error),
28-
}
29-
3015
#[derive(Debug, StructOpt)]
3116
pub enum Ideascale {
3217
Import(Import),
@@ -89,15 +74,15 @@ pub struct Import {
8974
}
9075

9176
impl Ideascale {
92-
pub fn exec(&self) -> Result<(), Error> {
77+
pub fn exec(&self) -> Result<(), Report> {
9378
match self {
9479
Ideascale::Import(import) => import.exec(),
9580
}
9681
}
9782
}
9883

9984
impl Import {
100-
fn exec(&self) -> Result<(), Error> {
85+
fn exec(&self) -> Result<(), Report> {
10186
let Import {
10287
fund,
10388
fund_goal,
@@ -182,21 +167,22 @@ impl Import {
182167
}
183168
}
184169

185-
fn dump_content_to_file(content: impl Serialize, file_path: &Path) -> Result<(), Error> {
170+
fn dump_content_to_file(content: impl Serialize, file_path: &Path) -> Result<(), Report> {
186171
let writer = jcli_lib::utils::io::open_file_write(&Some(file_path))?;
187-
serde_json::to_writer_pretty(writer, &content).map_err(Error::Serde)
172+
serde_json::to_writer_pretty(writer, &content)?;
173+
Ok(())
188174
}
189175

190-
fn read_json_from_file<T: DeserializeOwned>(file_path: &Path) -> Result<T, Error> {
176+
fn read_json_from_file<T: DeserializeOwned>(file_path: &Path) -> Result<T, Report> {
191177
let reader = io_utils::open_file_read(&Some(file_path))?;
192-
serde_json::from_reader(reader).map_err(Error::Serde)
178+
Ok(serde_json::from_reader(reader)?)
193179
}
194180

195181
fn parse_from_csv(s: &str) -> Filters {
196182
s.split(';').map(|x| x.to_string()).collect()
197183
}
198184

199-
fn read_scores_file(path: &Option<PathBuf>) -> Result<Scores, Error> {
185+
fn read_scores_file(path: &Option<PathBuf>) -> Result<Scores, Report> {
200186
let mut scores = Scores::new();
201187
if let Some(path) = path {
202188
let mut reader = csv::Reader::from_path(path)?;
@@ -218,7 +204,7 @@ fn read_scores_file(path: &Option<PathBuf>) -> Result<Scores, Error> {
218204
Ok(scores)
219205
}
220206

221-
fn read_sponsors_file(path: &Option<PathBuf>) -> Result<Sponsors, Error> {
207+
fn read_sponsors_file(path: &Option<PathBuf>) -> Result<Sponsors, Report> {
222208
let mut sponsors = Sponsors::new();
223209

224210
if let Some(path) = path {

catalyst-toolbox/src/bin/cli/kedqr/decode/img.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@ use catalyst_toolbox::kedqr::QrPin;
44
use chain_crypto::AsymmetricKey;
55
use chain_crypto::Ed25519Extended;
66
use chain_crypto::SecretKey;
7-
use std::error::Error;
7+
use color_eyre::Report;
88
use std::fs::File;
99
use std::io::Write;
1010
use std::path::Path;
1111
use std::path::PathBuf;
1212

13-
pub fn save_secret_from_qr(
14-
qr: PathBuf,
15-
output: Option<PathBuf>,
16-
pin: QrPin,
17-
) -> Result<(), Box<dyn Error>> {
13+
pub fn save_secret_from_qr(qr: PathBuf, output: Option<PathBuf>, pin: QrPin) -> Result<(), Report> {
1814
let sk = secret_from_qr(&qr, pin)?;
1915
let hrp = Ed25519Extended::SECRET_BECH32_HRP;
2016
let secret_key = bech32::encode(hrp, sk.leak_secret().to_base32(), Variant::Bech32)?;
@@ -36,7 +32,7 @@ pub fn save_secret_from_qr(
3632
pub fn secret_from_qr(
3733
qr: impl AsRef<Path>,
3834
pin: QrPin,
39-
) -> Result<SecretKey<Ed25519Extended>, catalyst_toolbox::kedqr::KeyQrCodeError> {
35+
) -> Result<SecretKey<Ed25519Extended>, Report> {
4036
let img = image::open(qr)?;
4137
let secret = KeyQrCode::decode(img, &pin.password)?;
4238
Ok(secret.first().unwrap().clone())

catalyst-toolbox/src/bin/cli/kedqr/decode/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ mod payload;
33

44
use crate::cli::kedqr::QrCodeOpts;
55
use catalyst_toolbox::kedqr::QrPin;
6+
use color_eyre::Report;
67
pub use img::{save_secret_from_qr, secret_from_qr};
78
pub use payload::{decode_payload, secret_from_payload};
8-
use std::error::Error;
99
use std::path::PathBuf;
1010
use structopt::StructOpt;
1111

@@ -28,7 +28,7 @@ pub struct DecodeQrCodeCmd {
2828
}
2929

3030
impl DecodeQrCodeCmd {
31-
pub fn exec(self) -> Result<(), Box<dyn Error>> {
31+
pub fn exec(self) -> Result<(), Report> {
3232
match self.opts {
3333
QrCodeOpts::Payload => decode_payload(self.input, self.output, self.pin),
3434
QrCodeOpts::Img => save_secret_from_qr(self.input, self.output, self.pin),

catalyst-toolbox/src/bin/cli/kedqr/decode/payload.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
use bech32::{ToBase32, Variant};
22
use catalyst_toolbox::kedqr::{decode, QrPin};
33
use chain_crypto::{AsymmetricKey, Ed25519Extended, SecretKey};
4+
use color_eyre::Report;
45
use std::{
5-
error::Error,
66
fs::{File, OpenOptions},
77
io::{BufRead, BufReader, Write},
88
path::{Path, PathBuf},
99
};
1010

11-
pub fn decode_payload(
12-
input: PathBuf,
13-
output: Option<PathBuf>,
14-
pin: QrPin,
15-
) -> Result<(), Box<dyn Error>> {
11+
pub fn decode_payload(input: PathBuf, output: Option<PathBuf>, pin: QrPin) -> Result<(), Report> {
1612
// generate qrcode with key and parsed pin
1713
let secret = secret_from_payload(input, pin)?;
1814
let hrp = Ed25519Extended::SECRET_BECH32_HRP;
@@ -35,7 +31,7 @@ pub fn decode_payload(
3531
pub fn secret_from_payload(
3632
input: impl AsRef<Path>,
3733
pin: QrPin,
38-
) -> Result<SecretKey<Ed25519Extended>, catalyst_toolbox::kedqr::KeyQrCodePayloadError> {
34+
) -> Result<SecretKey<Ed25519Extended>, Report> {
3935
let input = OpenOptions::new()
4036
.create(false)
4137
.read(true)
@@ -54,5 +50,5 @@ pub fn secret_from_payload(
5450
// use parsed pin from args
5551
let pwd = pin.password;
5652
// generate qrcode with key and parsed pin
57-
decode(payload_str, &pwd)
53+
Ok(decode(payload_str, &pwd)?)
5854
}

0 commit comments

Comments
 (0)