Debian Codegraph @

src/path/temp_path.rs

raw
1 use crate::path::from_path::FromPath;
2 use rand::distr::Alphanumeric;
3 use rand::{rng, Rng};
4 use std::env;
5 use std::iter;
6
7 #[cfg(windows)]
8 fn get_extra_path() -> Option<String> {
9 let name = env!("CARGO_PKG_NAME").to_string();
10 Some(name)
11 }
12
13 #[cfg(not(windows))]
14 fn get_extra_path() -> Option<String> {
15 None
16 }
17
18 fn get_random_string() -> String {
19 let mut rng_inst = rng();
20 iter::repeat(())
21 .map(|()| rng_inst.sample(Alphanumeric))
22 .map(char::from)
23 .take(10)
24 .collect()
25 }
26
27 pub(crate) fn get(extension: &str) -> String {
28 let name = env!("CARGO_PKG_NAME");
29 let mut file_name = get_random_string();
30 file_name.insert(0, '_');
31 file_name.insert_str(0, name);
32
33 let mut file_path = env::temp_dir();
34
35 if let Some(extra_path) = get_extra_path() {
36 file_path.push(extra_path);
37 };
38
39 file_path.push(file_name);
40 file_path.set_extension(extension);
41
42 FromPath::from_path(&file_path)
43 }