refactor: Use regular functions rather than FromStr impls

Implementing `FromStr` and then calling `parse()` creates an
indirection, which is hard to follow for people who are not familiar
with Rust. r10s recently had this problem.
This commit is contained in:
Hocuri
2026-04-26 09:43:51 +02:00
parent 5f1d54100f
commit 47b94f3f56
4 changed files with 20 additions and 27 deletions

View File

@@ -66,7 +66,6 @@ use std::cmp::max;
use std::collections::BTreeSet;
use std::fmt;
use std::num::ParseIntError;
use std::str::FromStr;
use std::time::{Duration, UNIX_EPOCH};
use anyhow::{Context as _, Result, ensure};
@@ -124,6 +123,12 @@ impl Timer {
Self::Enabled { duration }
}
}
/// Tries to parse a string as an integer,
/// and converts it to an ephemeral timer value.
pub fn from_str(input: &str) -> Result<Timer, ParseIntError> {
input.parse::<u32>().map(Self::from_u32)
}
}
impl fmt::Display for Timer {
@@ -132,14 +137,6 @@ impl fmt::Display for Timer {
}
}
impl FromStr for Timer {
type Err = ParseIntError;
fn from_str(input: &str) -> Result<Timer, ParseIntError> {
input.parse::<u32>().map(Self::from_u32)
}
}
impl rusqlite::types::ToSql for Timer {
fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {
let val = rusqlite::types::Value::Integer(match self {