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:
TuDatTr
2022-12-02 08:37:33 +01:00
parent be832438c5
commit 8cd8032741
14 changed files with 872 additions and 31 deletions

22
2022/common/src/lib.rs Normal file
View 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())
}