rustviz_lib/svg_frontend/
utils.rs1use std::io::{self, prelude::*};
2use std::fs::File;
3use std::path::Path;
4
5pub fn read_file<P>(file_path: P) -> io::Result<io::BufReader<File>>
9where
10 P: AsRef<Path>,
11{
12 let file = File::open(file_path)?;
13 Ok(io::BufReader::new(file))
14}
15
16pub fn read_file_to_string<P>(file_path: P) -> io::Result<String>
17where
18 P: AsRef<Path>,
19{
20 let mut string = String::new();
21 if let Ok(mut buf) = read_file(file_path) {
22 buf.read_to_string(&mut string)?;
23 }
24 Ok(string.to_owned())
25}
26
27pub fn read_lines<P>(file_path: P) -> io::Result<io::Lines<io::BufReader<File>>>
29where
30 P: AsRef<Path>,
31{
32 let file = File::open(file_path)?;
33 Ok(io::BufReader::new(file).lines())
34}
35
36pub fn create_and_write_to_file<P>(content: &String, file_path: P)
42where
43 P: AsRef<Path>,
44{
45 let display = file_path.as_ref().display();
46
47 let mut file = match File::create(file_path.as_ref()) {
49 Err(why) => panic!("couldn't create {}: {}", display, why),
50 Ok(file) => file,
51 };
52
53 match file.write_all(content.as_bytes()) {
55 Err(why) => panic!("couldn't write to {}: {}", display, why),
56 Ok(_) => println!("successfully wrote to {}", display),
57 }
58}