flowy_toml.rs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. use std::fs;
  2. use std::path::{Path, PathBuf};
  3. #[derive(serde::Deserialize, Clone, Debug)]
  4. pub struct FlowyConfig {
  5. #[serde(default)]
  6. pub event_files: Vec<String>,
  7. // Collect AST from the file or directory specified by proto_input to generate the proto files.
  8. #[serde(default)]
  9. pub proto_input: Vec<String>,
  10. // Output path for the generated proto files. The default value is default_proto_output()
  11. #[serde(default = "default_proto_output")]
  12. pub proto_output: String,
  13. // Create a crate that stores the generated protobuf Rust structures. The default value is default_protobuf_crate()
  14. #[serde(default = "default_protobuf_crate")]
  15. pub protobuf_crate_path: String,
  16. }
  17. fn default_proto_output() -> String {
  18. let mut path = PathBuf::from("resources");
  19. path.push("proto");
  20. path.to_str().unwrap().to_owned()
  21. }
  22. fn default_protobuf_crate() -> String {
  23. let mut path = PathBuf::from("src");
  24. path.push("protobuf");
  25. path.to_str().unwrap().to_owned()
  26. }
  27. impl FlowyConfig {
  28. pub fn from_toml_file(path: &Path) -> Self {
  29. let content = fs::read_to_string(path).unwrap();
  30. let config: FlowyConfig = toml::from_str(content.as_ref()).unwrap();
  31. config
  32. }
  33. }
  34. pub struct CrateConfig {
  35. pub crate_path: PathBuf,
  36. pub crate_folder: String,
  37. pub flowy_config: FlowyConfig,
  38. }
  39. pub fn parse_crate_config_from(entry: &walkdir::DirEntry) -> Option<CrateConfig> {
  40. let mut config_path = entry.path().parent().unwrap().to_path_buf();
  41. config_path.push("Flowy.toml");
  42. if !config_path.as_path().exists() {
  43. return None;
  44. }
  45. let crate_path = entry.path().parent().unwrap().to_path_buf();
  46. let flowy_config = FlowyConfig::from_toml_file(config_path.as_path());
  47. let crate_folder = crate_path
  48. .file_stem()
  49. .unwrap()
  50. .to_str()
  51. .unwrap()
  52. .to_string();
  53. Some(CrateConfig {
  54. crate_path,
  55. crate_folder,
  56. flowy_config,
  57. })
  58. }