util.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. use std::{
  2. fs::File,
  3. io,
  4. io::BufReader,
  5. path::{Path, PathBuf},
  6. };
  7. use chrono::Utc;
  8. use clap::Parser;
  9. use rand::{distributions::Alphanumeric, thread_rng, Rng};
  10. use serde::{de::DeserializeOwned, Deserialize, Serialize};
  11. use darkfi::{
  12. util::{
  13. cli::UrlConfig,
  14. serial::{Decodable, Encodable},
  15. },
  16. Result,
  17. };
  18. pub const CONFIG_FILE_CONTENTS: &[u8] = include_bytes!("../taud_config.toml");
  19. pub fn random_ref_id() -> String {
  20. thread_rng().sample_iter(&Alphanumeric).take(30).map(char::from).collect()
  21. }
  22. pub fn get_current_time() -> Timestamp {
  23. Timestamp(Utc::now().timestamp())
  24. }
  25. pub fn find_free_id(task_ids: &[u32]) -> u32 {
  26. for i in 1.. {
  27. if !task_ids.contains(&i) {
  28. return i
  29. }
  30. }
  31. 1
  32. }
  33. pub fn load<T: DeserializeOwned>(path: &Path) -> Result<T> {
  34. let file = File::open(path)?;
  35. let reader = BufReader::new(file);
  36. let value: T = serde_json::from_reader(reader)?;
  37. Ok(value)
  38. }
  39. pub fn save<T: Serialize>(path: &Path, value: &T) -> Result<()> {
  40. let file = File::create(path)?;
  41. serde_json::to_writer_pretty(file, value)?;
  42. Ok(())
  43. }
  44. #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
  45. pub struct Settings {
  46. pub dataset_path: PathBuf,
  47. }
  48. impl Default for Settings {
  49. fn default() -> Self {
  50. Self { dataset_path: PathBuf::from("") }
  51. }
  52. }
  53. #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, PartialOrd)]
  54. pub struct Timestamp(pub i64);
  55. impl Encodable for Timestamp {
  56. fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
  57. let mut len = 0;
  58. len += self.0.encode(&mut s)?;
  59. Ok(len)
  60. }
  61. }
  62. impl Decodable for Timestamp {
  63. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  64. Ok(Self(Decodable::decode(&mut d)?))
  65. }
  66. }
  67. /// taud cli
  68. #[derive(Parser)]
  69. #[clap(name = "taud")]
  70. pub struct CliTaud {
  71. /// Sets a custom config file
  72. #[clap(short, long)]
  73. pub config: Option<String>,
  74. /// Increase verbosity
  75. #[clap(short, parse(from_occurrences))]
  76. pub verbose: u8,
  77. }
  78. #[derive(Clone, Debug, Serialize, Deserialize)]
  79. pub struct TauConfig {
  80. /// path to dataset
  81. pub dataset_path: String,
  82. /// Path to DER-formatted PKCS#12 archive. (used only with tls listener url)
  83. pub tls_identity_path: String,
  84. /// The address where taud should bind its RPC socket
  85. pub rpc_listener_url: UrlConfig,
  86. }
  87. #[cfg(test)]
  88. mod tests {
  89. use super::*;
  90. #[test]
  91. fn find_free_id_test() -> Result<()> {
  92. let mut ids: Vec<u32> = vec![1, 3, 8, 9, 10, 3];
  93. let ids_empty: Vec<u32> = vec![];
  94. let ids_duplicate: Vec<u32> = vec![1; 100];
  95. let find_id = find_free_id(&ids);
  96. assert_eq!(find_id, 2);
  97. ids.push(find_id);
  98. assert_eq!(find_free_id(&ids), 4);
  99. assert_eq!(find_free_id(&ids_empty), 1);
  100. assert_eq!(find_free_id(&ids_duplicate), 2);
  101. Ok(())
  102. }
  103. }