-
Notifications
You must be signed in to change notification settings - Fork 60
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
Allow the context selector struct name to be specified #210
base: main
Are you sure you want to change the base?
Allow the context selector struct name to be specified #210
Conversation
You could already specify that no context selector should be generated, using context(false). Now you can specify the name of the context selector, using context(MySelectorTypeName).
Can you explain a bit more about the general path you see? I don't follow how this plays into that bigger feature. |
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.
Apologies for the late review — I opened this up and never took any action!
Sometimes, you may already have a struct with the same name as one of | ||
the variants of your error enum. You might also have 2 error enums | ||
that share a variant. In those cases, there may be naming collisions | ||
with the context selector type(s) generated by Snafu, which by default |
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.
The proper (non-code) name of the library is "SNAFU".
enum Error { | ||
#[snafu(context(FooContext))] | ||
Foo { | ||
source: std::io::Error |
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.
source: std::io::Error | |
source: std::io::Error, |
#[snafu( | ||
context(InvalidUserContext), | ||
display = r#"("User ID {} is invalid", user_id)"# | ||
)] |
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 style is fine, which is why it compiles, but I just find it ugly for whatever reason. FWIW, I tend to split it into two:
#[snafu(context(InvalidUserContext))]
#[snafu(display = r#"("User ID {} is invalid", user_id)"#)]
}, | ||
#[snafu( | ||
context(SaveConfigContext), | ||
display = r#"("Could not open config file at {}", source)"# |
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.
Feel free to use the modern style in these tests:
display("Could not open config file at {}", source)
#[derive(Debug, Snafu)] | ||
enum Error { | ||
#[snafu( | ||
context(OpenConfigContext), |
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.
Seeing it in use, I wonder if we want to give some more "namespacing". For example:
context(name(OpenConfigContext)),
Or
context(selector(OpenConfigContext)),
Or
context(selector_name(OpenConfigContext)),
You could already specify that no context selector should be generated, using
context(false)
. Now you can specify the name of the context selector, usingcontext(MySelectorTypeName)
.Helps with #188. I mean, it doesn't do exactly what asked there, which was to be able to put the generated context selectors into a submodule, but in the comments of that issue it was suggested) that this could be an alternative solution for the same issues.
This is also the first step toward #199, where custom context selectors will be needed.