Rename the config struct `Config`

pull/182/head
Franck Royer 3 years ago
parent fadbf1638a
commit 45dccb8be2
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4

@ -1,6 +1,6 @@
use crate::fs::ensure_directory_exists; use crate::fs::ensure_directory_exists;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use config::{Config, ConfigError}; use config::ConfigError;
use dialoguer::{theme::ColorfulTheme, Input}; use dialoguer::{theme::ColorfulTheme, Input};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{ use std::{
@ -19,19 +19,19 @@ const DEFAULT_BITCOIND_TESTNET_URL: &str = "http://127.0.0.1:18332";
const DEFAULT_MONERO_WALLET_RPC_TESTNET_URL: &str = "http://127.0.0.1:38083/json_rpc"; const DEFAULT_MONERO_WALLET_RPC_TESTNET_URL: &str = "http://127.0.0.1:38083/json_rpc";
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq)] #[derive(Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq)]
pub struct File { pub struct Config {
pub bitcoin: Bitcoin, pub bitcoin: Bitcoin,
pub monero: Monero, pub monero: Monero,
} }
impl File { impl Config {
pub fn read<D>(config_file: D) -> Result<Self, ConfigError> pub fn read<D>(config_file: D) -> Result<Self, ConfigError>
where where
D: AsRef<OsStr>, D: AsRef<OsStr>,
{ {
let config_file = Path::new(&config_file); let config_file = Path::new(&config_file);
let mut config = Config::new(); let mut config = config::Config::new();
config.merge(config::File::from(config_file))?; config.merge(config::File::from(config_file))?;
config.try_into() config.try_into()
} }
@ -54,7 +54,7 @@ pub struct Monero {
#[error("config not initialized")] #[error("config not initialized")]
pub struct ConfigNotInitialized {} pub struct ConfigNotInitialized {}
pub fn read_config(config_path: PathBuf) -> Result<Result<File, ConfigNotInitialized>> { pub fn read_config(config_path: PathBuf) -> Result<Result<Config, ConfigNotInitialized>> {
if config_path.exists() { if config_path.exists() {
info!( info!(
"Using config file at default path: {}", "Using config file at default path: {}",
@ -64,7 +64,7 @@ pub fn read_config(config_path: PathBuf) -> Result<Result<File, ConfigNotInitial
return Ok(Err(ConfigNotInitialized {})); return Ok(Err(ConfigNotInitialized {}));
} }
let file = File::read(&config_path) let file = Config::read(&config_path)
.with_context(|| format!("failed to read config file {}", config_path.display()))?; .with_context(|| format!("failed to read config file {}", config_path.display()))?;
Ok(Ok(file)) Ok(Ok(file))
@ -72,7 +72,7 @@ pub fn read_config(config_path: PathBuf) -> Result<Result<File, ConfigNotInitial
pub fn initial_setup<F>(config_path: PathBuf, config_file: F) -> Result<()> pub fn initial_setup<F>(config_path: PathBuf, config_file: F) -> Result<()>
where where
F: Fn() -> Result<File>, F: Fn() -> Result<Config>,
{ {
info!("Config file not found, running initial setup..."); info!("Config file not found, running initial setup...");
ensure_directory_exists(config_path.as_path())?; ensure_directory_exists(config_path.as_path())?;
@ -88,7 +88,7 @@ where
Ok(()) Ok(())
} }
pub fn query_user_for_initial_testnet_config() -> Result<File> { pub fn query_user_for_initial_testnet_config() -> Result<Config> {
println!(); println!();
let bitcoind_url: String = Input::with_theme(&ColorfulTheme::default()) let bitcoind_url: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Enter Bitcoind URL (including username and password if applicable) or hit return to use default") .with_prompt("Enter Bitcoind URL (including username and password if applicable) or hit return to use default")
@ -107,7 +107,7 @@ pub fn query_user_for_initial_testnet_config() -> Result<File> {
let monero_wallet_rpc_url = Url::parse(monero_wallet_rpc_url.as_str())?; let monero_wallet_rpc_url = Url::parse(monero_wallet_rpc_url.as_str())?;
println!(); println!();
Ok(File { Ok(Config {
bitcoin: Bitcoin { bitcoin: Bitcoin {
bitcoind_url, bitcoind_url,
wallet_name: bitcoin_wallet_name, wallet_name: bitcoin_wallet_name,
@ -129,7 +129,7 @@ mod tests {
let temp_dir = tempdir().unwrap().path().to_path_buf(); let temp_dir = tempdir().unwrap().path().to_path_buf();
let config_path = Path::join(&temp_dir, "config.toml"); let config_path = Path::join(&temp_dir, "config.toml");
let expected = File { let expected = Config {
bitcoin: Bitcoin { bitcoin: Bitcoin {
bitcoind_url: Url::from_str("http://127.0.0.1:18332").unwrap(), bitcoind_url: Url::from_str("http://127.0.0.1:18332").unwrap(),
wallet_name: "alice".to_string(), wallet_name: "alice".to_string(),

Loading…
Cancel
Save