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

Implement Display, LowerExp, UpperExp, ... #13

Closed
4 tasks
iliekturtles opened this issue Mar 12, 2017 · 6 comments
Closed
4 tasks

Implement Display, LowerExp, UpperExp, ... #13

iliekturtles opened this issue Mar 12, 2017 · 6 comments

Comments

@iliekturtles
Copy link
Owner

iliekturtles commented Mar 12, 2017

https://doc.rust-lang.org/core/fmt/trait.Binary.html
https://doc.rust-lang.org/core/fmt/trait.Display.html
https://doc.rust-lang.org/core/fmt/trait.LowerExp.html
https://doc.rust-lang.org/core/fmt/trait.LowerHex.html
https://doc.rust-lang.org/core/fmt/trait.Octal.html
https://doc.rust-lang.org/core/fmt/trait.UpperExp.html
https://doc.rust-lang.org/core/fmt/trait.UpperHex.html

Is there a way to use the fmt syntax or flags to include an implicit unit conversion, unit abbreviation, or unit label?

// What replaces `...` to allow the following outputs: 10 m, 10 meters, 32.8084 ft, 32.8084 feet
println!("{...}", new Length(10, meter));
  • Quantity
  • $quantities (Dimension)
  • BaseUnits
  • $unit (Measurement units)
@iliekturtles iliekturtles changed the title Implement Display Implement Display, LowerExp, UpperExp Mar 12, 2017
@iliekturtles
Copy link
Owner Author

Something like the following should be able to meet the requirements. Note that the following code takes a lot of liberties and is just to give an idea of what should be necessary.

The Arguments struct would contain uom specific formatting settings like the subunit to display and how to label the value. Numeric formatting options would still come from the format string and be pushed down to the underlying storage type's Display/LowerExp/... implementation.

mod fmt {
    pub struct Arguments<...> {
        subunit: Subunit,
        label: LabelEnum,
    }

    pub struct QuantityArguments<...> {
        quantity: Quantity<...>,
        arguments: Arguments,
    }

    impl Display for Arguments { ... }
    impl LowerExp for Arguments { ... }
    ...
}

...

let l = Length::<meter>::new(10);
let a1: Arguments = Arguments::new(meter, Abbreviation);
let a2: Arguments = Arguments::new(meter, Description);
let a3: QuantityArguments = Arguments::new(l, foot, Abbreviation);
let a4: QuantityArguments = Arguments::new(l, foot, Description);

// 10 m, 10 meters, 32.8084 ft, 32.8084 feet
println!("{}, {}, {}, {}", a1.with(l), a2.with(l), a3, a4);

@iliekturtles iliekturtles changed the title Implement Display, LowerExp, UpperExp Implement Display, LowerExp, UpperExp, ... Mar 28, 2018
@Aehmlo
Copy link
Contributor

Aehmlo commented Apr 13, 2018

I have a working implementation that'll print a Quantity in any of the desired styles (with the unit appended in the desired format); do we need to be able to print raw units as well? If so, I'll write that and send a PR, but I'm not sure I see any use cases for that.

@iliekturtles
Copy link
Owner Author

Not exactly sure what you mean by printing raw units. Can you provide a short example?

@Aehmlo
Copy link
Contributor

Aehmlo commented Apr 13, 2018

Something like println!("{}", meter), perhaps with the same options for the output to be "meter"/"m".

@iliekturtles
Copy link
Owner Author

Ahhh. Not needed! meter.abbreviation()/.singular()/.description() can be used.

@Aehmlo
Copy link
Contributor

Aehmlo commented Apr 13, 2018

That's what I figured. PR inbound soon.

iliekturtles pushed a commit that referenced this issue Jul 2, 2018
iliekturtles pushed a commit that referenced this issue Nov 14, 2018
iliekturtles pushed a commit that referenced this issue Dec 20, 2018
iliekturtles pushed a commit that referenced this issue Jan 12, 2019
Allows `Quantity` to be formatted with the standard library formatting
traits: Binary, Debug, Display, LowerExp, LowerHex, Octal, UpperExp,
UpperHex. Resolves #13.
iliekturtles pushed a commit that referenced this issue Jan 12, 2019
Allows `Quantity` to be formatted with the standard library formatting
traits: Binary, Debug, Display, LowerExp, LowerHex, Octal, UpperExp,
UpperHex. Resolves #13.
iliekturtles pushed a commit that referenced this issue Jan 12, 2019
Allows `Quantity` to be formatted with the standard library formatting
traits: Binary, Debug, Display, LowerExp, LowerHex, Octal, UpperExp,
UpperHex. Resolves #13.
iliekturtles pushed a commit that referenced this issue Jan 12, 2019
Allows `Quantity` to be formatted with the standard library formatting
traits: Binary, Debug, Display, LowerExp, LowerHex, Octal, UpperExp,
UpperHex. Resolves #13.
iliekturtles pushed a commit that referenced this issue Jan 12, 2019
Allows `Quantity` to be formatted with the standard library formatting
traits: Binary, Debug, Display, LowerExp, LowerHex, Octal, UpperExp,
UpperHex. Resolves #13.
iliekturtles pushed a commit that referenced this issue Jan 14, 2019
Allows `Quantity` to be formatted with the standard library formatting
traits: Binary, Debug, Display, LowerExp, LowerHex, Octal, UpperExp,
UpperHex. Resolves #13.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants