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

Fix number implementations are never used #24

Merged
merged 5 commits into from
Jul 28, 2022
Merged

Conversation

Nebdir
Copy link
Contributor

@Nebdir Nebdir commented Jun 29, 2022

These commits contain a new trait to use the correct types when formatting.
This closes #23.

while writing this I think about mixed values and I'm unsure if there are downsides that might follow this usage.

Anyway, while writing I noticed some warnings that could be removed without any downside, while improving readability, moving on to the newer syntax.

Jan Grigat added 2 commits June 29, 2022 23:43
@vitiral
Copy link
Owner

vitiral commented Jun 29, 2022 via email

@Nebdir
Copy link
Contributor Author

Nebdir commented Jun 30, 2022

absolut no work, it was the last commit.
should be good now.

src/fmtnum.rs Outdated
@@ -123,3 +123,13 @@ macro_rules! fmtfloat {
}
})*)
}

macro_rules! fmttype {
Copy link
Owner

Choose a reason for hiding this comment

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

is this the right place for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it generates the definition of what formatter should be used for which type of numbers, sounds good to me, but I'm not afraid to move it elsewhere if you want me to.

src/lib.rs Show resolved Hide resolved
@Nebdir
Copy link
Contributor Author

Nebdir commented Jul 26, 2022

I ended up splitting this into two parts and keeping the old behavior and adding an _ext function for formatting numbers and custom types. I've discussed this with a good friend and we see no other way for this. But different from the previous commitment, basic usage should be the same.

@Nebdir
Copy link
Contributor Author

Nebdir commented Jul 27, 2022

Another solution could be to offer a macro that implements the trait for types that already have a Display impl.

We still could use things like derive macros or feature flags, but I don't see anything that brings us closer to the ideal solution.

@vitiral
Copy link
Owner

vitiral commented Jul 27, 2022

after consideration, I think this should be a breaking change considering that it is fixing a bug. I'm also remembering some of the problem space. The main issue is that Display takes in a struct Formatter object. If that was a trait instead, then we wouldn't have to do any of these shenanigans.

Anyway, all water under the bridge. On that point though, your TypeFormatting is really just a re-implementation of Display using our own Formatter. Let's make the names match this fact:

  • rename TypeFormatting -> DisplayStr and move into lib.rs.
    • rename method do_format -> display_str
    • rename fmttype macro to something like display_str_impl and move into lib.rs
  • in strfmt function, replace Display with DisplayStr, to pick up your changes. I think this was your original approach and will be a breaking change.
  • create a new method strfmt_display which still uses Display, essentially converting everything to a String. This will allow folks to easily migrate if they depend on the Display implementation.

Thoughts?

@Nebdir
Copy link
Contributor Author

Nebdir commented Jul 27, 2022

Sounds good, then let's get this on the road:

  • rename TypeFormatting and functions
  • rename macros
  • change signature of strfmt to match new requirements
  • create legacy strfmt named strfmt_display
  • Add Documentation
  • add tests for legacy

@vitiral
Copy link
Owner

vitiral commented Jul 27, 2022

Sounds good, you rock!

@Nebdir
Copy link
Contributor Author

Nebdir commented Jul 27, 2022

same legacy convention for formatter? eg format_display?

@vitiral
Copy link
Owner

vitiral commented Jul 27, 2022

You mean the trait Format? Yes, I think you should take in DisplayStr instead of display, and there can be a format_display method like you said.

};
strfmt_map(fmtstr, &formatter)
}

pub trait Format {
Copy link
Contributor Author

@Nebdir Nebdir Jul 27, 2022

Choose a reason for hiding this comment

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

Sorry, I'll make this as clear as I can explain with my limited English.
The marked trait (line 96) is defining functions for str and String where usage would be "{Zero}".format(&vars).
By adding a legacy function here named strfmt_display it would make sense to add a similar extension to allow a usage like "{Zero}".format_display(&vars) as either the original signature would change from format<K, D: fmt::Display>(&self, vars: &HashMap<K, D>) -> Result<String> to format<K, D: DisplayStr>(&self, vars: &HashMap<K, D>) -> Result<String> or the body would change and default to use the legacy code.

Don't hesitate to ask, sometimes I think way too complicated and by reading these lines I wrote I'm not sure if it gets clear what problem ist the cause for this..

Copy link
Owner

@vitiral vitiral Jul 27, 2022

Choose a reason for hiding this comment

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

Thanks for clarifying. I agree with you, there should be two functioned defined by Format:

trait Format {
  fn format<K, D: DisplayStr>(&self, vars: &HashMap<K, D>) -> Result<String>

  fn format_display<K, D: Display>(&self, vars: &HashMap<K, D>) -> Result<String>
}

~ renamed DisplayStr::do_format to DisplayStr::display_str
~ renamed fmttype macro to display_str_impl
moved display_str_iml to lib.rs
reordered lib.rs for readability
+ added legacy versions for strfmt (strfmt_display) and format (format_display)
+ deprecated legacy versions strfmt_display and format_display
+ added documentation
+ added tests for legacy versions to keep functionality for easier migration
@Nebdir
Copy link
Contributor Author

Nebdir commented Jul 28, 2022

Okay, so I was realizing that one big commit wasn't one of my greatest ideas, sry.

Second I added some documentation and deprecation marks. As these changes contain a change of the API I thought it should increment the minor number at least.

src/lib.rs Outdated Show resolved Hide resolved
src/tests/float.rs Outdated Show resolved Hide resolved
src/tests/float.rs Outdated Show resolved Hide resolved
~ made examples simpler
@vitiral vitiral merged commit 0d26997 into vitiral:master Jul 28, 2022
@vitiral
Copy link
Owner

vitiral commented Jul 28, 2022

pushed to crates.io after a few minor fixes from me

https://crates.io/crates/strfmt

@Nebdir
Copy link
Contributor Author

Nebdir commented Jul 28, 2022

ah, the version number. great, that was intended by the way, as it's your project and therefore your decision when things will get a release.

The next thing on my list is the refactoring to remove warnings.

@vitiral
Copy link
Owner

vitiral commented Jul 28, 2022

I ran cargo fmt and removed fixed lint warnings before I released, sorry 😄

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.

Behaviour of f64 formatting differs from std::fmt
2 participants