-
Notifications
You must be signed in to change notification settings - Fork 5
Add error_set_part macro #38
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
7 commits
Select commit
Hold shift + click to select a range
4445a84
Move no_std test
mcmah309 4b57fd9
Add error_set_part macro
mcmah309 776e540
Remove combine_parts default
mcmah309 ce9ea2b
Add combine_parts test
mcmah309 e9460b2
fix test path
mcmah309 e8b32f4
Update pattern
mcmah309 ca73831
Use line number instead of character offset
mcmah309 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
Some comments aren't visible on the classic Files Changed page.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| [workspace] | ||
| resolver = "3" | ||
| members = ["error_set", "error_set_impl"] | ||
| members = ["error_set", "error_set_impl", "examples/error_set_part"] | ||
|
|
||
| exclude = ["test_no_std"] | ||
| exclude = ["examples/test_no_std"] |
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,131 @@ | ||
| use std::fs; | ||
| use std::path::{Path, PathBuf}; | ||
|
|
||
| use ignore::WalkBuilder; | ||
|
|
||
| /// Combines all uses of the `error_set_part!` macro in `src/` (respecting `.gitignore`) into one | ||
| /// `error_set!` macro at the generated file `src/error_set.rs` | ||
| pub fn combine_error_set_parts() { | ||
| let mut parts = String::with_capacity(1024); | ||
| for file_path in find_rust_files("src") { | ||
| if let Ok(content) = fs::read_to_string(&file_path) { | ||
| extract_error_set_parts(&content, &mut parts, &file_path); | ||
| } | ||
| } | ||
| if parts.is_empty() { | ||
| return; | ||
| } | ||
| let output_path = "src/error_set.rs"; | ||
| if let Err(e) = fs::write( | ||
| output_path, | ||
| format!( | ||
| "// This file is auto-generated\n\nerror_set::error_set! {{\n{parts}\n}}" | ||
| ), | ||
| ) { | ||
| panic!("Failed to write to {}: {}", output_path, e); | ||
| } | ||
| // if let Err(e) = std::process::Command::new("rustfmt") | ||
| // .arg(output_path) | ||
| // .status() | ||
| // { | ||
| // println!("cargo:warning=Failed to format {:?}: {}", output_path, e); | ||
| // } | ||
| } | ||
|
|
||
| #[inline] | ||
| fn find_rust_files<P: AsRef<Path>>(dir: P) -> impl Iterator<Item = PathBuf> { | ||
| WalkBuilder::new(dir) | ||
| .build() | ||
| .filter_map(|e| e.ok()) | ||
| .filter(|e| e.path().extension().and_then(|s| s.to_str()) == Some("rs")) | ||
| .map(|e| e.path().to_path_buf()) | ||
| } | ||
mcmah309 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #[inline] | ||
| fn extract_error_set_parts(content: &str, error_set_body: &mut String, current_file: &Path) { | ||
| let needle = "error_set_part!"; | ||
| let mut pos = 0; | ||
|
|
||
| while let Some(relative_idx) = content[pos..].find(needle) { | ||
| let statement_start = pos + relative_idx; | ||
| let mut brace_pos = statement_start + needle.len(); | ||
|
|
||
| // Skip whitespace | ||
| let skip = content[brace_pos..].chars() | ||
| .take_while(|c| c.is_whitespace()) | ||
| .map(|c| c.len_utf8()) | ||
| .sum::<usize>(); | ||
| brace_pos += skip; | ||
|
|
||
| if brace_pos < content.len() { | ||
| let end = extract_balanced_braces(content, brace_pos, error_set_body, current_file); | ||
| pos = end; | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[inline] | ||
| fn extract_balanced_braces(content: &str, brace_start_pos: usize, error_set_body: &mut String, current_file: &Path) -> usize { | ||
| let mut chars = content[brace_start_pos..].chars(); | ||
|
|
||
| let next = chars.next(); | ||
| if let Some(next) = next { | ||
| if next != '{' { | ||
| panic!("Expected '{{' after error_set_part! macro in {}", current_file.display()); | ||
| } | ||
| } else { | ||
| panic!("Unexpected end of input after error_set_part! macro in {}", current_file.display()); | ||
| } | ||
|
|
||
| let mut depth = 1; | ||
| let mut end = brace_start_pos + 1; | ||
|
|
||
| for (i, ch) in chars.enumerate() { | ||
| match ch { | ||
| '{' => depth += 1, | ||
| '}' => { | ||
| depth -= 1; | ||
| if depth == 0 { | ||
| end = brace_start_pos + i + 2; // +1 for opening brace, +1 for current closing brace | ||
| break; | ||
| } | ||
| } | ||
| _ => {} | ||
| } | ||
| } | ||
|
|
||
| if depth == 0 { | ||
| let line_number = content[..brace_start_pos].chars().filter(|&c| c == '\n').count() + 1; | ||
| error_set_body.push_str(&format!("\t// From `{}:{line_number}`\n", current_file.display())); | ||
| error_set_body.push_str(&content[brace_start_pos + 1..end - 1].trim_start_matches('\n')); | ||
| error_set_body.push_str("\n"); | ||
| end | ||
| } else { | ||
| panic!("Unmatched braces in error_set_part! macro in {}", current_file.display()); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_extract_error_set_parts() { | ||
| let code = r#" | ||
| error_set_part! { | ||
| MyError1, | ||
| MyError2, | ||
| } | ||
|
|
||
| fn some_function() {} | ||
|
|
||
| error_set_part! { AnotherError } | ||
| "#; | ||
| let mut parts = String::new(); | ||
| extract_error_set_parts(code, &mut parts, &PathBuf::from("test")); | ||
| assert!(parts.contains("MyError1")); | ||
| assert!(parts.contains("AnotherError")); | ||
| } | ||
| } | ||
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 @@ | ||
| ignored.rs |
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,10 @@ | ||
| [package] | ||
| name = "error_set_part" | ||
| version = "0.1.0" | ||
| edition = "2024" | ||
|
|
||
| [dependencies] | ||
| error_set = { path = "../../error_set" } | ||
|
|
||
| [build-dependencies] | ||
| error_set = { path = "../../error_set", features = ["combine_parts"] } |
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,3 @@ | ||
| fn main() { | ||
| error_set::combine_error_set_parts(); | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.