Skip to content

Commit

Permalink
Clippy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dantleech committed Nov 12, 2024
1 parent b05eb60 commit 2afa2d1
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/component/activity_list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl View for ActivityList {
);

self.cursor_pos = Some((
1 + rect.x + ((app.activity_list.filter_text_area.visual_cursor() as u16).max(scroll) - scroll) as u16,
1 + rect.x + ((app.activity_list.filter_text_area.visual_cursor() as u16).max(scroll) - scroll),
rect.y + 1,
));

Expand Down
1 change: 0 additions & 1 deletion src/component/polyline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub fn draw(
f: &mut Buffer,
area: tui::layout::Rect,
) {
if app.activity.is_none() {}
let activity = app.activity.clone().unwrap();

if activity.summary_polyline.is_none() {
Expand Down
40 changes: 23 additions & 17 deletions src/expr/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub enum Evalue {
impl Evalue {
fn to_bool(&self) -> bool {
match self {
Evalue::String(v) => v != "" && v != "0",
Evalue::String(v) => !v.is_empty() && v != "0",
Evalue::Number(n) => *n != 0.0,
Evalue::Bool(b) => *b,
Evalue::Date(_) => true,
Expand All @@ -38,6 +38,12 @@ impl Evalue {
}
}

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

impl Evaluator {
pub fn new() -> Evaluator {
Evaluator {}
Expand All @@ -53,7 +59,7 @@ impl Evaluator {
}

pub fn evaluate(&self, expr: &Expr, vars: &Vars) -> Result<bool, String> {
match self.evaluate_expr(&expr, vars)? {
match self.evaluate_expr(expr, vars)? {
Evalue::Number(n) => {
Err(format!("expression must evaluate to a boolean, got {:?}: {:?}", expr, n).to_string())
}
Expand All @@ -68,7 +74,7 @@ impl Evaluator {
match expr {
Expr::Boolean(b) => Ok(Evalue::Bool(*b)),
Expr::String(s) => Ok(Evalue::String(s.clone())),
Expr::Date(s) => Ok(Evalue::Date(s.clone())),
Expr::Date(s) => Ok(Evalue::Date(*s)),
Expr::Quantity(expr, unit) => {
let val = match self.evaluate_expr(expr, vars)? {
Evalue::Number(n) => Ok(n),
Expand Down Expand Up @@ -110,14 +116,14 @@ mod test {
#[test]
fn test_evaluate() {
let result = Evaluator::new().parse_and_evaluate("false", &HashMap::new());
assert_eq!(false, result.unwrap());
assert!(!result.unwrap());
let result = Evaluator::new().parse_and_evaluate("20 > 10", &HashMap::new());

assert_eq!(true, result.unwrap());
assert!(result.unwrap());

let result = Evaluator::new().parse_and_evaluate("20 > 10 and false", &HashMap::new());

assert_eq!(false, result.unwrap());
assert!(!result.unwrap());
}

#[test]
Expand All @@ -127,26 +133,26 @@ mod test {
("type".to_string(), Evalue::String("Run".to_string())),
]);
let result = Evaluator::new().parse_and_evaluate("distance > 5", &map);
assert_eq!(true, result.unwrap());
assert!(result.unwrap());
let result = Evaluator::new().parse_and_evaluate("distance < 5", &map);
assert_eq!(false, result.unwrap());
assert!(!result.unwrap());
let result = Evaluator::new().parse_and_evaluate("distance = 10", &map);
assert_eq!(true, result.unwrap());
assert!(result.unwrap());
let result = Evaluator::new().parse_and_evaluate("type = 'Run'", &map);
assert_eq!(true, result.unwrap());
assert!(result.unwrap());
let result = Evaluator::new().parse_and_evaluate("type ~ 'Ru'", &map);
assert_eq!(true, result.unwrap());
assert!(result.unwrap());
let result = Evaluator::new().parse_and_evaluate("type !~ 'Rup'", &map);
assert_eq!(true, result.unwrap());
assert!(result.unwrap());
let result = Evaluator::new().parse_and_evaluate("type != 'Run'", &map);
assert_eq!(false, result.unwrap());
assert!(!result.unwrap());
let result = Evaluator::new().parse_and_evaluate("2024-01-06 > 2020-01-06", &map);
assert_eq!(true, result.unwrap());
assert!(result.unwrap());
let result = Evaluator::new().parse_and_evaluate("2024-01-06 < 2020-01-06", &map);
assert_eq!(false, result.unwrap());
assert!(!result.unwrap());
let result = Evaluator::new().parse_and_evaluate("1kmph = 1000", &map);
assert_eq!(true, result.unwrap());
assert!(result.unwrap());
let result = Evaluator::new().parse_and_evaluate("1mph > 1609 and 1mph < 1610", &map);
assert_eq!(true, result.unwrap());
assert!(result.unwrap());
}
}
20 changes: 7 additions & 13 deletions src/expr/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ pub struct Lexer<'a> {
}

impl Lexer<'_> {
pub fn new<'a>(expr: &'a str) -> Lexer<'_> {
pub fn new(expr: &str) -> Lexer<'_> {
Lexer { expr, pos: 0 }
}
pub fn next(&mut self) -> Token {
self.skip_whitespace();
let c = self.current();
let t = match c {

match c {
'\0' => self.spawn_token(TokenKind::Eol, self.pos),
_ => {
if is_number(c) {
Expand Down Expand Up @@ -93,26 +94,19 @@ impl Lexer<'_> {
_ => self.spawn_advance(TokenKind::Unkown, 1),
}
}
};
t
}
}

fn advance(&mut self) {
self.pos += 1;
}

fn current(&self) -> char {
match self.expr.chars().nth(self.pos) {
Some(s) => s,
None => '\0',
}
self.expr.chars().nth(self.pos).unwrap_or('\0')
}

fn peek(&self, amount: usize) -> char {
match self.expr.chars().nth(self.pos + amount) {
Some(s) => s,
None => '\0',
}
self.expr.chars().nth(self.pos + amount).unwrap_or('\0')
}

fn parse_number_or_date(&mut self) -> Token {
Expand Down Expand Up @@ -196,7 +190,7 @@ impl Lexer<'_> {
length,
};
self.pos += length;
return t;
t
}

}
Expand Down
4 changes: 2 additions & 2 deletions src/expr/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub struct Parser<'a> {
}

impl Parser<'_> {
pub fn new<'a>(expr: &'a str) -> Parser<'a> {
pub fn new(expr: &str) -> Parser<'_> {
let lexer = Lexer::new(expr);
Parser { lexer }
}
Expand Down Expand Up @@ -91,7 +91,7 @@ impl Parser<'_> {

// suffix
if next_t.kind == TokenKind::Name {
left = Expr::Quantity(Box::new(left), QuantityUnit::try_from(self.lexer.token_value(&next_t)).unwrap());
left = Expr::Quantity(Box::new(left), QuantityUnit::from(self.lexer.token_value(&next_t)));
next_t = self.lexer.next();
if next_t.kind == TokenKind::Eol {
return Ok((left, next_t));
Expand Down
17 changes: 9 additions & 8 deletions src/store/activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ impl Iterator for Activities {
}
}

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

impl Activities {
pub fn new() -> Activities {
Self {
Expand Down Expand Up @@ -209,23 +215,18 @@ impl Activities {
pub(crate) fn by_expr(&self, evaluator: &Evaluator, expr: &Expr) -> Activities {
self.activities.clone()
.into_iter()
.filter(|a| match evaluator.evaluate(expr, &Vars::from([
.filter(|a| evaluator.evaluate(expr, &Vars::from([
("distance".to_string(), Evalue::Number(a.distance)),
("type".to_string(), Evalue::String(a.activity_type.to_string())),
("heartrate".to_string(), Evalue::Number(a.average_heartrate.unwrap_or(0.0))),
("title".to_string(), Evalue::String(a.title.clone())),
("elevation".to_string(), Evalue::Number(a.total_elevation_gain)),
("time".to_string(), Evalue::Number(a.moving_time as f64)),
("date".to_string(), Evalue::Date(
a.start_date.unwrap_or(
NaiveDateTime::default()
).try_into().unwrap()
a.start_date.unwrap_or_default().into()
)),
("speed".to_string(), Evalue::Number(a.meters_per_hour())),
])) {
Ok(v) => v,
Err(_) => false,
})
])).unwrap_or_default())
.collect()
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/sync/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ impl ActivityConverter<'_> {
"#
).fetch_all(self.pool).await?;

self.logger.info(format!("Converting activities")).await;
self.logger.info("Converting activities".to_string()).await;
let mut i = 0;
for raw_activity in raw_activities {
let listed: client::Activity =
serde_json::from_str(match &raw_activity.activity {
Some(a) => &a.as_str(),
Some(a) => a.as_str(),
None => raw_activity.listed.as_str()
}).expect("Could not decode JSON");
if i % 10 == 0 { self.logger.info(format!("Converting activity {}", listed.name)).await;}
i = i + 1;
i += 1;
let activity = Activity {
id: listed.id,
title: listed.name.clone(),
Expand Down
15 changes: 6 additions & 9 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,12 @@ fn header<'a>(app: &'a mut App, mapped_events: Vec<StravaEvent>) -> Paragraph<'a
let mut hints: Vec<Span> = vec![];
for event in mapped_events {
match app.key_map.key(&event) {
Some(k) => match k {
KeyCode::Char(c) => {
hints.push(Span::styled(
format!("[{}]", c),
Style::default().fg(strava),
));
hints.push(Span::raw(format!("{} ", StravaEvent::describe(&event))));
}
_ => (),
Some(k) => if let KeyCode::Char(c) = k {
hints.push(Span::styled(
format!("[{}]", c),
Style::default().fg(strava),
));
hints.push(Span::raw(format!("{} ", StravaEvent::describe(&event))));
},
None => continue,
}
Expand Down

0 comments on commit 2afa2d1

Please sign in to comment.