summaryrefslogtreecommitdiff
path: root/src/util.rs
blob: 02c282c1807a9fafb837587e19eb385301961119 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

use std::cmp::{min, max};


pub fn clamp<T: Ord>(val: T, lower: T, upper: T) -> T{
	max(min(val, upper), lower)
}


pub fn strip_prefix<'a>(txt: &'a str, prefix: &'a str) -> Option<&'a str> {
	if txt.starts_with(prefix) {
		Some(txt.split_at(prefix.len()).1)
	} else {
		None
	}
}

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 ),* ) => {{
		#[allow(unused_mut)]
		let mut h = std::collections::HashMap::new();
		$(
			h.insert($key, $value);
		)*
		h
	}}
}

#[cfg(test)]
mod tests {
	use std::collections::HashMap;
	#[test]
	fn test_hashmap_macro() {
		let mut h = hashmap!("hello" => 1, "world" => 2);
		assert_eq!(h.remove("hello"), Some(1));
		assert_eq!(h.remove("world"), Some(2));
		assert!(h.is_empty());
		let h2: HashMap<i32, usize> = hashmap!();
		assert!(h2.is_empty());
		assert_eq!(h2, HashMap::new());
		
	}
}