Simplify if-statements
This commit is contained in:
parent
50714395c2
commit
3034a7cf52
|
@ -179,11 +179,10 @@ impl FromStr for Datetime {
|
||||||
chars.clone().next() == Some('T') {
|
chars.clone().next() == Some('T') {
|
||||||
chars.next();
|
chars.next();
|
||||||
true
|
true
|
||||||
} else if full_date.is_none() {
|
|
||||||
true
|
|
||||||
} else {
|
} else {
|
||||||
false
|
full_date.is_none()
|
||||||
};
|
};
|
||||||
|
|
||||||
let time = if partial_time {
|
let time = if partial_time {
|
||||||
let h1 = digit(&mut chars)?;
|
let h1 = digit(&mut chars)?;
|
||||||
let h2 = digit(&mut chars)?;
|
let h2 = digit(&mut chars)?;
|
||||||
|
@ -299,7 +298,7 @@ impl FromStr for Datetime {
|
||||||
|
|
||||||
fn digit(chars: &mut str::Chars) -> Result<u8, DatetimeParseError> {
|
fn digit(chars: &mut str::Chars) -> Result<u8, DatetimeParseError> {
|
||||||
match chars.next() {
|
match chars.next() {
|
||||||
Some(c) if '0' <= c && c <= '9' => Ok(c as u8 - '0' as u8),
|
Some(c) if '0' <= c && c <= '9' => Ok(c as u8 - b'0'),
|
||||||
_ => Err(DatetimeParseError { _private: () }),
|
_ => Err(DatetimeParseError { _private: () }),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
10
src/de.rs
10
src/de.rs
|
@ -687,7 +687,7 @@ impl<'a> Deserializer<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn number_or_date(&mut self, s: &'a str) -> Result<Value<'a>, Error> {
|
fn number_or_date(&mut self, s: &'a str) -> Result<Value<'a>, Error> {
|
||||||
if s.contains("T") || (s.len() > 1 && s[1..].contains("-")) &&
|
if s.contains('T') || (s.len() > 1 && s[1..].contains('-')) &&
|
||||||
!s.contains("e-") {
|
!s.contains("e-") {
|
||||||
self.datetime(s, false).map(Value::Datetime)
|
self.datetime(s, false).map(Value::Datetime)
|
||||||
} else if self.eat(Token::Colon)? {
|
} else if self.eat(Token::Colon)? {
|
||||||
|
@ -698,7 +698,7 @@ impl<'a> Deserializer<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn number(&mut self, s: &'a str) -> Result<Value<'a>, Error> {
|
fn number(&mut self, s: &'a str) -> Result<Value<'a>, Error> {
|
||||||
if s.contains("e") || s.contains("E") {
|
if s.contains('e') || s.contains('E') {
|
||||||
self.float(s, None).map(Value::Float)
|
self.float(s, None).map(Value::Float)
|
||||||
} else if self.eat(Token::Period)? {
|
} else if self.eat(Token::Period)? {
|
||||||
let at = self.tokens.current();
|
let at = self.tokens.current();
|
||||||
|
@ -727,7 +727,7 @@ impl<'a> Deserializer<'a> {
|
||||||
if suffix != "" {
|
if suffix != "" {
|
||||||
return Err(self.error(start, ErrorKind::NumberInvalid))
|
return Err(self.error(start, ErrorKind::NumberInvalid))
|
||||||
}
|
}
|
||||||
prefix.replace("_", "").trim_left_matches("+").parse().map_err(|_e| {
|
prefix.replace("_", "").trim_left_matches('+').parse().map_err(|_e| {
|
||||||
self.error(start, ErrorKind::NumberInvalid)
|
self.error(start, ErrorKind::NumberInvalid)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -789,7 +789,7 @@ impl<'a> Deserializer<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut exponent = None;
|
let mut exponent = None;
|
||||||
if suffix.starts_with("e") || suffix.starts_with("E") {
|
if suffix.starts_with('e') || suffix.starts_with('E') {
|
||||||
let (a, b) = if suffix.len() == 1 {
|
let (a, b) = if suffix.len() == 1 {
|
||||||
self.eat(Token::Plus)?;
|
self.eat(Token::Plus)?;
|
||||||
match self.next()? {
|
match self.next()? {
|
||||||
|
@ -807,7 +807,7 @@ impl<'a> Deserializer<'a> {
|
||||||
exponent = Some(a);
|
exponent = Some(a);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut number = integral.trim_left_matches("+")
|
let mut number = integral.trim_left_matches('+')
|
||||||
.chars()
|
.chars()
|
||||||
.filter(|c| *c != '_')
|
.filter(|c| *c != '_')
|
||||||
.collect::<String>();
|
.collect::<String>();
|
||||||
|
|
|
@ -563,7 +563,7 @@ impl<'a, 'b> ser::SerializeMap for SerializeTable<'a, 'b> {
|
||||||
SerializeTable::Table { ref mut key, .. } => {
|
SerializeTable::Table { ref mut key, .. } => {
|
||||||
key.truncate(0);
|
key.truncate(0);
|
||||||
*key = input.serialize(StringExtractor)?;
|
*key = input.serialize(StringExtractor)?;
|
||||||
if key.contains("\n") {
|
if key.contains('\n') {
|
||||||
return Err(Error::KeyNewline)
|
return Err(Error::KeyNewline)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -141,7 +141,7 @@ impl<'a> Tokenizer<'a> {
|
||||||
if val == "" {
|
if val == "" {
|
||||||
return Err(Error::EmptyTableKey(offset))
|
return Err(Error::EmptyTableKey(offset))
|
||||||
}
|
}
|
||||||
match src.find("\n") {
|
match src.find('\n') {
|
||||||
None => Ok(val),
|
None => Ok(val),
|
||||||
Some(i) => Err(Error::NewlineInTableKey(offset + i)),
|
Some(i) => Err(Error::NewlineInTableKey(offset + i)),
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ fn to_json(toml: toml::Value) -> Json {
|
||||||
Toml::Float(f) => doit("float", Json::String({
|
Toml::Float(f) => doit("float", Json::String({
|
||||||
let s = format!("{:.15}", f);
|
let s = format!("{:.15}", f);
|
||||||
let s = format!("{}", s.trim_right_matches('0'));
|
let s = format!("{}", s.trim_right_matches('0'));
|
||||||
if s.ends_with(".") {format!("{}0", s)} else {s}
|
if s.ends_with('.') {format!("{}0", s)} else {s}
|
||||||
})),
|
})),
|
||||||
Toml::Boolean(b) => doit("bool", Json::String(format!("{}", b))),
|
Toml::Boolean(b) => doit("bool", Json::String(format!("{}", b))),
|
||||||
Toml::Datetime(s) => doit("datetime", Json::String(s.to_string())),
|
Toml::Datetime(s) => doit("datetime", Json::String(s.to_string())),
|
||||||
|
|
Loading…
Reference in a new issue