]> git.lizzy.rs Git - rust.git/blob - src/libcore/fmt/rt.rs
auto merge of #14568 : erickt/rust/slice-update, r=alexcrichton
[rust.git] / src / libcore / fmt / rt.rs
1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! This is an internal module used by the ifmt! runtime. These structures are
12 //! emitted to static arrays to precompile format strings ahead of time.
13 //!
14 //! These definitions are similar to their `ct` equivalents, but differ in that
15 //! these can be statically allocated and are slightly optimized for the runtime
16
17 use option::Option;
18
19 #[doc(hidden)]
20 pub enum Piece<'a> {
21     String(&'a str),
22     // FIXME(#8259): this shouldn't require the unit-value here
23     CurrentArgument(()),
24     Argument(Argument<'a>),
25 }
26
27 #[doc(hidden)]
28 pub struct Argument<'a> {
29     pub position: Position,
30     pub format: FormatSpec,
31     pub method: Option<&'a Method<'a>>
32 }
33
34 #[doc(hidden)]
35 pub struct FormatSpec {
36     pub fill: char,
37     pub align: Alignment,
38     pub flags: uint,
39     pub precision: Count,
40     pub width: Count,
41 }
42
43 /// Possible alignments that can be requested as part of a formatting directive.
44 #[deriving(PartialEq)]
45 pub enum Alignment {
46     /// Indication that contents should be left-aligned.
47     AlignLeft,
48     /// Indication that contents should be right-aligned.
49     AlignRight,
50     /// No alignment was requested.
51     AlignUnknown,
52 }
53
54 #[doc(hidden)]
55 pub enum Count {
56     CountIs(uint), CountIsParam(uint), CountIsNextParam, CountImplied,
57 }
58
59 #[doc(hidden)]
60 pub enum Position {
61     ArgumentNext, ArgumentIs(uint)
62 }
63
64 /// Flags which can be passed to formatting via a directive.
65 ///
66 /// These flags are discovered through the `flags` field of the `Formatter`
67 /// structure. The flag in that structure is a union of these flags into a
68 /// `uint` where each flag's discriminant is the corresponding bit.
69 pub enum Flag {
70     /// A flag which enables number formatting to always print the sign of a
71     /// number.
72     FlagSignPlus,
73     /// Currently not a used flag
74     FlagSignMinus,
75     /// Indicates that the "alternate formatting" for a type should be used.
76     ///
77     /// The meaning of this flag is type-specific.
78     FlagAlternate,
79     /// Indicates that padding should be done with a `0` character as well as
80     /// being aware of the sign to be printed.
81     FlagSignAwareZeroPad,
82 }
83
84 #[doc(hidden)]
85 pub enum Method<'a> {
86     Plural(Option<uint>, &'a [PluralArm<'a>], &'a [Piece<'a>]),
87     Select(&'a [SelectArm<'a>], &'a [Piece<'a>]),
88 }
89
90 #[doc(hidden)]
91 pub enum PluralSelector {
92     Keyword(PluralKeyword),
93     Literal(uint),
94 }
95
96 #[doc(hidden)]
97 pub enum PluralKeyword {
98     Zero,
99     One,
100     Two,
101     Few,
102     Many,
103 }
104
105 #[doc(hidden)]
106 pub struct PluralArm<'a> {
107     pub selector: PluralSelector,
108     pub result: &'a [Piece<'a>],
109 }
110
111 #[doc(hidden)]
112 pub struct SelectArm<'a> {
113     pub selector: &'a str,
114     pub result: &'a [Piece<'a>],
115 }