AppFlowy/shared-lib/lib-infra/src/code_gen/flowy_toml.rs

42 lines
1.2 KiB
Rust
Raw Normal View History

use std::fs;
2022-02-17 12:10:29 +00:00
use std::path::{Path, PathBuf};
#[derive(serde::Deserialize, Clone, Debug)]
pub struct FlowyConfig {
pub event_files: Vec<String>,
pub proto_rust_file_input_dir: Vec<String>,
pub proto_file_output_dir: String,
pub protobuf_crate_output_dir: String,
}
impl FlowyConfig {
2022-02-17 12:10:29 +00:00
pub fn from_toml_file(path: &Path) -> Self {
let content = fs::read_to_string(path).unwrap();
let config: FlowyConfig = toml::from_str(content.as_ref()).unwrap();
config
}
}
pub struct CrateConfig {
2022-02-17 12:10:29 +00:00
pub crate_path: PathBuf,
pub crate_folder: String,
2022-02-15 13:27:00 +00:00
pub flowy_config: FlowyConfig,
}
pub fn parse_crate_config_from(entry: &walkdir::DirEntry) -> Option<CrateConfig> {
2022-02-17 12:10:29 +00:00
let mut config_path = entry.path().parent().unwrap().to_path_buf();
config_path.push("Flowy.toml");
if !config_path.as_path().exists() {
return None;
}
2022-02-17 12:10:29 +00:00
let crate_path = entry.path().parent().unwrap().to_path_buf();
let flowy_config = FlowyConfig::from_toml_file(config_path.as_path());
let crate_folder = crate_path.file_stem().unwrap().to_str().unwrap().to_string();
Some(CrateConfig {
crate_path,
crate_folder,
flowy_config,
})
}