rustfmt libfmt_macros
This commit is contained in:
parent
ec4362da56
commit
1bdf4ad8dc
1 changed files with 220 additions and 160 deletions
|
@ -75,7 +75,7 @@ pub struct FormatSpec<'a> {
|
||||||
/// The descriptor string representing the name of the format desired for
|
/// The descriptor string representing the name of the format desired for
|
||||||
/// this argument, this can be empty or any number of characters, although
|
/// this argument, this can be empty or any number of characters, although
|
||||||
/// it is required to be one word.
|
/// it is required to be one word.
|
||||||
pub ty: &'a str
|
pub ty: &'a str,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Enum describing where an argument for a format can be located.
|
/// Enum describing where an argument for a format can be located.
|
||||||
|
@ -202,7 +202,12 @@ impl<'a> Parser<'a> {
|
||||||
/// returned, otherwise the character is consumed and true is returned.
|
/// returned, otherwise the character is consumed and true is returned.
|
||||||
fn consume(&mut self, c: char) -> bool {
|
fn consume(&mut self, c: char) -> bool {
|
||||||
if let Some(&(_, maybe)) = self.cur.peek() {
|
if let Some(&(_, maybe)) = self.cur.peek() {
|
||||||
if c == maybe { self.cur.next(); true } else { false }
|
if c == maybe {
|
||||||
|
self.cur.next();
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
@ -227,7 +232,11 @@ impl<'a> Parser<'a> {
|
||||||
/// character
|
/// character
|
||||||
fn ws(&mut self) {
|
fn ws(&mut self) {
|
||||||
while let Some(&(_, c)) = self.cur.peek() {
|
while let Some(&(_, c)) = self.cur.peek() {
|
||||||
if c.is_whitespace() { self.cur.next(); } else { break }
|
if c.is_whitespace() {
|
||||||
|
self.cur.next();
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -237,8 +246,12 @@ impl<'a> Parser<'a> {
|
||||||
// we may not consume the character, peek the iterator
|
// we may not consume the character, peek the iterator
|
||||||
while let Some(&(pos, c)) = self.cur.peek() {
|
while let Some(&(pos, c)) = self.cur.peek() {
|
||||||
match c {
|
match c {
|
||||||
'{' | '}' => { return &self.input[start..pos]; }
|
'{' | '}' => {
|
||||||
_ => { self.cur.next(); }
|
return &self.input[start..pos];
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
self.cur.next();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
&self.input[start..self.input.len()]
|
&self.input[start..self.input.len()]
|
||||||
|
@ -263,7 +276,7 @@ impl<'a> Parser<'a> {
|
||||||
Some(&(_, c)) if c.is_alphabetic() => {
|
Some(&(_, c)) if c.is_alphabetic() => {
|
||||||
ArgumentNamed(self.word())
|
ArgumentNamed(self.word())
|
||||||
}
|
}
|
||||||
_ => ArgumentNext
|
_ => ArgumentNext,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -279,7 +292,9 @@ impl<'a> Parser<'a> {
|
||||||
width: CountImplied,
|
width: CountImplied,
|
||||||
ty: &self.input[..0],
|
ty: &self.input[..0],
|
||||||
};
|
};
|
||||||
if !self.consume(':') { return spec }
|
if !self.consume(':') {
|
||||||
|
return spec
|
||||||
|
}
|
||||||
|
|
||||||
// fill character
|
// fill character
|
||||||
if let Some(&(_, c)) = self.cur.peek() {
|
if let Some(&(_, c)) = self.cur.peek() {
|
||||||
|
@ -347,7 +362,11 @@ impl<'a> Parser<'a> {
|
||||||
/// width.
|
/// width.
|
||||||
fn count(&mut self) -> Count<'a> {
|
fn count(&mut self) -> Count<'a> {
|
||||||
if let Some(i) = self.integer() {
|
if let Some(i) = self.integer() {
|
||||||
if self.consume('$') { CountIsParam(i) } else { CountIs(i) }
|
if self.consume('$') {
|
||||||
|
CountIsParam(i)
|
||||||
|
} else {
|
||||||
|
CountIs(i)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
let tmp = self.cur.clone();
|
let tmp = self.cur.clone();
|
||||||
let word = self.word();
|
let word = self.word();
|
||||||
|
@ -370,8 +389,13 @@ impl<'a> Parser<'a> {
|
||||||
/// characters.
|
/// characters.
|
||||||
fn word(&mut self) -> &'a str {
|
fn word(&mut self) -> &'a str {
|
||||||
let start = match self.cur.peek() {
|
let start = match self.cur.peek() {
|
||||||
Some(&(pos, c)) if c.is_xid_start() => { self.cur.next(); pos }
|
Some(&(pos, c)) if c.is_xid_start() => {
|
||||||
_ => { return &self.input[..0]; }
|
self.cur.next();
|
||||||
|
pos
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
return &self.input[..0];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
while let Some(&(pos, c)) = self.cur.peek() {
|
while let Some(&(pos, c)) = self.cur.peek() {
|
||||||
if c.is_xid_continue() {
|
if c.is_xid_continue() {
|
||||||
|
@ -397,7 +421,11 @@ impl<'a> Parser<'a> {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if found { Some(cur) } else { None }
|
if found {
|
||||||
|
Some(cur)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -437,36 +465,55 @@ mod tests {
|
||||||
same("\\}}", &[String("\\"), String("}")]);
|
same("\\}}", &[String("\\"), String("}")]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test] fn invalid01() { musterr("{") }
|
#[test]
|
||||||
#[test] fn invalid02() { musterr("}") }
|
fn invalid01() {
|
||||||
#[test] fn invalid04() { musterr("{3a}") }
|
musterr("{")
|
||||||
#[test] fn invalid05() { musterr("{:|}") }
|
}
|
||||||
#[test] fn invalid06() { musterr("{:>>>}") }
|
#[test]
|
||||||
|
fn invalid02() {
|
||||||
|
musterr("}")
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn invalid04() {
|
||||||
|
musterr("{3a}")
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn invalid05() {
|
||||||
|
musterr("{:|}")
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn invalid06() {
|
||||||
|
musterr("{:>>>}")
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn format_nothing() {
|
fn format_nothing() {
|
||||||
same("{}", &[NextArgument(Argument {
|
same("{}",
|
||||||
|
&[NextArgument(Argument {
|
||||||
position: ArgumentNext,
|
position: ArgumentNext,
|
||||||
format: fmtdflt(),
|
format: fmtdflt(),
|
||||||
})]);
|
})]);
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn format_position() {
|
fn format_position() {
|
||||||
same("{3}", &[NextArgument(Argument {
|
same("{3}",
|
||||||
|
&[NextArgument(Argument {
|
||||||
position: ArgumentIs(3),
|
position: ArgumentIs(3),
|
||||||
format: fmtdflt(),
|
format: fmtdflt(),
|
||||||
})]);
|
})]);
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn format_position_nothing_else() {
|
fn format_position_nothing_else() {
|
||||||
same("{3:}", &[NextArgument(Argument {
|
same("{3:}",
|
||||||
|
&[NextArgument(Argument {
|
||||||
position: ArgumentIs(3),
|
position: ArgumentIs(3),
|
||||||
format: fmtdflt(),
|
format: fmtdflt(),
|
||||||
})]);
|
})]);
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn format_type() {
|
fn format_type() {
|
||||||
same("{3:a}", &[NextArgument(Argument {
|
same("{3:a}",
|
||||||
|
&[NextArgument(Argument {
|
||||||
position: ArgumentIs(3),
|
position: ArgumentIs(3),
|
||||||
format: FormatSpec {
|
format: FormatSpec {
|
||||||
fill: None,
|
fill: None,
|
||||||
|
@ -480,7 +527,8 @@ mod tests {
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn format_align_fill() {
|
fn format_align_fill() {
|
||||||
same("{3:>}", &[NextArgument(Argument {
|
same("{3:>}",
|
||||||
|
&[NextArgument(Argument {
|
||||||
position: ArgumentIs(3),
|
position: ArgumentIs(3),
|
||||||
format: FormatSpec {
|
format: FormatSpec {
|
||||||
fill: None,
|
fill: None,
|
||||||
|
@ -491,7 +539,8 @@ mod tests {
|
||||||
ty: "",
|
ty: "",
|
||||||
},
|
},
|
||||||
})]);
|
})]);
|
||||||
same("{3:0<}", &[NextArgument(Argument {
|
same("{3:0<}",
|
||||||
|
&[NextArgument(Argument {
|
||||||
position: ArgumentIs(3),
|
position: ArgumentIs(3),
|
||||||
format: FormatSpec {
|
format: FormatSpec {
|
||||||
fill: Some('0'),
|
fill: Some('0'),
|
||||||
|
@ -502,7 +551,8 @@ mod tests {
|
||||||
ty: "",
|
ty: "",
|
||||||
},
|
},
|
||||||
})]);
|
})]);
|
||||||
same("{3:*<abcd}", &[NextArgument(Argument {
|
same("{3:*<abcd}",
|
||||||
|
&[NextArgument(Argument {
|
||||||
position: ArgumentIs(3),
|
position: ArgumentIs(3),
|
||||||
format: FormatSpec {
|
format: FormatSpec {
|
||||||
fill: Some('*'),
|
fill: Some('*'),
|
||||||
|
@ -516,7 +566,8 @@ mod tests {
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn format_counts() {
|
fn format_counts() {
|
||||||
same("{:10s}", &[NextArgument(Argument {
|
same("{:10s}",
|
||||||
|
&[NextArgument(Argument {
|
||||||
position: ArgumentNext,
|
position: ArgumentNext,
|
||||||
format: FormatSpec {
|
format: FormatSpec {
|
||||||
fill: None,
|
fill: None,
|
||||||
|
@ -527,7 +578,8 @@ mod tests {
|
||||||
ty: "s",
|
ty: "s",
|
||||||
},
|
},
|
||||||
})]);
|
})]);
|
||||||
same("{:10$.10s}", &[NextArgument(Argument {
|
same("{:10$.10s}",
|
||||||
|
&[NextArgument(Argument {
|
||||||
position: ArgumentNext,
|
position: ArgumentNext,
|
||||||
format: FormatSpec {
|
format: FormatSpec {
|
||||||
fill: None,
|
fill: None,
|
||||||
|
@ -538,7 +590,8 @@ mod tests {
|
||||||
ty: "s",
|
ty: "s",
|
||||||
},
|
},
|
||||||
})]);
|
})]);
|
||||||
same("{:.*s}", &[NextArgument(Argument {
|
same("{:.*s}",
|
||||||
|
&[NextArgument(Argument {
|
||||||
position: ArgumentNext,
|
position: ArgumentNext,
|
||||||
format: FormatSpec {
|
format: FormatSpec {
|
||||||
fill: None,
|
fill: None,
|
||||||
|
@ -549,7 +602,8 @@ mod tests {
|
||||||
ty: "s",
|
ty: "s",
|
||||||
},
|
},
|
||||||
})]);
|
})]);
|
||||||
same("{:.10$s}", &[NextArgument(Argument {
|
same("{:.10$s}",
|
||||||
|
&[NextArgument(Argument {
|
||||||
position: ArgumentNext,
|
position: ArgumentNext,
|
||||||
format: FormatSpec {
|
format: FormatSpec {
|
||||||
fill: None,
|
fill: None,
|
||||||
|
@ -560,7 +614,8 @@ mod tests {
|
||||||
ty: "s",
|
ty: "s",
|
||||||
},
|
},
|
||||||
})]);
|
})]);
|
||||||
same("{:a$.b$s}", &[NextArgument(Argument {
|
same("{:a$.b$s}",
|
||||||
|
&[NextArgument(Argument {
|
||||||
position: ArgumentNext,
|
position: ArgumentNext,
|
||||||
format: FormatSpec {
|
format: FormatSpec {
|
||||||
fill: None,
|
fill: None,
|
||||||
|
@ -574,7 +629,8 @@ mod tests {
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn format_flags() {
|
fn format_flags() {
|
||||||
same("{:-}", &[NextArgument(Argument {
|
same("{:-}",
|
||||||
|
&[NextArgument(Argument {
|
||||||
position: ArgumentNext,
|
position: ArgumentNext,
|
||||||
format: FormatSpec {
|
format: FormatSpec {
|
||||||
fill: None,
|
fill: None,
|
||||||
|
@ -585,7 +641,8 @@ mod tests {
|
||||||
ty: "",
|
ty: "",
|
||||||
},
|
},
|
||||||
})]);
|
})]);
|
||||||
same("{:+#}", &[NextArgument(Argument {
|
same("{:+#}",
|
||||||
|
&[NextArgument(Argument {
|
||||||
position: ArgumentNext,
|
position: ArgumentNext,
|
||||||
format: FormatSpec {
|
format: FormatSpec {
|
||||||
fill: None,
|
fill: None,
|
||||||
|
@ -599,7 +656,9 @@ mod tests {
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn format_mixture() {
|
fn format_mixture() {
|
||||||
same("abcd {3:a} efg", &[String("abcd "), NextArgument(Argument {
|
same("abcd {3:a} efg",
|
||||||
|
&[String("abcd "),
|
||||||
|
NextArgument(Argument {
|
||||||
position: ArgumentIs(3),
|
position: ArgumentIs(3),
|
||||||
format: FormatSpec {
|
format: FormatSpec {
|
||||||
fill: None,
|
fill: None,
|
||||||
|
@ -609,6 +668,7 @@ mod tests {
|
||||||
width: CountImplied,
|
width: CountImplied,
|
||||||
ty: "a",
|
ty: "a",
|
||||||
},
|
},
|
||||||
}), String(" efg")]);
|
}),
|
||||||
|
String(" efg")]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue