cli_config.rs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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(Clone, 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 = "database_path")]
  43. pub database_path: String,
  44. #[serde(rename = "walletdb_path")]
  45. pub walletdb_path: String,
  46. #[serde(rename = "log_path")]
  47. pub log_path: String,
  48. #[serde(rename = "password")]
  49. pub password: String,
  50. }
  51. #[derive(Serialize, Deserialize, Debug)]
  52. pub struct GatewaydConfig {
  53. #[serde(rename = "connect_url")]
  54. pub accept_url: String,
  55. #[serde(rename = "publisher_url")]
  56. pub publisher_url: String,
  57. #[serde(rename = "log_path")]
  58. pub log_path: String,
  59. }
  60. #[derive(Clone, Serialize, Deserialize, Debug)]
  61. pub struct CashierdConfig {
  62. #[serde(rename = "accept_url")]
  63. pub accept_url: String,
  64. #[serde(rename = "rpc_url")]
  65. pub rpc_url: String,
  66. #[serde(rename = "gateway_url")]
  67. pub gateway_url: String,
  68. #[serde(rename = "gateway_subscriber_url")]
  69. pub gateway_subscriber_url: String,
  70. #[serde(rename = "log_path")]
  71. pub log_path: String,
  72. #[serde(rename = "database_path")]
  73. pub database_path: String,
  74. #[serde(rename = "cashierdb_path")]
  75. pub cashierdb_path: String,
  76. #[serde(rename = "password")]
  77. pub password: String,
  78. #[serde(rename = "client_password")]
  79. pub client_password: String,
  80. }