Add an error return method to `format`
Nobody has claimed this yet.
- Dominant language
- Markdown
- Stars
- 6.6k
- Forks
- 1.7k
- Avg merge
- 16h 14m
- Merged PRs (30d)
- 1
Description
Add an error return method to format!
The format! macro never fails and the compiler reports an error when it does.
let a:String = format!("{}",9);
Sometimes, however, I may need to reorganize the formatted string in a custom program macro based on user input.
When the user typed something wrong, I wanted to catch the compiler error and re-report the error in the right place.
I wish there was a way to return the wrong or a way to do this, thanks a lot.
let b:Result<String,String> = try_format!("{:?}",9);
match b {
Ok(ok) => {
// do something...
},
Err(err) => {
// Custom error handling...
},
}
Here's a scenario that could go wrong to make my intentions more obvious.
I provide my users with a macro called comment!, which takes a format argument like format! :
#[comment("{:1$}","hi~",10)]
fn do(){
//....
}
comment! inserts a new line of code into method do :
fn do(){
println!("{:1$}","hi~",10);
//....
}
If the user accidentally enters formatting parameters incorrectly:
#[comment("{:$}","hi~",10)]
^^^^
fn do(){
//....
}
Then, the newly generated code will be wrong.
The compiler will give a vague error on #[comment(...)] and cannot indicate the correct location:
fn do(){
println!("{:$}","hi~",10); // ^^^^ section is what actually went wrong, missing the width index.
^^^^
//....
}
Now, I want to check that the formatting parameters are correct when I generate 'println'.
I think the best way is to catch any errors the compiler gives you,
And display the error in the corresponding position of comment! :
#[comment("{:$}","hi~",10)]
~~~~
fn do(){
//....
}
So, I wish there was a try_format!.
When the parameter is not correct, can return an error, let me control.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the issue's examples of format!, try_format!, and the comment! procedural macro scenario. Determine whether the requested behavior is feasible and what API and diagnostic behavior it would require; done means documenting a decided approach in an RFC or closing the proposal with a clear rationale.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100