Miscellaneous cleanups

This commit is contained in:
Nathan Fisher 2023-06-11 00:18:28 -04:00
parent 5c82b53a28
commit c905e20b36
8 changed files with 73 additions and 80 deletions

View file

@ -18,15 +18,18 @@ impl<S: FingerPrintStore> Default for Builder<S> {
}
impl<S: FingerPrintStore + 'static> Builder<S> {
#[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<S>) -> Self {
self.verifier = Some(verifier);
self

View file

@ -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<String>,
@ -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<GemtextNode> {
@ -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;

View file

@ -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()
})
}

View file

@ -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()

View file

@ -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<Message, super::Error> {
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() {

View file

@ -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},
};

View file

@ -132,8 +132,7 @@ impl DateTime {
pub fn normalize(&self) -> Option<Self> {
if self.tz == Some(TimeZone::UTC) {
Some(*self)
} else {
if let Some(TimeZone::Offset(tz)) = self.tz {
} 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),
@ -162,7 +161,6 @@ impl DateTime {
None
}
}
}
}
#[cfg(test)]

View file

@ -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,11 +176,10 @@ 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;
}
}
Mode::Day => {
if self.day.is_some() || self.format == Format::Basic {
return Err(Error::UnexpectedChar(self.mode, '-'));
@ -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,12 +231,11 @@ 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 {
return Err(Error::UnexpectedChar(self.mode, 'T'));
}
self.sep = true;
Ok(())
}
}
fn zed(&mut self) -> Result<(), Error> {
if self.mode == Mode::Year
@ -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;
}
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(())