Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

michiel-de-muynck
Copy link

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).

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.

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).
@shepmaster
Copy link
Owner

This is also the first step toward #199, where custom context selectors will be needed.

Can you explain a bit more about the general path you see? I don't follow how this plays into that bigger feature.

Copy link
Owner

@shepmaster shepmaster left a 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
Copy link
Owner

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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
source: std::io::Error
source: std::io::Error,

Comment on lines +24 to +27
#[snafu(
context(InvalidUserContext),
display = r#"("User ID {} is invalid", user_id)"#
)]
Copy link
Owner

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)"#
Copy link
Owner

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),
Copy link
Owner

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)),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants