|
| 1 | +use std::{ |
| 2 | + fs, |
| 3 | + io, |
| 4 | + path::Path, |
| 5 | +}; |
| 6 | + |
| 7 | +use serde::{ |
| 8 | + Deserialize, |
| 9 | + Serialize, |
| 10 | +}; |
| 11 | + |
| 12 | +#[derive(Deserialize, Serialize, Debug, Clone)] |
| 13 | +pub struct LocalConfig { |
| 14 | + pub problem_id: u32, |
| 15 | + pub problem_name: String, |
| 16 | + pub language: String, |
| 17 | +} |
| 18 | + |
| 19 | +impl LocalConfig { |
| 20 | + pub fn new( |
| 21 | + problem_id: u32, problem_name: String, language: String, |
| 22 | + ) -> Self { |
| 23 | + Self { problem_id, problem_name, language } |
| 24 | + } |
| 25 | + |
| 26 | + /// Find and read local config from current directory or parent directories |
| 27 | + pub fn find_and_read() -> io::Result<Option<Self>> { |
| 28 | + let mut current_dir = std::env::current_dir()?; |
| 29 | + |
| 30 | + loop { |
| 31 | + let config_path = current_dir.join(".leetcode-cli"); |
| 32 | + if config_path.exists() { |
| 33 | + let content = fs::read_to_string(&config_path)?; |
| 34 | + let config: LocalConfig = |
| 35 | + toml::from_str(&content).map_err(|e| { |
| 36 | + io::Error::new(io::ErrorKind::InvalidData, e) |
| 37 | + })?; |
| 38 | + return Ok(Some(config)); |
| 39 | + } |
| 40 | + |
| 41 | + if !current_dir.pop() { |
| 42 | + break; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + Ok(None) |
| 47 | + } |
| 48 | + |
| 49 | + /// Write local config to specified directory |
| 50 | + pub fn write_to_dir(&self, dir: &Path) -> io::Result<()> { |
| 51 | + let config_path = dir.join(".leetcode-cli"); |
| 52 | + let content = toml::to_string(self) |
| 53 | + .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; |
| 54 | + fs::write(config_path, content) |
| 55 | + } |
| 56 | + |
| 57 | + /// Read local config from specified file path |
| 58 | + pub fn read_from_path(path: &Path) -> io::Result<Self> { |
| 59 | + let content = fs::read_to_string(path)?; |
| 60 | + toml::from_str(&content) |
| 61 | + .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) |
| 62 | + } |
| 63 | + |
| 64 | + /// Get the main source file name based on language |
| 65 | + pub fn get_main_file(&self) -> String { |
| 66 | + match self.language.to_lowercase().as_str() { |
| 67 | + "rust" => "main.rs".to_string(), |
| 68 | + "python" | "python3" => "main.py".to_string(), |
| 69 | + "javascript" => "main.js".to_string(), |
| 70 | + "typescript" => "main.ts".to_string(), |
| 71 | + "go" => "main.go".to_string(), |
| 72 | + "java" => "Main.java".to_string(), |
| 73 | + "c++" => "main.cpp".to_string(), |
| 74 | + "c" => "main.c".to_string(), |
| 75 | + _ => "main.txt".to_string(), |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /// Resolve problem ID and file path from CLI args or local config |
| 80 | + pub fn resolve_problem_params( |
| 81 | + id: Option<u32>, path_to_file: Option<String>, |
| 82 | + ) -> io::Result<(u32, String)> { |
| 83 | + match (id, &path_to_file) { |
| 84 | + (Some(id), Some(path)) => Ok((id, path.clone())), |
| 85 | + _ => { |
| 86 | + // Try to find local config |
| 87 | + match Self::find_and_read()? { |
| 88 | + Some(config) => { |
| 89 | + let problem_id = id.unwrap_or(config.problem_id); |
| 90 | + let file_path = path_to_file.unwrap_or_else(|| { |
| 91 | + format!("src/{}", config.get_main_file()) |
| 92 | + }); |
| 93 | + Ok((problem_id, file_path)) |
| 94 | + }, |
| 95 | + None => { |
| 96 | + if id.is_none() { |
| 97 | + return Err(io::Error::new( |
| 98 | + io::ErrorKind::NotFound, |
| 99 | + "No problem ID provided and no .leetcode-cli \ |
| 100 | + config found. Either provide --id or run \ |
| 101 | + from a problem directory", |
| 102 | + )); |
| 103 | + } |
| 104 | + if path_to_file.is_none() { |
| 105 | + return Err(io::Error::new( |
| 106 | + io::ErrorKind::NotFound, |
| 107 | + "No file path provided", |
| 108 | + )); |
| 109 | + } |
| 110 | + // If we get here, both id and path_to_file must be Some |
| 111 | + match (id, path_to_file) { |
| 112 | + (Some(id), Some(path)) => Ok((id, path)), |
| 113 | + _ => Err(io::Error::other( |
| 114 | + "Unexpected error: id or path_to_file missing \ |
| 115 | + after checks", |
| 116 | + )), |
| 117 | + } |
| 118 | + }, |
| 119 | + } |
| 120 | + }, |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +#[cfg(test)] |
| 126 | +mod tests { |
| 127 | + use tempfile::TempDir; |
| 128 | + |
| 129 | + use super::*; |
| 130 | + |
| 131 | + #[test] |
| 132 | + fn test_local_config_creation() { |
| 133 | + let config = |
| 134 | + LocalConfig::new(1, "two_sum".to_string(), "Rust".to_string()); |
| 135 | + |
| 136 | + assert_eq!(config.problem_id, 1); |
| 137 | + assert_eq!(config.problem_name, "two_sum"); |
| 138 | + assert_eq!(config.language, "Rust"); |
| 139 | + } |
| 140 | + |
| 141 | + #[test] |
| 142 | + fn test_write_and_read_config() { |
| 143 | + let temp_dir = TempDir::new().unwrap(); |
| 144 | + let config = |
| 145 | + LocalConfig::new(1, "two_sum".to_string(), "Rust".to_string()); |
| 146 | + |
| 147 | + config.write_to_dir(temp_dir.path()).unwrap(); |
| 148 | + |
| 149 | + let config_path = temp_dir.path().join(".leetcode-cli"); |
| 150 | + assert!(config_path.exists()); |
| 151 | + |
| 152 | + let read_config = LocalConfig::read_from_path(&config_path).unwrap(); |
| 153 | + assert_eq!(read_config.problem_id, 1); |
| 154 | + assert_eq!(read_config.problem_name, "two_sum"); |
| 155 | + assert_eq!(read_config.language, "Rust"); |
| 156 | + } |
| 157 | + |
| 158 | + #[test] |
| 159 | + fn test_get_main_file() { |
| 160 | + let config = |
| 161 | + LocalConfig::new(1, "two_sum".to_string(), "Rust".to_string()); |
| 162 | + assert_eq!(config.get_main_file(), "main.rs"); |
| 163 | + |
| 164 | + let config = |
| 165 | + LocalConfig::new(1, "two_sum".to_string(), "Python".to_string()); |
| 166 | + assert_eq!(config.get_main_file(), "main.py"); |
| 167 | + } |
| 168 | +} |
0 commit comments