cli_config.rs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. use crate::{Error, Result};
  2. use serde::de::DeserializeOwned;
  3. use serde::{Deserialize, Serialize};
  4. use std::marker::PhantomData;
  5. use std::{
  6. fs,
  7. path::{Path, PathBuf},
  8. str,
  9. };
  10. #[derive(Clone, Default)]
  11. pub struct Config<T> {
  12. config: PhantomData<T>,
  13. }
  14. impl<T: Serialize + DeserializeOwned> Config<T> {
  15. pub fn load(path: PathBuf) -> Result<T> {
  16. if Path::new(&path).exists() {
  17. let toml = fs::read(&path)?;
  18. let str_buff = str::from_utf8(&toml)?;
  19. let config: T = toml::from_str(str_buff.clone())?;
  20. Ok(config)
  21. } else {
  22. println!("No config files were found in .config/darkfi. Please follow the instructions in the README and add default configs.");
  23. Err(Error::ConfigNotFound)
  24. }
  25. }
  26. }
  27. #[derive(Serialize, Deserialize, Debug)]
  28. pub struct DrkConfig {
  29. pub rpc_url: String,
  30. pub log_path: String,
  31. }
  32. #[derive(Serialize, Deserialize, Debug)]
  33. pub struct GatewaydConfig {
  34. #[serde(rename = "connect_url")]
  35. pub accept_url: String,
  36. #[serde(rename = "publisher_url")]
  37. pub publisher_url: String,
  38. #[serde(rename = "log_path")]
  39. pub log_path: String,
  40. }
  41. #[derive(Serialize, Deserialize, Debug)]
  42. pub struct CashierdConfig {
  43. #[serde(rename = "accept_url")]
  44. pub accept_url: String,
  45. #[serde(rename = "rpc_url")]
  46. pub rpc_url: String,
  47. #[serde(rename = "gateway_url")]
  48. pub gateway_url: String,
  49. #[serde(rename = "log_path")]
  50. pub log_path: String,
  51. #[serde(rename = "password")]
  52. pub password: String,
  53. #[serde(rename = "client_password")]
  54. pub client_password: String,
  55. }