cli_config.rs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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(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 DarkfidConfig {
  34. #[serde(rename = "connect_url")]
  35. pub connect_url: String,
  36. #[serde(rename = "subscriber_url")]
  37. pub subscriber_url: String,
  38. #[serde(rename = "cashier_url")]
  39. pub cashier_url: String,
  40. #[serde(rename = "rpc_url")]
  41. pub rpc_url: String,
  42. #[serde(rename = "log_path")]
  43. pub log_path: String,
  44. #[serde(rename = "password")]
  45. pub password: String,
  46. }
  47. #[derive(Serialize, Deserialize, Debug)]
  48. pub struct GatewaydConfig {
  49. #[serde(rename = "connect_url")]
  50. pub accept_url: String,
  51. #[serde(rename = "publisher_url")]
  52. pub publisher_url: String,
  53. #[serde(rename = "log_path")]
  54. pub log_path: String,
  55. }
  56. #[derive(Serialize, Deserialize, Debug)]
  57. pub struct CashierdConfig {
  58. #[serde(rename = "accept_url")]
  59. pub accept_url: String,
  60. #[serde(rename = "rpc_url")]
  61. pub rpc_url: String,
  62. #[serde(rename = "gateway_url")]
  63. pub gateway_url: String,
  64. #[serde(rename = "log_path")]
  65. pub log_path: String,
  66. #[serde(rename = "password")]
  67. pub password: String,
  68. #[serde(rename = "client_password")]
  69. pub client_password: String,
  70. }