Remove config and fs from the lib

The settings module has to be part of the lib because settings are used in the test as well.
Config is only used in main, thus it should not leak into the lib.
Note that this allows to remove the dead_code allowance in fs.
This commit is contained in:
Daniel Karzel 2021-01-29 11:31:34 +11:00
parent 9be6449e49
commit eb34adc45d
5 changed files with 19 additions and 20 deletions

View File

@ -1,4 +1,4 @@
use crate::fs::ensure_directory_exists;
use crate::{fs::ensure_directory_exists, settings::Settings};
use anyhow::{Context, Result};
use config::{Config, ConfigError};
use dialoguer::{theme::ColorfulTheme, Input};
@ -17,7 +17,6 @@ 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";
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct File {
pub bitcoin: Bitcoin,
pub monero: Monero,
@ -117,6 +116,14 @@ pub fn query_user_for_initial_testnet_config() -> Result<File> {
})
}
pub fn settings_from_config_file_and_defaults(config: File) -> Settings {
Settings::testnet(
config.bitcoin.bitcoind_url,
config.bitcoin.wallet_name,
config.monero.wallet_rpc_url,
)
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -5,12 +5,10 @@ use std::path::{Path, PathBuf};
/// This is to store the configuration and seed files
// Linux: /home/<user>/.config/xmr-btc-swap/
// OSX: /Users/<user>/Library/Preferences/xmr-btc-swap/
#[allow(dead_code)]
fn default_config_dir() -> Option<PathBuf> {
ProjectDirs::from("", "", "xmr-btc-swap").map(|proj_dirs| proj_dirs.config_dir().to_path_buf())
}
#[allow(dead_code)]
pub fn default_config_path() -> anyhow::Result<PathBuf> {
default_config_dir()
.map(|dir| Path::join(&dir, "config.toml"))
@ -20,7 +18,6 @@ pub fn default_config_path() -> anyhow::Result<PathBuf> {
/// This is to store the DB
// Linux: /home/<user>/.local/share/nectar/
// OSX: /Users/<user>/Library/Application Support/nectar/
#[allow(dead_code)]
pub fn default_data_dir() -> Option<std::path::PathBuf> {
ProjectDirs::from("", "", "nectar").map(|proj_dirs| proj_dirs.data_dir().to_path_buf())
}

View File

@ -17,7 +17,6 @@
)]
pub mod bitcoin;
pub mod config;
pub mod database;
pub mod monero;
pub mod network;
@ -26,5 +25,4 @@ pub mod seed;
pub mod settings;
pub mod trace;
mod fs;
mod serde_peer_id;

View File

@ -15,15 +15,16 @@
use crate::{
cli::{Command, Options, Resume},
config::{
initial_setup, query_user_for_initial_testnet_config, read_config, ConfigNotInitialized,
initial_setup, query_user_for_initial_testnet_config, read_config,
settings_from_config_file_and_defaults, ConfigNotInitialized,
},
settings::Settings,
};
use anyhow::{Context, Result};
use database::Database;
use fs::{default_config_path, default_data_dir};
use prettytable::{row, Table};
use protocol::{alice, bob, bob::Builder, SwapAmounts};
use settings::Settings;
use std::{path::PathBuf, sync::Arc};
use structopt::StructOpt;
use trace::init_tracing;
@ -229,7 +230,7 @@ fn init_settings(config_path: Option<PathBuf>) -> Result<Settings> {
}
};
let settings = Settings::from_config_file_and_defaults(config);
let settings = settings_from_config_file_and_defaults(config);
Ok(settings)
}

View File

@ -1,4 +1,4 @@
use crate::{bitcoin::Timelock, config::File};
use crate::bitcoin::Timelock;
use conquer_once::Lazy;
use std::time::Duration;
use url::Url;
@ -9,15 +9,11 @@ pub struct Settings {
}
impl Settings {
pub fn from_config_file_and_defaults(config: File) -> Self {
Settings::testnet(
config.bitcoin.bitcoind_url,
config.bitcoin.wallet_name,
config.monero.wallet_rpc_url,
)
}
fn testnet(bitcoind_url: Url, bitcoin_wallet_name: String, monero_wallet_rpc_url: Url) -> Self {
pub fn testnet(
bitcoind_url: Url,
bitcoin_wallet_name: String,
monero_wallet_rpc_url: Url,
) -> Self {
Self {
wallets: Wallets::testnet(bitcoind_url, bitcoin_wallet_name, monero_wallet_rpc_url),
protocol: Protocol::testnet(),