flowy_toml.rs 1.4 KB

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