mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-04-20 16:06:00 -04:00
Use nested Result instead of Error enum
This commit is contained in:
parent
616d120377
commit
345c57915e
@ -49,27 +49,24 @@ pub struct Monero {
|
||||
pub wallet_rpc_url: Url,
|
||||
}
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("config not initialized")]
|
||||
ConfigNotInitialized,
|
||||
#[error("other error")]
|
||||
Other(#[from] anyhow::Error),
|
||||
}
|
||||
#[derive(thiserror::Error, Debug, Clone, Copy)]
|
||||
#[error("config not initialized")]
|
||||
pub struct ConfigNotInitialized {}
|
||||
|
||||
pub fn read_config(config_path: PathBuf) -> anyhow::Result<File, Error> {
|
||||
pub fn read_config(config_path: PathBuf) -> Result<Result<File, ConfigNotInitialized>> {
|
||||
if config_path.exists() {
|
||||
info!(
|
||||
"Using config file at default path: {}",
|
||||
config_path.display()
|
||||
);
|
||||
} else {
|
||||
return Err(Error::ConfigNotInitialized);
|
||||
return Ok(Err(ConfigNotInitialized {}));
|
||||
}
|
||||
|
||||
File::read(&config_path)
|
||||
.with_context(|| format!("failed to read config file {}", config_path.display()))
|
||||
.map_err(Error::Other)
|
||||
let file = File::read(&config_path)
|
||||
.with_context(|| format!("failed to read config file {}", config_path.display()))?;
|
||||
|
||||
Ok(Ok(file))
|
||||
}
|
||||
|
||||
pub fn initial_setup<F>(config_path: PathBuf, config_file: F) -> Result<()>
|
||||
@ -142,7 +139,7 @@ mod tests {
|
||||
};
|
||||
|
||||
initial_setup(config_path.clone(), || Ok(expected.clone())).unwrap();
|
||||
let actual = read_config(config_path).unwrap();
|
||||
let actual = read_config(config_path).unwrap().unwrap();
|
||||
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
@ -14,9 +14,11 @@
|
||||
|
||||
use crate::{
|
||||
cli::{Command, Options, Resume},
|
||||
config::read_config,
|
||||
config::{
|
||||
initial_setup, query_user_for_initial_testnet_config, read_config, ConfigNotInitialized,
|
||||
},
|
||||
};
|
||||
use anyhow::{bail, Context, Result};
|
||||
use anyhow::{Context, Result};
|
||||
use database::Database;
|
||||
use fs::default_config_path;
|
||||
use prettytable::{row, Table};
|
||||
@ -24,7 +26,6 @@ use protocol::{alice, bob, bob::Builder, SwapAmounts};
|
||||
use settings::Settings;
|
||||
use std::sync::Arc;
|
||||
use structopt::StructOpt;
|
||||
use swap::config::{initial_setup, query_user_for_initial_testnet_config};
|
||||
use trace::init_tracing;
|
||||
use tracing::{info, log::LevelFilter};
|
||||
use uuid::Uuid;
|
||||
@ -206,13 +207,12 @@ async fn main() -> Result<()> {
|
||||
|
||||
fn init_settings() -> Result<Settings> {
|
||||
let config_path = default_config_path()?;
|
||||
let config = match read_config(config_path.clone()) {
|
||||
let config = match read_config(config_path.clone())? {
|
||||
Ok(config) => config,
|
||||
Err(config::Error::ConfigNotInitialized) => {
|
||||
Err(ConfigNotInitialized {}) => {
|
||||
initial_setup(config_path.clone(), query_user_for_initial_testnet_config)?;
|
||||
read_config(config_path)?
|
||||
read_config(config_path)?.expect("after initial setup config can be read")
|
||||
}
|
||||
Err(e) => bail!(e),
|
||||
};
|
||||
|
||||
let settings = Settings::from_config_file_and_defaults(config);
|
||||
|
Loading…
x
Reference in New Issue
Block a user