-
Notifications
You must be signed in to change notification settings - Fork 14
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
Support Formatter::alternate
(the #
in format string) for another display variant
#41
Comments
Thanks. I think it is a good idea. I had not considered the "alternate" form at all until now. I gave it some thought and thought the following points also need to be considered
|
I guess // all on one line
#[display("value {} = {0}", alt = "Value {} is {0}", style = "UPPERCASE", alt_style = "TitleCase")]
// dedicated #[alt_display(...)] attribute
#[display("value {} = {0}", style = "UPPERCASE")]
#[alt_display("value {} = {0}", style = "TitleCase")]
// Re-use the same attribute multiple times, allowing each to append values (might want to detect dups?)
#[display("value {} = {0}", style = "UPPERCASE")]
#[display(alt = "value {} = {0}", alt_style = "TitleCase")] I was also thinking of the best code to generate. At the fn root, it could be one of these (assuming the enum has just one variant: // Two match statements
if f.alternate() {
match self {
Self::Replace => f.write_str("replace"),
}
} else {
match self {
Self::Replace => f.write_str("REPLACE"),
}
}
// --------------------------------------------------------------------------
// One match, each variant has its own if f.alternate() {...} else {...}
match self {
Self::Replace => if f.alternate() {
f.write_str("replace")
} else {
f.write_str("REPLACE")
}
} Some possible optimizations (not sure if worth it, or if compiler will automatically do them):
|
As another option, I considered specifying multiple settings in a tuple, as shown below. #[display(style = ("UPPERCASE", "TitleCase"))] It is concise, but difficult to use with arguments that can specify expressions, and the lack of keywords such as |
Oxigraph (SPAQRL RDF engine) has two representations of every enum value, e.g.
Function::Replace
has to be formatted as eitherreplace
orREPLACE
depending on the usecase. Currently, Oxigraph has a regularfmt::Display
implementation with a giantmatch
statement to format the upper case variant, and another functionfn fmt_sse(&self, f: &mut impl fmt::Write)
-- that looks identical to the Display, but is used by another code path.I would like to propose
parse-display
to add support for multiple display variants. When present, parse-display can generate this type of code, which will be used byformat!("{func}")
vsformat!("{func:#}")
CC: @Tpt
The text was updated successfully, but these errors were encountered: