util.rs 2.5 KB

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