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:
parent
198f7c7f26
commit
39c6758475
3 changed files with 70 additions and 57 deletions
|
@ -11,7 +11,7 @@
|
||||||
// spell-checker:ignore (vars) charf decf floatf intf scif strf Cninety
|
// spell-checker:ignore (vars) charf decf floatf intf scif strf Cninety
|
||||||
|
|
||||||
mod spec;
|
mod spec;
|
||||||
mod num_format;
|
pub mod num_format;
|
||||||
|
|
||||||
use spec::Spec;
|
use spec::Spec;
|
||||||
use std::{
|
use std::{
|
||||||
|
|
|
@ -1,17 +1,60 @@
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
use super::{
|
use super::FormatError;
|
||||||
spec::{
|
|
||||||
Case, FloatVariant, ForceDecimal, NumberAlignment, PositiveSign, Prefix, UnsignedIntVariant,
|
|
||||||
},
|
|
||||||
FormatError,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub trait Formatter {
|
pub trait Formatter {
|
||||||
type Input;
|
type Input;
|
||||||
fn fmt(&self, writer: impl Write, x: Self::Input) -> Result<(), FormatError>;
|
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 struct SignedInt {
|
||||||
pub width: usize,
|
pub width: usize,
|
||||||
pub positive_sign: PositiveSign,
|
pub positive_sign: PositiveSign,
|
||||||
|
@ -87,6 +130,20 @@ pub struct Float {
|
||||||
pub precision: usize,
|
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 {
|
impl Formatter for Float {
|
||||||
type Input = f64;
|
type Input = f64;
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
// spell-checker:ignore (vars) charf decf floatf intf scif strf Cninety
|
// spell-checker:ignore (vars) charf decf floatf intf scif strf Cninety
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
num_format::{self, Formatter},
|
num_format::{
|
||||||
|
self, Case, FloatVariant, ForceDecimal, Formatter, NumberAlignment, PositiveSign, Prefix,
|
||||||
|
UnsignedIntVariant,
|
||||||
|
},
|
||||||
FormatArgument, FormatError,
|
FormatArgument, FormatError,
|
||||||
};
|
};
|
||||||
use std::{fmt::Display, io::Write};
|
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
|
/// Precision and width specified might use an asterisk to indicate that they are
|
||||||
/// determined by an argument.
|
/// determined by an argument.
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
|
@ -295,7 +250,8 @@ impl Spec {
|
||||||
width,
|
width,
|
||||||
positive_sign,
|
positive_sign,
|
||||||
alignment,
|
alignment,
|
||||||
}.fmt(writer, *i)
|
}
|
||||||
|
.fmt(writer, *i)
|
||||||
}
|
}
|
||||||
&Spec::UnsignedInt {
|
&Spec::UnsignedInt {
|
||||||
variant,
|
variant,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue