Skip to content

Commit 00569b5

Browse files
committed
Restructure modules
1 parent 16a9f72 commit 00569b5

2 files changed

Lines changed: 57 additions & 58 deletions

File tree

src/mirrored_to_disk.rs

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
use core::fmt::Debug;
12
use std::{
23
ops::{Deref, DerefMut},
3-
path::PathBuf,
4+
path::{Path, PathBuf},
45
};
56

67
use eyre::Context as _;
7-
use serde::Serialize;
8-
use tokio::fs::{create_dir_all, try_exists};
8+
use serde::{Deserialize, Serialize};
9+
use tokio::{
10+
fs::{read, try_exists, File},
11+
io::AsyncWriteExt as _,
12+
};
913
use tracing::warn;
1014

11-
use crate::storage::{ReadFromFileExt, WriteToFileExt};
12-
1315
#[derive(Debug)]
1416
pub struct MirroredToDisk<T> {
1517
pub path: PathBuf,
@@ -68,3 +70,51 @@ where
6870
self.value.write_to_file(&self.path).await
6971
}
7072
}
73+
74+
pub trait ReadFromFileExt {
75+
async fn read_from_file(path: impl AsRef<Path> + Debug) -> eyre::Result<Self>
76+
where
77+
Self: Sized;
78+
}
79+
80+
impl<T> ReadFromFileExt for T
81+
where
82+
T: for<'de> Deserialize<'de>,
83+
{
84+
async fn read_from_file(path: impl AsRef<Path> + Debug) -> eyre::Result<Self> {
85+
let contents = read(path).await.wrap_err("failed to read file")?;
86+
serde_json::from_slice(&contents).wrap_err("failed to deserialize JSON")
87+
}
88+
}
89+
90+
pub trait WriteToFileExt {
91+
async fn write_to_file(&self, path: impl AsRef<Path> + Debug) -> eyre::Result<()>;
92+
}
93+
94+
impl<T> WriteToFileExt for T
95+
where
96+
T: Serialize,
97+
{
98+
async fn write_to_file(&self, path: impl AsRef<Path> + Debug) -> eyre::Result<()> {
99+
let path = path.as_ref();
100+
101+
let temp_path = path.with_extension("tmp");
102+
let contents = serde_json::to_vec_pretty(self).wrap_err("failed to serialize to JSON")?;
103+
104+
let mut file = File::create(&temp_path)
105+
.await
106+
.wrap_err("failed to create temp file")?;
107+
108+
file.write_all(&contents)
109+
.await
110+
.wrap_err("failed to write to temp file")?;
111+
112+
file.sync_all().await.wrap_err("failed to sync to disk")?;
113+
114+
tokio::fs::rename(&temp_path, path)
115+
.await
116+
.wrap_err("failed to rename temp file to target")?;
117+
118+
Ok(())
119+
}
120+
}

src/storage.rs

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
use core::fmt::Debug;
22
use std::{
33
collections::{BTreeMap, BTreeSet},
4-
path::{Path, PathBuf},
4+
path::PathBuf,
55
time::{Duration, SystemTime},
66
};
77

88
use argon2::{password_hash::SaltString, Argon2, PasswordHash, PasswordHasher, PasswordVerifier};
99
use eyre::Context;
1010
use rand_core::OsRng;
1111
use serde::{Deserialize, Serialize};
12-
use tokio::{
13-
fs::{create_dir_all, read, try_exists, File},
14-
io::AsyncWriteExt,
15-
};
12+
use tokio::fs::{create_dir_all, try_exists};
1613

1714
use crate::mirrored_to_disk::MirroredToDisk;
1815

@@ -189,51 +186,3 @@ pub struct Talk {
189186
pub nerds: BTreeSet<usize>,
190187
pub noobs: BTreeSet<usize>,
191188
}
192-
193-
pub trait ReadFromFileExt {
194-
async fn read_from_file(path: impl AsRef<Path> + Debug) -> eyre::Result<Self>
195-
where
196-
Self: Sized;
197-
}
198-
199-
impl<T> ReadFromFileExt for T
200-
where
201-
T: for<'de> Deserialize<'de>,
202-
{
203-
async fn read_from_file(path: impl AsRef<Path> + Debug) -> eyre::Result<Self> {
204-
let contents = read(path).await.wrap_err("failed to read file")?;
205-
serde_json::from_slice(&contents).wrap_err("failed to deserialize JSON")
206-
}
207-
}
208-
209-
pub trait WriteToFileExt {
210-
async fn write_to_file(&self, path: impl AsRef<Path> + Debug) -> eyre::Result<()>;
211-
}
212-
213-
impl<T> WriteToFileExt for T
214-
where
215-
T: Serialize,
216-
{
217-
async fn write_to_file(&self, path: impl AsRef<Path> + Debug) -> eyre::Result<()> {
218-
let path = path.as_ref();
219-
220-
let temp_path = path.with_extension("tmp");
221-
let contents = serde_json::to_vec_pretty(self).wrap_err("failed to serialize to JSON")?;
222-
223-
let mut file = File::create(&temp_path)
224-
.await
225-
.wrap_err("failed to create temp file")?;
226-
227-
file.write_all(&contents)
228-
.await
229-
.wrap_err("failed to write to temp file")?;
230-
231-
file.sync_all().await.wrap_err("failed to sync to disk")?;
232-
233-
tokio::fs::rename(&temp_path, path)
234-
.await
235-
.wrap_err("failed to rename temp file to target")?;
236-
237-
Ok(())
238-
}
239-
}

0 commit comments

Comments
 (0)