flowy_toml.rs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. use std::fs;
  2. #[derive(serde::Deserialize)]
  3. pub struct FlowyConfig {
  4. pub proto_crates: Vec<String>,
  5. pub event_files: Vec<String>,
  6. }
  7. impl FlowyConfig {
  8. pub fn from_toml_file(path: &str) -> Self {
  9. let content = fs::read_to_string(path).unwrap();
  10. let config: FlowyConfig = toml::from_str(content.as_ref()).unwrap();
  11. config
  12. }
  13. }
  14. pub struct CrateConfig {
  15. pub(crate) crate_path: String,
  16. pub(crate) folder_name: String,
  17. pub(crate) flowy_config: FlowyConfig,
  18. }
  19. impl CrateConfig {
  20. pub fn proto_paths(&self) -> Vec<String> {
  21. let proto_paths = self
  22. .flowy_config
  23. .proto_crates
  24. .iter()
  25. .map(|name| format!("{}/{}", self.crate_path, name))
  26. .collect::<Vec<String>>();
  27. proto_paths
  28. }
  29. }
  30. pub fn parse_crate_config_from(entry: &walkdir::DirEntry) -> Option<CrateConfig> {
  31. let path = entry.path().parent().unwrap();
  32. let crate_path = path.to_str().unwrap().to_string();
  33. let folder_name = path.file_stem().unwrap().to_str().unwrap().to_string();
  34. let config_path = format!("{}/Flowy.toml", crate_path);
  35. if !std::path::Path::new(&config_path).exists() {
  36. return None;
  37. }
  38. let flowy_config = FlowyConfig::from_toml_file(config_path.as_ref());
  39. Some(CrateConfig {
  40. crate_path,
  41. folder_name,
  42. flowy_config,
  43. })
  44. }