1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-03 06:27:45 +00:00

uucore/format: move types for num_format

This commit is contained in:
Terts Diepraam 2023-11-09 16:05:11 +01:00
parent 198f7c7f26
commit 39c6758475
3 changed files with 70 additions and 57 deletions

View file

@ -11,7 +11,7 @@
// spell-checker:ignore (vars) charf decf floatf intf scif strf Cninety
mod spec;
mod num_format;
pub mod num_format;
use spec::Spec;
use std::{

View file

@ -1,17 +1,60 @@
use std::io::Write;
use super::{
spec::{
Case, FloatVariant, ForceDecimal, NumberAlignment, PositiveSign, Prefix, UnsignedIntVariant,
},
FormatError,
};
use super::FormatError;
pub trait Formatter {
type Input;
fn fmt(&self, writer: impl Write, x: Self::Input) -> Result<(), FormatError>;
}
#[derive(Clone, Copy)]
pub enum UnsignedIntVariant {
Decimal,
Octal(Prefix),
Hexadecimal(Case, Prefix),
}
#[derive(Clone, Copy)]
pub enum FloatVariant {
Decimal,
Scientific,
Shortest,
Hexadecimal,
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Case {
Lowercase,
Uppercase,
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Prefix {
No,
Yes,
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum ForceDecimal {
No,
Yes,
}
#[derive(Clone, Copy)]
pub enum PositiveSign {
None,
Plus,
Space,
}
#[derive(Clone, Copy)]
pub enum NumberAlignment {
Left,
RightSpace,
RightZero,
}
pub struct SignedInt {
pub width: usize,
pub positive_sign: PositiveSign,
@ -87,6 +130,20 @@ pub struct Float {
pub precision: usize,
}
impl Default for Float {
fn default() -> Self {
Self {
variant: FloatVariant::Decimal,
case: Case::Lowercase,
force_decimal: ForceDecimal::No,
width: 0,
positive_sign: PositiveSign::None,
alignment: NumberAlignment::Left,
precision: 2,
}
}
}
impl Formatter for Float {
type Input = f64;

View file

@ -1,7 +1,10 @@
// spell-checker:ignore (vars) charf decf floatf intf scif strf Cninety
use super::{
num_format::{self, Formatter},
num_format::{
self, Case, FloatVariant, ForceDecimal, Formatter, NumberAlignment, PositiveSign, Prefix,
UnsignedIntVariant,
},
FormatArgument, FormatError,
};
use std::{fmt::Display, io::Write};
@ -36,54 +39,6 @@ pub enum Spec {
},
}
#[derive(Clone, Copy)]
pub enum UnsignedIntVariant {
Decimal,
Octal(Prefix),
Hexadecimal(Case, Prefix),
}
#[derive(Clone, Copy)]
pub enum FloatVariant {
Decimal,
Scientific,
Shortest,
Hexadecimal,
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Case {
Lowercase,
Uppercase,
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Prefix {
No,
Yes,
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum ForceDecimal {
No,
Yes,
}
#[derive(Clone, Copy)]
pub enum PositiveSign {
None,
Plus,
Space,
}
#[derive(Clone, Copy)]
pub enum NumberAlignment {
Left,
RightSpace,
RightZero,
}
/// Precision and width specified might use an asterisk to indicate that they are
/// determined by an argument.
#[derive(Clone, Copy)]
@ -295,7 +250,8 @@ impl Spec {
width,
positive_sign,
alignment,
}.fmt(writer, *i)
}
.fmt(writer, *i)
}
&Spec::UnsignedInt {
variant,