| 1 | use std::path::Path; |
| 2 | |
| 3 | use crate::config::Config; |
| 4 | use crate::tui::app::App; |
| 5 | |
| 6 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 7 | pub(super) struct SetupPersistenceFacts { |
| 8 | pub(super) home_result: String, |
| 9 | pub(super) config_result: String, |
| 10 | pub(super) state_result: String, |
| 11 | pub(super) constitution_result: String, |
| 12 | pub(super) memory_result: String, |
| 13 | pub(super) notes_result: String, |
| 14 | pub(super) result: String, |
| 15 | } |
| 16 | |
| 17 | impl Default for SetupPersistenceFacts { |
| 18 | fn default() -> Self { |
| 19 | Self { |
| 20 | home_result: "CODEWHALE_HOME not loaded".to_string(), |
| 21 | config_result: "config path not loaded".to_string(), |
| 22 | state_result: "setup state path not loaded".to_string(), |
| 23 | constitution_result: "constitution path not loaded".to_string(), |
| 24 | memory_result: "memory path not loaded".to_string(), |
| 25 | notes_result: "notes path not loaded".to_string(), |
| 26 | result: "persistence paths not loaded".to_string(), |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | impl SetupPersistenceFacts { |
| 32 | pub(super) fn from_app_config(app: &App, config: &Config, codewhale_home: &Path) -> Self { |
| 33 | let home_source = if codewhale_config::codewhale_home_is_explicit() { |
| 34 | "explicit" |
| 35 | } else { |
| 36 | "default" |
| 37 | }; |
| 38 | let home_presence = dir_presence(codewhale_home); |
| 39 | let config_path = codewhale_config::resolve_config_path(app.config_path.clone()) |
| 40 | .unwrap_or_else(|_| codewhale_home.join("config.toml")); |
| 41 | let state_path = codewhale_config::SetupState::path().unwrap_or_else(|_| { |
| 42 | codewhale_home.join(codewhale_config::setup_state::SETUP_STATE_FILE_NAME) |
| 43 | }); |
| 44 | let constitution_path = codewhale_config::UserConstitution::path() |
| 45 | .unwrap_or_else(|_| codewhale_home.join("constitution.json")); |
| 46 | let memory_path = config.memory_path(); |
| 47 | let notes_path = config.notes_path(); |
| 48 | |
| 49 | let config_presence = file_presence(&config_path); |
| 50 | let state_presence = file_presence(&state_path); |
| 51 | let constitution_presence = file_presence(&constitution_path); |
| 52 | let memory_presence = file_presence(&memory_path); |
| 53 | let notes_presence = file_presence(¬es_path); |
| 54 | |
| 55 | Self { |
| 56 | home_result: format!( |
| 57 | "{home_source} CODEWHALE_HOME at {} ({home_presence})", |
| 58 | codewhale_home.display() |
| 59 | ), |
| 60 | config_result: path_result(&config_path, config_presence), |
| 61 | state_result: path_result(&state_path, state_presence), |
| 62 | constitution_result: path_result(&constitution_path, constitution_presence), |
| 63 | memory_result: path_result(&memory_path, memory_presence), |
| 64 | notes_result: path_result(¬es_path, notes_presence), |
| 65 | result: format!( |
| 66 | "home_source={home_source}, home={home_presence}, config={config_presence}, setup_state={state_presence}, constitution={constitution_presence}, memory={memory_presence}, notes={notes_presence}, mode=read_only_review" |
| 67 | ), |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | fn path_result(path: &Path, presence: &'static str) -> String { |
| 73 | format!("{} ({presence})", path.display()) |
| 74 | } |
| 75 | |
| 76 | fn dir_presence(path: &Path) -> &'static str { |
| 77 | if path.is_dir() { |
| 78 | "present" |
| 79 | } else if path.exists() { |
| 80 | "exists-not-dir" |
| 81 | } else { |
| 82 | "missing" |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | fn file_presence(path: &Path) -> &'static str { |
| 87 | if path.is_file() { |
| 88 | "present" |
| 89 | } else if path.exists() { |
| 90 | "exists-not-file" |
| 91 | } else { |
| 92 | "missing" |
| 93 | } |
| 94 | } |
| 95 |