| 24 | } |
| 25 | |
| 26 | pub fn read_config() -> Result<Config, AppError> { |
| 27 | let paths = fw_path()?; |
| 28 | |
| 29 | let settings_raw = read_to_string(&paths.settings) |
| 30 | .map_err(|e| AppError::RuntimeError(format!("Could not read settings file ({}): {}", paths.settings.to_string_lossy(), e)))?; |
| 31 | |
| 32 | let settings: PersistedSettings = toml::from_str(&settings_raw)?; |
| 33 | |
| 34 | let mut projects: BTreeMap<String, Project> = BTreeMap::new(); |
| 35 | if paths.projects.exists() { |
| 36 | for maybe_project_file in WalkDir::new(&paths.projects).follow_links(true) { |
| 37 | let project_file = maybe_project_file?; |
| 38 | if project_file.metadata()?.is_file() && !project_file.file_name().to_os_string().eq(".DS_Store") { |
| 39 | let raw_project = read_to_string(project_file.path())?; |
| 40 | let mut project: Project = match toml::from_str(&raw_project) { |
| 41 | o @ Ok(_) => o, |
| 42 | e @ Err(_) => { |
| 43 | eprintln!("There is an issue in your config for project {}", project_file.file_name().to_string_lossy()); |
| 44 | e |
| 45 | } |
| 46 | }?; |
| 47 | |
| 48 | project.name = project_file |
| 49 | .file_name() |
| 50 | .to_str() |
| 51 | .map(ToOwned::to_owned) |
| 52 | .ok_or(AppError::InternalError("Failed to get project name"))?; |
| 53 | project.project_config_path = PathBuf::from(project_file.path().parent().ok_or(AppError::InternalError("Expected file to have a parent"))?) |
| 54 | .strip_prefix(paths.projects.as_path()) |
| 55 | .map_err(|e| AppError::RuntimeError(format!("Failed to strip prefix: {}", e)))? |
| 56 | .to_string_lossy() |
| 57 | .to_string(); |
| 58 | if projects.contains_key(&project.name) { |
| 59 | eprintln!( |
| 60 | "Inconsistency found: project {} defined more than once. Will use the project that is found last. Results might be inconsistent.", |
| 61 | project.name |
| 62 | ); |
| 63 | } |
| 64 | projects.insert(project.name.clone(), project); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | let mut tags: BTreeMap<String, Tag> = BTreeMap::new(); |
| 70 | if paths.tags.exists() { |
| 71 | for maybe_tag_file in WalkDir::new(&paths.tags).follow_links(true) { |
| 72 | let tag_file = maybe_tag_file?; |
| 73 | |
| 74 | if tag_file.metadata()?.is_file() && !tag_file.file_name().to_os_string().eq(".DS_Store") { |
| 75 | let raw_tag = read_to_string(tag_file.path())?; |
| 76 | let mut tag: Tag = toml::from_str(&raw_tag)?; |
| 77 | let tag_name: String = tag_file |
| 78 | .file_name() |
| 79 | .to_str() |
| 80 | .map(ToOwned::to_owned) |
| 81 | .ok_or(AppError::InternalError("Failed to get tag name"))?; |
| 82 | tag.tag_config_path = PathBuf::from(tag_file.path().parent().ok_or(AppError::InternalError("Expected file to have a parent"))?) |
| 83 | .strip_prefix(paths.tags.as_path()) |