rust/src/libstd/fmt/rt.rs
Alex Crichton 80487ddcad std: Extract format string parsing out of libstd
This code does not belong in libstd, and rather belongs in a dedicated crate. In
the future, the syntax::ext::format module should move to the fmt_macros crate
(hence the name of the crate), but for now the fmt_macros crate will only
contain the format string parser.

The entire fmt_macros crate is marked #[experimental] because it is not meant
for general consumption, only the format!() interface is officially supported,
not the internals.

This is a breaking change for anyone using the internals of std::fmt::parse.
Some of the flags have moved to std::fmt::rt, while the actual parsing support
has all moved to the fmt_macros library.

[breaking-change]
2014-05-08 09:35:59 -07:00

102 lines
2.4 KiB
Rust

// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! This is an internal module used by the ifmt! runtime. These structures are
//! emitted to static arrays to precompile format strings ahead of time.
//!
//! These definitions are similar to their `ct` equivalents, but differ in that
//! these can be statically allocated and are slightly optimized for the runtime
#![allow(missing_doc)]
#![doc(hidden)]
use option::Option;
#[cfg(stage0)]
pub use fmt::parse::{Alignment, AlignLeft, AlignRight, AlignUnknown};
#[cfg(stage0)]
pub use fmt::parse::{PluralKeyword, Zero, One, Two, Few, Many};
#[cfg(stage0)]
pub use fmt::parse::{Flag, FlagSignPlus, FlagSignMinus, FlagSignAwareZeroPad};
#[cfg(stage0)]
pub use fmt::parse::{FlagAlternate};
pub enum Piece<'a> {
String(&'a str),
// FIXME(#8259): this shouldn't require the unit-value here
CurrentArgument(()),
Argument(Argument<'a>),
}
pub struct Argument<'a> {
pub position: Position,
pub format: FormatSpec,
pub method: Option<&'a Method<'a>>
}
pub struct FormatSpec {
pub fill: char,
pub align: Alignment,
pub flags: uint,
pub precision: Count,
pub width: Count,
}
#[cfg(not(stage0))]
#[deriving(Eq)]
pub enum Alignment {
AlignLeft,
AlignRight,
AlignUnknown,
}
pub enum Count {
CountIs(uint), CountIsParam(uint), CountIsNextParam, CountImplied,
}
pub enum Position {
ArgumentNext, ArgumentIs(uint)
}
#[cfg(not(stage0))]
pub enum Flag {
FlagSignPlus,
FlagSignMinus,
FlagAlternate,
FlagSignAwareZeroPad,
}
pub enum Method<'a> {
Plural(Option<uint>, &'a [PluralArm<'a>], &'a [Piece<'a>]),
Select(&'a [SelectArm<'a>], &'a [Piece<'a>]),
}
pub enum PluralSelector {
Keyword(PluralKeyword),
Literal(uint),
}
pub enum PluralKeyword {
Zero,
One,
Two,
Few,
Many,
}
pub struct PluralArm<'a> {
pub selector: PluralSelector,
pub result: &'a [Piece<'a>],
}
pub struct SelectArm<'a> {
pub selector: &'a str,
pub result: &'a [Piece<'a>],
}