|
1 | 1 | use core::fmt::Debug; |
2 | 2 | use std::{ |
3 | 3 | collections::{BTreeMap, BTreeSet}, |
4 | | - path::{Path, PathBuf}, |
| 4 | + path::PathBuf, |
5 | 5 | time::{Duration, SystemTime}, |
6 | 6 | }; |
7 | 7 |
|
8 | 8 | use argon2::{password_hash::SaltString, Argon2, PasswordHash, PasswordHasher, PasswordVerifier}; |
9 | 9 | use eyre::Context; |
10 | 10 | use rand_core::OsRng; |
11 | 11 | 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}; |
16 | 13 |
|
17 | 14 | use crate::mirrored_to_disk::MirroredToDisk; |
18 | 15 |
|
@@ -189,51 +186,3 @@ pub struct Talk { |
189 | 186 | pub nerds: BTreeSet<usize>, |
190 | 187 | pub noobs: BTreeSet<usize>, |
191 | 188 | } |
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