1 | use std::io::BufRead; |
2 | |
3 | #[track_caller ] |
4 | pub fn read_file_lines(path: &str) -> Vec<String> { |
5 | let Ok(file: File) = std::fs::File::open(path) else { |
6 | panic!("failed to open file ` {path}`" ) |
7 | }; |
8 | |
9 | let file: BufReader = std::io::BufReader::new(inner:file); |
10 | let mut lines: Vec = vec![]; |
11 | |
12 | for line: Result in file.lines() { |
13 | let Ok(line: String) = line else { |
14 | panic!("failed to read file lines ` {path}`" ); |
15 | }; |
16 | |
17 | lines.push(line); |
18 | } |
19 | |
20 | lines |
21 | } |
22 | |
23 | #[track_caller ] |
24 | pub fn write_to_file<C: AsRef<[u8]>>(path: &str, contents: C) { |
25 | if let Some(parent: &Path) = std::path::Path::new(path).parent() { |
26 | if std::fs::create_dir_all(path:parent).is_err() { |
27 | panic!("failed to create directory ` {path}`" ); |
28 | } |
29 | } |
30 | |
31 | if std::fs::write(path, contents).is_err() { |
32 | panic!("failed to write file ` {path}`" ); |
33 | } |
34 | } |
35 | |