diff options
Diffstat (limited to 'src/util.rs')
| -rw-r--r-- | src/util.rs | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/util.rs b/src/util.rs index 6a22031..02c282c 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,6 +1,7 @@ use std::cmp::{min, max}; + pub fn clamp<T: Ord>(val: T, lower: T, upper: T) -> T{ max(min(val, upper), lower) } @@ -14,6 +15,30 @@ pub fn strip_prefix<'a>(txt: &'a str, prefix: &'a str) -> Option<&'a str> { } } +use std::fs; +use std::path::Path; +use crate::{ + errors::AnyError, + aerr +}; + +pub fn write_file_safe<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> Result<(), AnyError> { + let temppath = path + .as_ref() + .with_file_name( + format!( + "tempfile_{}_{}.tmp", + path.as_ref().file_name().ok_or(aerr!("writing to directory"))?.to_str().unwrap_or("invalid"), + rand::random::<u64>() + ) + ); + fs::write(&temppath, contents)?; + fs::rename(&temppath, path)?; + Ok(()) +} + + + #[macro_export] macro_rules! hashmap { ( $($key:expr => $value:expr ),* ) => {{ |
