From 9379d064507684a29de700ab93d96c9f1da15422 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Sat, 20 Mar 2021 03:31:16 -0500 Subject: [PATCH] maildir updates --- maildir/src/lib.rs | 28 ++++++++++++++++++++-------- src/config.rs | 2 +- src/main.rs | 2 +- src/ui/mail_view.rs | 4 ++-- src/ui/mod.rs | 2 +- 5 files changed, 25 insertions(+), 13 deletions(-) diff --git a/maildir/src/lib.rs b/maildir/src/lib.rs index d9440e7..e5f1491 100644 --- a/maildir/src/lib.rs +++ b/maildir/src/lib.rs @@ -1,8 +1,8 @@ use std::path::{Path, PathBuf}; -use tokio::fs::{File, self}; use anyhow::Result; use tempfile::NamedTempFile; +use tokio::fs::{self, File, OpenOptions}; pub struct Maildir { path: PathBuf, @@ -18,16 +18,19 @@ impl Maildir { /// Stores a new message into the `new` directory // TODO: maybe have a streaming option? - pub async fn store(&self) -> Result<()> { - let unique_name = "hellosu"; - let tmp_file = self.tmp_dir().join(unique_name); + pub async fn store(&self, opts: StoreOptions) -> Result { + let unique_name = opts.create_unique_name(); + let tmp_file = self.tmp_dir().join(&unique_name); { - let mut file = File::create(&tmp_file).await?; + let mut file = OpenOptions::new() + .create_new(true) // fail if the file already exists, this means we aren't unique! + .open(&tmp_file) + .await?; } - let new_file = self.new_dir().join(unique_name); - fs::rename(tmp_file, new_file).await?; - Ok(()) + let new_file = self.new_dir().join(&unique_name); + fs::rename(&tmp_file, &new_file).await?; + Ok(new_file) } /// Returns the path to the `tmp` subdirectory @@ -48,3 +51,12 @@ impl Maildir { self.path.join("cur") } } + +/// Options that will be used to determine the filename it's stored to +pub struct StoreOptions {} + +impl StoreOptions { + pub fn create_unique_name(&self) -> String { + format!("") + } +} diff --git a/src/config.rs b/src/config.rs index cc0dbd0..15f94e3 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,8 +2,8 @@ //! //! One of the primary goals of panorama is to be able to always hot-reload configuration files. -use std::fs::{self, File}; use std::collections::HashMap; +use std::fs::{self, File}; use std::io::Read; use std::path::{Path, PathBuf}; diff --git a/src/main.rs b/src/main.rs index b44f23b..54427d1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use std::thread; use anyhow::Result; use fern::colors::{Color, ColoredLevelConfig}; -use futures::future::{TryFutureExt}; +use futures::future::TryFutureExt; use panorama::{ config::spawn_config_watcher_system, mail::{self, MailEvent}, diff --git a/src/ui/mail_view.rs b/src/ui/mail_view.rs index 38b520a..6bc1592 100644 --- a/src/ui/mail_view.rs +++ b/src/ui/mail_view.rs @@ -7,8 +7,8 @@ use std::sync::{ use anyhow::Result; use chrono::{DateTime, Datelike, Duration, Local}; use chrono_humanize::HumanTime; +use crossterm::event::{KeyCode, KeyEvent}; use panorama_imap::response::Envelope; -use crossterm::event::{KeyEvent, KeyCode}; use tui::{ buffer::Buffer, layout::{Constraint, Direction, Layout, Rect}, @@ -19,7 +19,7 @@ use tui::{ use crate::mail::EmailMetadata; -use super::{FrameType, HandlesInput, TermType, InputResult, Window, UI}; +use super::{FrameType, HandlesInput, InputResult, TermType, Window, UI}; #[derive(Default, Debug)] pub struct MailView { diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 1b0131b..039708f 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -288,7 +288,7 @@ pub async fn run_ui( let mut should_pop = false; if let Some(input_state) = input_states.last_mut() { match input_state.handle_key(&mut term, evt)? { - InputResult::Ok => {}, + InputResult::Ok => {} InputResult::Push(state) => { input_states.push(state); }