From c905e20b360fd3cb2b161eaf57b66eee1dd4c520 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Sun, 11 Jun 2023 00:18:28 -0400 Subject: [PATCH] Miscellaneous cleanups --- src/connection/builder.rs | 3 +++ src/gemtext/parser.rs | 28 +++++++++----------- src/mailstore/filesystem.rs | 2 +- src/mailstore/mod.rs | 2 +- src/message/parser.rs | 18 ++++++------- src/prelude.rs | 2 +- src/time/mod.rs | 52 ++++++++++++++++++------------------- src/time/parser.rs | 46 ++++++++++++++++---------------- 8 files changed, 73 insertions(+), 80 deletions(-) diff --git a/src/connection/builder.rs b/src/connection/builder.rs index 5720f0f..0ece67a 100644 --- a/src/connection/builder.rs +++ b/src/connection/builder.rs @@ -18,15 +18,18 @@ impl Default for Builder { } impl Builder { + #[must_use] pub fn new() -> Self { Self::default() } + #[must_use] pub fn stream(mut self, stream: TcpStream) -> Self { self.stream = Some(stream); self } + #[must_use] pub fn verifier(mut self, verifier: Verifier) -> Self { self.verifier = Some(verifier); self diff --git a/src/gemtext/parser.rs b/src/gemtext/parser.rs index 3ae7bdd..a80fa40 100644 --- a/src/gemtext/parser.rs +++ b/src/gemtext/parser.rs @@ -7,8 +7,9 @@ pub struct PreBlk<'a> { lines: Vec<&'a str>, } -#[derive(Debug)] +#[derive(Debug, Default)] enum State<'a> { + #[default] Normal, Preformatted(PreBlk<'a>), Quote(Vec<&'a str>), @@ -42,8 +43,8 @@ impl fmt::Display for GemtextNode { Self::ListItem(l) => writeln!(f, "* {l}"), Self::Quote(q) => writeln!(f, "> {q}"), Self::Preformatted(a, p) => match a { - None => writeln!(f, "```\n{}\n```", p), - Some(alt) => writeln!(f, "```{alt}\n{}\n```", p), + None => writeln!(f, "```\n{p}\n```"), + Some(alt) => writeln!(f, "```{alt}\n{p}\n```"), }, Self::Link(l) => writeln!(f, "=> {l}"), } @@ -107,7 +108,7 @@ impl<'a> GemtextNode { } fn parse_timestamp(text: &'a str) -> Self { - let Some(line) = text.strip_prefix('@').map(|x| x.trim()) else { + let Some(line) = text.strip_prefix('@').map(str::trim) else { return Self::Text(text.to_string()); }; if let Ok(dt) = line.parse() { @@ -118,7 +119,7 @@ impl<'a> GemtextNode { } } -#[derive(Debug)] +#[derive(Debug, Default)] pub struct Parser<'a> { state: State<'a>, title: Option, @@ -127,11 +128,7 @@ pub struct Parser<'a> { impl<'a> Parser<'a> { pub fn new() -> Self { - Self { - state: State::Normal, - title: None, - lines: vec![], - } + Self::default() } pub fn parse(mut self, raw: &'a str) -> Vec { @@ -146,7 +143,7 @@ impl<'a> Parser<'a> { State::Normal => {} State::Preformatted(_) => self.leave_preformatted(), State::Quote(q) => { - let quote = q.join("\n").to_string(); + let quote = q.join("\n"); self.lines.push(GemtextNode::Quote(quote)); } } @@ -160,9 +157,8 @@ impl<'a> Parser<'a> { fn heading(&mut self, line: &'a str) { let line = GemtextNode::parse_heading(line); if self.title.is_none() { - match &line { - GemtextNode::Heading1(t) => self.title = Some(t.clone()), - _ => {} + if let GemtextNode::Heading1(t) = &line { + self.title = Some(t.clone()); } } self.lines.push(line); @@ -182,7 +178,7 @@ impl<'a> Parser<'a> { fn leave_quote(&mut self, line: &'a str) { match &mut self.state { State::Quote(q) => { - let quote = q.join("\n").to_string(); + let quote = q.join("\n"); self.lines.push(GemtextNode::Quote(quote)); } _ => panic!("Attempt to parse as quote when not in quote mode"), @@ -204,7 +200,7 @@ impl<'a> Parser<'a> { fn leave_preformatted(&mut self) { match &self.state { State::Preformatted(v) => { - let s = v.lines.join("\n").to_string(); + let s = v.lines.join("\n"); self.lines .push(GemtextNode::Preformatted(v.alt.map(str::to_string), s)); self.state = State::Normal; diff --git a/src/mailstore/filesystem.rs b/src/mailstore/filesystem.rs index 5308d1a..1d2e133 100644 --- a/src/mailstore/filesystem.rs +++ b/src/mailstore/filesystem.rs @@ -81,7 +81,7 @@ impl MailStore for Filesystem { self.get_folder(user, folder).and_then(|f| { f.messages .values() - .find(|m| m.title.as_ref().map(String::as_str) == Some(title)) + .find(|m| m.title.as_deref() == Some(title)) .cloned() }) } diff --git a/src/mailstore/mod.rs b/src/mailstore/mod.rs index 976217f..e167f79 100644 --- a/src/mailstore/mod.rs +++ b/src/mailstore/mod.rs @@ -103,7 +103,7 @@ impl MailStore for Domain { u.folders.get(folder).and_then(|f| { f.messages .values() - .find(|m| m.title.as_ref().map(String::as_str) == Some(title)) + .find(|m| m.title.as_deref() == Some(title)) }) }) .cloned() diff --git a/src/message/parser.rs b/src/message/parser.rs index dd5ef49..727184f 100644 --- a/src/message/parser.rs +++ b/src/message/parser.rs @@ -16,30 +16,28 @@ pub struct Parser { impl Parser { pub fn new(id: &str) -> Self { - let mut p = Self::default(); - p.id = id.to_string(); - p + Self { id: id.to_string(), ..Default::default() } } pub fn parse(mut self, content: &str) -> Result { let lines = content.lines(); for l in lines { match l { - s if s.starts_with("<") && self.from.is_none() && self.body.is_empty() => { + s if s.starts_with('<') && self.from.is_none() && self.body.is_empty() => { let s = s.strip_prefix('<').unwrap().trim(); let from: Mailbox = s.parse()?; self.from = Some(from); } - s if s.starts_with("<") && self.body.is_empty() => { + s if s.starts_with('<') && self.body.is_empty() => { let sndr = s.strip_prefix('<').unwrap().trim(); let from: Mailbox = sndr.parse()?; self.senders.push(from); } - s if s.starts_with(":") && self.body.is_empty() => { + s if s.starts_with(':') && self.body.is_empty() => { self.recipients = s.parse()?; } - s if s.starts_with("@") && self.timestamp.is_none() && self.body.is_empty() => { - self.timestamp = Some(s.strip_prefix("@").unwrap().trim().to_string()); + s if s.starts_with('@') && self.timestamp.is_none() && self.body.is_empty() => { + self.timestamp = Some(s.strip_prefix('@').unwrap().trim().to_string()); } s if s.starts_with("###") && self.title.is_none() => { if let Some(t) = s.strip_prefix("###").map(|x| x.trim().to_string()) { @@ -59,8 +57,8 @@ impl Parser { } self.body.push_str(s); } - s if s.starts_with("#") && self.title.is_none() => { - if let Some(t) = s.strip_prefix("#").map(|x| x.trim().to_string()) { + s if s.starts_with('#') && self.title.is_none() => { + if let Some(t) = s.strip_prefix('#').map(|x| x.trim().to_string()) { self.title = Some(t); } if !self.body.is_empty() { diff --git a/src/prelude.rs b/src/prelude.rs index 8b42b9f..98c50ad 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -14,5 +14,5 @@ pub use super::{ AuthenticationFailure, Error as ParseStatusError, PermanentFailure, Redirect, Status, TemporaryFailure, }, - time::{Error as ParseTimeError, DateTime, TimeZone, Sign, Parser as TimeParser}, + time::{DateTime, Error as ParseTimeError, Parser as TimeParser, Sign, TimeZone}, }; diff --git a/src/time/mod.rs b/src/time/mod.rs index c7a57d5..6d64680 100644 --- a/src/time/mod.rs +++ b/src/time/mod.rs @@ -132,35 +132,33 @@ impl DateTime { pub fn normalize(&self) -> Option { if self.tz == Some(TimeZone::UTC) { Some(*self) - } else { - if let Some(TimeZone::Offset(tz)) = self.tz { - let hour = if let Some(hour) = self.hour { - match tz.sign { - Sign::Positive => Some(hour + tz.hours), - Sign::Negative => Some(hour - tz.hours), - } - } else { - None - }; - let minute = 'blk: { - if let Some(minutes) = self.minute { - if let Some(o) = tz.minutes { - match tz.sign { - Sign::Positive => break 'blk Some(minutes + o), - Sign::Negative => break 'blk Some(minutes - o), - } - } - } - None - }; - Some(DateTime { - hour, - minute, - ..*self - }) + } else if let Some(TimeZone::Offset(tz)) = self.tz { + let hour = if let Some(hour) = self.hour { + match tz.sign { + Sign::Positive => Some(hour + tz.hours), + Sign::Negative => Some(hour - tz.hours), + } } else { None - } + }; + let minute = 'blk: { + if let Some(minutes) = self.minute { + if let Some(o) = tz.minutes { + match tz.sign { + Sign::Positive => break 'blk Some(minutes + o), + Sign::Negative => break 'blk Some(minutes - o), + } + } + } + None + }; + Some(DateTime { + hour, + minute, + ..*self + }) + } else { + None } } } diff --git a/src/time/parser.rs b/src/time/parser.rs index 8260a9d..7ac4bf2 100644 --- a/src/time/parser.rs +++ b/src/time/parser.rs @@ -57,7 +57,8 @@ impl<'a> Parser<'a> { let max = match self.month { Some(1 | 3 | 5 | 7 | 10 | 12) => 31, Some(2) => { - if self.year.unwrap() % 4 == 0 { + let year = self.year.unwrap(); + if year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) { 29 } else { 28 @@ -175,10 +176,9 @@ impl<'a> Parser<'a> { Mode::Month => { if self.month.is_some() { return Err(Error::UnexpectedChar(self.mode, '-')); - } else { - self.format = Format::Extended; - self.sep = true; } + self.format = Format::Extended; + self.sep = true; } Mode::Day => { if self.day.is_some() || self.format == Format::Basic { @@ -188,11 +188,11 @@ impl<'a> Parser<'a> { } } Mode::Hour | Mode::Minute | Mode::Second => { - if !self.buffer.is_empty() { - return Err(Error::UnexpectedChar(self.mode, '-')); - } else { + if self.buffer.is_empty() { self.buffer.push('-'); self.mode = Mode::TimeZone; + } else { + return Err(Error::UnexpectedChar(self.mode, '-')); } } Mode::TimeZone => { @@ -215,16 +215,14 @@ impl<'a> Parser<'a> { Mode::Hour | Mode::Minute | Mode::Second => { if !self.buffer.is_empty() || self.format == Format::Basic { return Err(Error::UnexpectedChar(self.mode, ':')); - } else { - self.sep = true; } + self.sep = true; } Mode::TimeZone => { if !self.buffer.len() == 2 || self.format == Format::Basic { return Err(Error::UnexpectedChar(self.mode, ':')); - } else { - self.sep = true; } + self.sep = true; } Mode::Finish => return Err(Error::TrailingGarbage), } @@ -233,11 +231,10 @@ impl<'a> Parser<'a> { fn tee(&mut self) -> Result<(), Error> { if self.mode != Mode::Hour || !self.buffer.is_empty() { - Err(Error::UnexpectedChar(self.mode, 'T')) - } else { - self.sep = true; - Ok(()) + return Err(Error::UnexpectedChar(self.mode, 'T')); } + self.sep = true; + Ok(()) } fn zed(&mut self) -> Result<(), Error> { @@ -247,21 +244,22 @@ impl<'a> Parser<'a> { || !self.buffer.is_empty() { return Err(Error::UnexpectedChar(self.mode, 'Z')); - } else { - self.tz = Some(TimeZone::UTC); - self.mode = Mode::Finish; } + self.tz = Some(TimeZone::UTC); + self.mode = Mode::Finish; Ok(()) } fn plus(&mut self) -> Result<(), Error> { - if self.mode != Mode::TimeZone && self.mode != Mode::Finish || !self.buffer.is_empty() { + if self.mode != Mode::TimeZone && self.mode != Mode::Finish + || !self.buffer.is_empty() + || self.mode == Mode::Year + || self.mode == Mode::Month + || self.mode == Mode::Day + { return Err(Error::UnexpectedChar(self.mode, '+')); - } else if self.mode == Mode::Year || self.mode == Mode::Month || self.mode == Mode::Day { - return Err(Error::UnexpectedChar(self.mode, '+')); - } else { - self.buffer.push('+'); } + self.buffer.push('+'); Ok(()) } @@ -289,7 +287,7 @@ impl<'a> Parser<'a> { sign, hours, minutes, - })) + })); } } Ok(())