-
Notifications
You must be signed in to change notification settings - Fork 0
feat(config): add local problem config with auto-detection #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| use std::{ | ||
| fs, | ||
| io, | ||
| path::Path, | ||
| }; | ||
|
|
||
| use serde::{ | ||
| Deserialize, | ||
| Serialize, | ||
| }; | ||
|
|
||
| #[derive(Deserialize, Serialize, Debug, Clone)] | ||
| pub struct LocalConfig { | ||
| pub problem_id: u32, | ||
| pub problem_name: String, | ||
| pub language: String, | ||
| } | ||
|
|
||
| impl LocalConfig { | ||
| pub fn new( | ||
| problem_id: u32, problem_name: String, language: String, | ||
| ) -> Self { | ||
| Self { problem_id, problem_name, language } | ||
| } | ||
|
|
||
| /// Find and read local config from current directory or parent directories | ||
| pub fn find_and_read() -> io::Result<Option<Self>> { | ||
| let mut current_dir = std::env::current_dir()?; | ||
|
|
||
| loop { | ||
| let config_path = current_dir.join(".leetcode-cli"); | ||
| if config_path.exists() { | ||
| let content = fs::read_to_string(&config_path)?; | ||
| let config: LocalConfig = | ||
| toml::from_str(&content).map_err(|e| { | ||
| io::Error::new(io::ErrorKind::InvalidData, e) | ||
| })?; | ||
| return Ok(Some(config)); | ||
| } | ||
|
|
||
| if !current_dir.pop() { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| Ok(None) | ||
| } | ||
|
|
||
| /// Write local config to specified directory | ||
| pub fn write_to_dir(&self, dir: &Path) -> io::Result<()> { | ||
| let config_path = dir.join(".leetcode-cli"); | ||
| let content = toml::to_string(self) | ||
| .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; | ||
| fs::write(config_path, content) | ||
| } | ||
|
|
||
| /// Read local config from specified file path | ||
| pub fn read_from_path(path: &Path) -> io::Result<Self> { | ||
| let content = fs::read_to_string(path)?; | ||
| toml::from_str(&content) | ||
| .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) | ||
| } | ||
|
|
||
| /// Get the main source file name based on language | ||
| pub fn get_main_file(&self) -> String { | ||
| match self.language.to_lowercase().as_str() { | ||
| "rust" => "main.rs".to_string(), | ||
| "python" | "python3" => "main.py".to_string(), | ||
| "javascript" => "main.js".to_string(), | ||
| "typescript" => "main.ts".to_string(), | ||
| "go" => "main.go".to_string(), | ||
| "java" => "Main.java".to_string(), | ||
| "c++" => "main.cpp".to_string(), | ||
| "c" => "main.c".to_string(), | ||
| _ => "main.txt".to_string(), | ||
| } | ||
| } | ||
|
|
||
| /// Resolve problem ID and file path from CLI args or local config | ||
| pub fn resolve_problem_params( | ||
| id: Option<u32>, path_to_file: Option<String>, | ||
| ) -> io::Result<(u32, String)> { | ||
| match (id, &path_to_file) { | ||
| (Some(id), Some(path)) => Ok((id, path.clone())), | ||
| _ => { | ||
| // Try to find local config | ||
| match Self::find_and_read()? { | ||
| Some(config) => { | ||
| let problem_id = id.unwrap_or(config.problem_id); | ||
| let file_path = path_to_file.unwrap_or_else(|| { | ||
| format!("src/{}", config.get_main_file()) | ||
| }); | ||
| Ok((problem_id, file_path)) | ||
| }, | ||
| None => { | ||
| if id.is_none() { | ||
| return Err(io::Error::new( | ||
| io::ErrorKind::NotFound, | ||
| "No problem ID provided and no .leetcode-cli \ | ||
| config found. Either provide --id or run \ | ||
| from a problem directory", | ||
| )); | ||
| } | ||
| if path_to_file.is_none() { | ||
| return Err(io::Error::new( | ||
| io::ErrorKind::NotFound, | ||
| "No file path provided", | ||
| )); | ||
| } | ||
| // If we get here, both id and path_to_file must be Some | ||
| match (id, path_to_file) { | ||
| (Some(id), Some(path)) => Ok((id, path)), | ||
| _ => Err(io::Error::other( | ||
| "Unexpected error: id or path_to_file missing \ | ||
| after checks", | ||
| )), | ||
| } | ||
| }, | ||
| } | ||
| }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use tempfile::TempDir; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_local_config_creation() { | ||
| let config = | ||
| LocalConfig::new(1, "two_sum".to_string(), "Rust".to_string()); | ||
|
|
||
| assert_eq!(config.problem_id, 1); | ||
| assert_eq!(config.problem_name, "two_sum"); | ||
| assert_eq!(config.language, "Rust"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_write_and_read_config() { | ||
| let temp_dir = TempDir::new().unwrap(); | ||
| let config = | ||
| LocalConfig::new(1, "two_sum".to_string(), "Rust".to_string()); | ||
|
|
||
| config.write_to_dir(temp_dir.path()).unwrap(); | ||
|
|
||
| let config_path = temp_dir.path().join(".leetcode-cli"); | ||
| assert!(config_path.exists()); | ||
|
|
||
| let read_config = LocalConfig::read_from_path(&config_path).unwrap(); | ||
| assert_eq!(read_config.problem_id, 1); | ||
| assert_eq!(read_config.problem_name, "two_sum"); | ||
| assert_eq!(read_config.language, "Rust"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_get_main_file() { | ||
| let config = | ||
| LocalConfig::new(1, "two_sum".to_string(), "Rust".to_string()); | ||
| assert_eq!(config.get_main_file(), "main.rs"); | ||
|
|
||
| let config = | ||
| LocalConfig::new(1, "two_sum".to_string(), "Python".to_string()); | ||
| assert_eq!(config.get_main_file(), "main.py"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This condition will never be reached because the outer match pattern
_only matches whenpath_to_fileisNone, making this check redundant and creating unreachable code.