flowy_toml.rs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. "resources/proto".to_owned()
  19. }
  20. fn default_protobuf_crate() -> String {
  21. "src/protobuf".to_owned()
  22. }
  23. impl FlowyConfig {
  24. pub fn from_toml_file(path: &Path) -> Self {
  25. let content = fs::read_to_string(path).unwrap();
  26. let config: FlowyConfig = toml::from_str(content.as_ref()).unwrap();
  27. config
  28. }
  29. }
  30. pub struct CrateConfig {
  31. pub crate_path: PathBuf,
  32. pub crate_folder: String,
  33. pub flowy_config: FlowyConfig,
  34. }
  35. pub fn parse_crate_config_from(entry: &walkdir::DirEntry) -> Option<CrateConfig> {
  36. let mut config_path = entry.path().parent().unwrap().to_path_buf();
  37. config_path.push("Flowy.toml");
  38. if !config_path.as_path().exists() {
  39. return None;
  40. }
  41. let crate_path = entry.path().parent().unwrap().to_path_buf();
  42. let flowy_config = FlowyConfig::from_toml_file(config_path.as_path());
  43. let crate_folder = crate_path.file_stem().unwrap().to_str().unwrap().to_string();
  44. Some(CrateConfig {
  45. crate_path,
  46. crate_folder,
  47. flowy_config,
  48. })
  49. }