Skip to content

Commit

Permalink
Apply clippy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Apr 14, 2024
1 parent e1094aa commit 9a46c69
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 36 deletions.
4 changes: 2 additions & 2 deletions examples/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use parse_zoneinfo::table::TableBuilder;
// ignored in this case, however this never happens in the tz database as it
// stands.
fn strip_comments(mut line: String) -> String {
line.find('#').map(|pos| line.truncate(pos));
if let Some(pos) = line.find('#') { line.truncate(pos) }
line
}

Expand All @@ -23,7 +23,7 @@ fn main() {
let parser = LineParser::new();
let mut builder = TableBuilder::new();
for line in &lines {
match parser.parse_str(&line).unwrap() {
match parser.parse_str(line).unwrap() {
Line::Zone(zone) => builder.add_zone_line(zone).unwrap(),
Line::Continuation(cont) => builder.add_continuation_line(cont).unwrap(),
Line::Rule(rule) => builder.add_rule_line(rule).unwrap(),
Expand Down
48 changes: 27 additions & 21 deletions src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ impl std::error::Error for Error {}
//
// All of these regexes use the `(?x)` flag, which means they support
// comments and whitespace directly in the regex string!
impl Default for LineParser {
fn default() -> Self {
Self::new()
}
}

impl LineParser {
pub fn new() -> Self {
LineParser {
Expand Down Expand Up @@ -962,7 +968,7 @@ impl LineParser {
fn parse_timespec_and_type(&self, input: &str) -> Result<TimeSpecAndType, Error> {
if input == "-" {
Ok(TimeSpecAndType(TimeSpec::Zero, TimeType::Wall))
} else if input.chars().all(|c| c == '-' || c.is_digit(10)) {
} else if input.chars().all(|c| c == '-' || c.is_ascii_digit()) {
Ok(TimeSpecAndType(
TimeSpec::Hours(input.parse().unwrap()),
TimeType::Wall,
Expand Down Expand Up @@ -1016,10 +1022,10 @@ impl LineParser {
}

fn parse_dayspec(&self, input: &str) -> Result<DaySpec, Error> {
if input.chars().all(|c| c.is_digit(10)) {
if input.chars().all(|c| c.is_ascii_digit()) {
Ok(DaySpec::Ordinal(input.parse().unwrap()))
} else if input.starts_with("last") {
let weekday = input[4..].parse()?;
} else if let Some(remainder) = input.strip_prefix("last") {
let weekday = remainder.parse()?;
Ok(DaySpec::Last(weekday))
} else if let Some(caps) = self.day_field.captures(input) {
let weekday = caps.name("weekday").unwrap().as_str().parse().unwrap();
Expand Down Expand Up @@ -1066,14 +1072,14 @@ impl LineParser {
};

Ok(Rule {
name: name,
from_year: from_year,
to_year: to_year,
month: month,
day: day,
time: time,
time_to_add: time_to_add,
letters: letters,
name,
from_year,
to_year,
month,
day,
time,
time_to_add,
letters,
})
} else {
Err(Error::NotParsedAsRuleLine)
Expand Down Expand Up @@ -1131,10 +1137,10 @@ impl LineParser {
};

Ok(ZoneInfo {
utc_offset: utc_offset,
saving: saving,
format: format,
time: time,
utc_offset,
saving,
format,
time,
})
}

Expand All @@ -1143,8 +1149,8 @@ impl LineParser {
let name = caps.name("name").unwrap().as_str();
let info = self.zoneinfo_from_captures(caps)?;
Ok(Zone {
name: name,
info: info,
name,
info,
})
} else {
Err(Error::NotParsedAsZoneLine)
Expand Down Expand Up @@ -1282,7 +1288,7 @@ mod tests {

#[test]
fn negative_offsets() {
static LINE: &'static str = "Zone Europe/London -0:01:15 - LMT 1847 Dec 1 0:00s";
static LINE: &str = "Zone Europe/London -0:01:15 - LMT 1847 Dec 1 0:00s";
let parser = LineParser::new();
let zone = parser.parse_zone(LINE).unwrap();
assert_eq!(
Expand All @@ -1293,7 +1299,7 @@ mod tests {

#[test]
fn negative_offsets_2() {
static LINE: &'static str =
static LINE: &str =
"Zone Europe/Madrid -0:14:44 - LMT 1901 Jan 1 0:00s";
let parser = LineParser::new();
let zone = parser.parse_zone(LINE).unwrap();
Expand All @@ -1305,7 +1311,7 @@ mod tests {

#[test]
fn negative_offsets_3() {
static LINE: &'static str = "Zone America/Danmarkshavn -1:14:40 - LMT 1916 Jul 28";
static LINE: &str = "Zone America/Danmarkshavn -1:14:40 - LMT 1916 Jul 28";
let parser = LineParser::new();
let zone = parser.parse_zone(LINE).unwrap();
assert_eq!(
Expand Down
10 changes: 5 additions & 5 deletions src/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl Structure for Table {
}
}

TableStructure { mappings: mappings }
TableStructure { mappings }
}
}

Expand All @@ -94,7 +94,7 @@ impl<'table> IntoIterator for TableStructure<'table> {

Iter {
structure: self,
keys: keys,
keys,
}
}
}
Expand Down Expand Up @@ -182,7 +182,7 @@ mod test {
assert_eq!(
structure.next(),
Some(TableStructureEntry {
name: &"a".to_owned(),
name: "a",
children: vec![Child::TimeZone("b")]
})
);
Expand All @@ -200,14 +200,14 @@ mod test {
assert_eq!(
structure.next(),
Some(TableStructureEntry {
name: &"a".to_owned(),
name: "a",
children: vec![Child::Submodule("b"), Child::TimeZone("e")]
})
);
assert_eq!(
structure.next(),
Some(TableStructureEntry {
name: &"a/b".to_owned(),
name: "a/b",
children: vec![Child::TimeZone("c"), Child::TimeZone("d")]
})
);
Expand Down
10 changes: 8 additions & 2 deletions src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl Table {
Some(&*self.zonesets[zone_name])
} else if self.links.contains_key(zone_name) {
let target = &self.links[zone_name];
Some(&*self.zonesets[&*target])
Some(&*self.zonesets[target])
} else {
None
}
Expand Down Expand Up @@ -285,6 +285,12 @@ pub struct TableBuilder {
current_zoneset_name: Option<String>,
}

impl Default for TableBuilder {
fn default() -> Self {
Self::new()
}
}

impl TableBuilder {
/// Creates a new builder with an empty table.
pub fn new() -> TableBuilder {
Expand Down Expand Up @@ -342,7 +348,7 @@ impl TableBuilder {
.table
.rulesets
.entry(rule_line.name.to_owned())
.or_insert_with(Vec::new);
.or_default();

ruleset.push(rule_line.into());
self.current_zoneset_name = None;
Expand Down
13 changes: 7 additions & 6 deletions src/transitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ impl TableTransitions for Table {
}

Saving::Multiple(ref rules) => {
let rules = &self.rulesets[&*rules];
let rules = &self.rulesets[rules];
builder.add_multiple_saving(
zone_info,
&*rules,
rules,
&mut dst_offset,
use_until,
utc_offset,
Expand Down Expand Up @@ -262,21 +262,22 @@ impl FixedTimespanSetBuilder {
let timespan = FixedTimespan {
utc_offset: timespan.offset,
dst_offset: *dst_offset,
name: start_zone_id.clone().unwrap_or_else(String::new),
name: start_zone_id.clone().unwrap_or_default(),
};

self.rest.push((time, timespan));
*insert_start_transition = false;
} else {
self.first = Some(FixedTimespan {
utc_offset: utc_offset,
utc_offset,
dst_offset: *dst_offset,
name: start_zone_id.clone().unwrap_or_else(String::new),
name: start_zone_id.clone().unwrap_or_default(),
});
}
}

#[allow(unused_results)]
#[allow(clippy::too_many_arguments)]
fn add_multiple_saving(
&mut self,
timespan: &ZoneInfo,
Expand Down Expand Up @@ -391,7 +392,7 @@ impl FixedTimespanSetBuilder {
};

let mut zoneset = FixedTimespanSet {
first: first,
first,
rest: self.rest,
};
optimise(&mut zoneset);
Expand Down

0 comments on commit 9a46c69

Please sign in to comment.