Refactor to use common lib
Added solution for AoC 2022/2 Signed-off-by: TuDatTr <tuan-dat.tran@tudattr.dev>
This commit is contained in:
22
2022/common/src/lib.rs
Normal file
22
2022/common/src/lib.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Write};
|
||||
use std::path::Path;
|
||||
use tracing::{debug, info};
|
||||
|
||||
pub fn create_file(path: &Path, content: String) {
|
||||
info!("Creating file");
|
||||
debug!("Writing file from: {:?}\nWith:\n{}", path, content);
|
||||
|
||||
let mut file = File::create(path).unwrap();
|
||||
let _ = file.write_all(content.as_bytes());
|
||||
}
|
||||
|
||||
pub fn read_file(path: &Path) -> std::io::Result<String> {
|
||||
info!("Reading file");
|
||||
debug!("Reading file from: {:?}", path);
|
||||
|
||||
let mut file = File::open(path)?;
|
||||
let mut contents = String::new();
|
||||
let _ = file.read_to_string(&mut contents)?;
|
||||
Ok(contents.trim().to_string())
|
||||
}
|
||||
Reference in New Issue
Block a user