Fix CS and some other small things

Useful tool, that clippy...

Signed-off-by: Jacob Kiers <jacob@jacobkiers.net>
This commit is contained in:
Jacob Kiers 2022-12-14 15:34:29 +01:00
parent 21bdb9488e
commit aef3cb592a
3 changed files with 24 additions and 35 deletions

View File

@ -34,10 +34,7 @@ pub(crate) fn add_entry_to_feed(
_ => "".to_string(),
},
},
email: match &from.address {
Some(e) => Some(e.to_string()),
_ => None,
},
email: from.address.as_ref().map(|e| e.to_string()),
uri: None,
},
title: parsed
@ -46,7 +43,7 @@ pub(crate) fn add_entry_to_feed(
.to_string(),
content: Some(processed_html.clone()),
id: url.clone(),
published: Utc.timestamp_opt(date.to_timestamp(), 0).unwrap(), //(format!("{}{}", &date.to_iso8601(), "+00:00").as_str()).`unwrap(),
published: Utc.timestamp_opt(date.to_timestamp(), 0).unwrap(),
url: match include_html {
true => url,
false => "".to_string(),
@ -105,8 +102,8 @@ impl From<Newsletter> for Entry {
eb.title(post.title)
.id(post.id)
.published(Some(post.published.clone().into()))
.author(post.author.into())
.published(Some(post.published.into()))
.author(post.author)
.content(content);
if post.url.len() > 1 {

View File

@ -20,8 +20,8 @@ use std::{
pub(crate) use message::Message;
const INDEX_HTML: & 'static str = include_str!("../resources/index.html");
const FEED_STYLESHEET: & 'static str = include_str!("../resources/feed.xsl");
const INDEX_HTML: &str = include_str!("../resources/index.html");
const FEED_STYLESHEET: &str = include_str!("../resources/feed.xsl");
fn main() -> Result<(), Box<dyn Error>> {
let cli = cli::Cli::parse();
@ -41,7 +41,7 @@ fn main() -> Result<(), Box<dyn Error>> {
username.to_owned(),
password.to_owned(),
),
cli::Command::BuildFeed { filename , hostname, include_html } => build_feed(&filename, hostname.to_owned(), *include_html),
cli::Command::BuildFeed { filename, hostname, include_html } => build_feed(filename, hostname, *include_html),
_ => unimplemented!("This method is not yet implemented."),
};
@ -56,7 +56,7 @@ fn create_directory<P: AsRef<Path>>(dir: P) -> Result<(), std::io::Error> {
Ok(())
}
fn build_feed(filename: &PathBuf, hostname: String, include_html: bool) -> Result<(), Box<dyn Error>> {
fn build_feed(filename: &PathBuf, hostname: &String, include_html: bool) -> Result<(), Box<dyn Error>> {
let dir = filename.parent().ok_or(format!(
"Could not get parent directory of {}",
filename.display()
@ -76,7 +76,7 @@ fn build_feed(filename: &PathBuf, hostname: String, include_html: bool) -> Resul
let mut feed = feed::build_atom_feed(&hostname, feed_file);
let mut reader = DataDirectoryMessageReader::new((&Path::new("data")).to_path_buf());
let mut reader = DataDirectoryMessageReader::new(Path::new("data").to_path_buf());
for msg in reader.read_rfc822_messages() {
let parsed = msg.get_parsed().expect("A parsed messsage.");
@ -86,15 +86,12 @@ fn build_feed(filename: &PathBuf, hostname: String, include_html: bool) -> Resul
msg.get_uid()
))?;
let subject = match parsed.subject() {
Some(subject) => subject,
None => "No subject",
};
let subject = parsed.subject().unwrap_or("No subject");
println!(
"Processing message {} from {} with subject {}",
msg.get_uid(),
date.to_string(),
date,
subject
);
@ -109,7 +106,7 @@ fn build_feed(filename: &PathBuf, hostname: String, include_html: bool) -> Resul
feed::add_entry_to_feed(&mut feed, &msg, &processed_html, &hostname, include_html);
}
if feed.entries.len() > 0 {
if !feed.entries.is_empty() {
feed.set_updated(Utc::now());
println!("Writing feed to {}", filename.display());
@ -136,10 +133,10 @@ fn fetch_from_imap(
print!("Getting mail from {} for mailbox {}", server, username);
let mut reader = ImapReader::new(
String::from(server),
server,
port,
String::from(username),
String::from(password),
username,
password,
);
for msg in reader.read_rfc822_messages() {
@ -153,15 +150,12 @@ fn fetch_from_imap(
msg.get_uid()
))?;
let subject = match parsed.subject() {
Some(subject) => subject,
None => "No subject",
};
let subject = parsed.subject().unwrap_or("No subject");
println!(
"Processing message {} from {} with subject {}",
msg.get_uid(),
date.to_string(),
date,
subject
);
@ -191,14 +185,14 @@ fn get_path(parsed: &ParsedMessage, msg: &Message) -> String {
);
let hash = base16ct::lower::encode_string(&Sha256::digest(
&parsed.body_html(0).expect("Expected a body").as_bytes(),
parsed.body_html(0).expect("Expected a body").as_bytes(),
));
let uid: i32 = msg.get_uid()
.parse()
.expect(&format!("Could not convert message id {} to an i32.", msg.get_uid()));
.unwrap_or_else(|_| panic!("Could not convert message id {} to an i32.", msg.get_uid()));
format!("{:05}_{}_{}.html", uid, date_str, &hash).to_owned()
format!("{:05}_{}_{}.html", uid, date_str, &hash)
}
fn process_html(input: &str) -> Result<String, ()> {
@ -211,6 +205,6 @@ fn write_file<P: Into<PathBuf>, D: AsRef<[u8]>>(html_path: P, data: D) -> Result
.write(true)
.create(true)
.open(&path)
.expect(format!("Could not open file '{}' for writing", &path.display()).as_str())
.unwrap_or_else(|_| panic!("Could not open file '{}' for writing", &path.display()))
.write_all(data.as_ref())
}

View File

@ -44,7 +44,7 @@ impl EmailReader for DataDirectoryMessageReader {
Some(ext) => ext == "eml",
None => false,
})
.map(|i| {
.filter_map(|i| {
let uid = i
.path()
.file_stem()
@ -52,9 +52,9 @@ impl EmailReader for DataDirectoryMessageReader {
.to_owned()
.into_string()
.expect("Could not convert filename to string.")
.split("_")
.split('_')
.collect::<Vec<&str>>()[0]
.trim_start_matches("0")
.trim_start_matches('0')
.to_string();
if let Ok(data) = std::fs::read(i.path()) {
@ -63,8 +63,6 @@ impl EmailReader for DataDirectoryMessageReader {
None
}
})
.filter(|i| i.is_some())
.map(|i| i.unwrap())
.map(|i| Message::new(i.0, i.1));
let iter = items.collect::<Vec<Message>>().into_iter();