git: reset

This commit is contained in:
k
2024-06-23 17:38:53 +02:00
commit a1a576d088
375 changed files with 40952 additions and 0 deletions

43
parser/src/io/binary.rs Normal file
View File

@@ -0,0 +1,43 @@
use std::{
fmt::Debug,
fs::File,
io::{BufReader, BufWriter},
};
use bincode::{config, decode_from_std_read, encode_into_std_write, Decode, Encode};
pub struct Binary;
impl Binary {
pub fn import<T>(path: &str) -> color_eyre::Result<T>
where
T: Decode,
{
let config = config::standard();
let file = File::open(path)?;
let mut reader = BufReader::new(file);
let decoded = decode_from_std_read(&mut reader, config)?;
Ok(decoded)
}
pub fn export<T>(path: &str, value: &T) -> color_eyre::Result<()>
where
T: Debug + Encode,
{
let config = config::standard();
let file = File::create(path).inspect_err(|_| {
dbg!(path, value);
})?;
let mut writer = BufWriter::new(file);
encode_into_std_write(value, &mut writer, config)?;
Ok(())
}
}

2
parser/src/io/consts.rs Normal file
View File

@@ -0,0 +1,2 @@
pub const IMPORTS_FOLDER_PATH: &str = "./imports";
pub const OUTPUTS_FOLDER_PATH: &str = "./target/outputs";

37
parser/src/io/json.rs Normal file
View File

@@ -0,0 +1,37 @@
use std::{
fs::File,
io::{BufReader, BufWriter},
};
use serde::{de::DeserializeOwned, Serialize};
pub struct Json;
impl Json {
pub fn import<T>(path: &str) -> color_eyre::Result<T>
where
T: DeserializeOwned,
{
let file = File::open(path)?;
let reader = BufReader::new(file);
Ok(serde_json::from_reader(reader)?)
}
pub fn export<T>(path: &str, value: &T) -> color_eyre::Result<()>
where
T: Serialize,
{
let file = File::create(path).unwrap_or_else(|_| {
dbg!(&path);
panic!("No such file or directory")
});
let mut writer = BufWriter::new(file);
serde_json::to_writer_pretty(&mut writer, value)?;
Ok(())
}
}

11
parser/src/io/mod.rs Normal file
View File

@@ -0,0 +1,11 @@
mod binary;
mod consts;
mod json;
mod path;
mod serialization;
pub use binary::*;
pub use consts::*;
pub use json::*;
pub use path::*;
pub use serialization::*;

3
parser/src/io/path.rs Normal file
View File

@@ -0,0 +1,3 @@
pub fn format_path(path: &str) -> String {
path.replace(['-', '_', ' '], "/")
}

View File

@@ -0,0 +1,55 @@
use std::fmt::Debug;
use allocative::Allocative;
use bincode::{Decode, Encode};
use serde::{de::DeserializeOwned, Serialize};
use crate::io::{Binary, Json};
#[derive(PartialEq, PartialOrd, Ord, Eq, Debug, Clone, Copy, Default, Allocative)]
pub enum Serialization {
#[default]
Binary,
Json,
}
impl Serialization {
pub fn to_extension(&self) -> &str {
match self {
Self::Binary => "bin",
Self::Json => "json",
}
}
pub fn from_extension(extension: &str) -> Self {
match extension {
"bin" => Self::Binary,
"json" => Self::Json,
_ => panic!("Extension \"{extension}\" isn't supported"),
}
}
pub fn append_extension(&self, path: &str) -> String {
format!("{path}.{}", self.to_extension())
}
pub fn import<T>(&self, path: &str) -> color_eyre::Result<T>
where
T: Debug + DeserializeOwned + Decode,
{
match self {
Serialization::Binary => Binary::import(path),
Serialization::Json => Json::import(path),
}
}
pub fn export<T>(&self, path: &str, value: &T) -> color_eyre::Result<()>
where
T: Debug + Serialize + Encode,
{
match self {
Serialization::Binary => Binary::export(path, value),
Serialization::Json => Json::export(path, value),
}
}
}