rust_voice_chat_room/protocol/src/lib.rs

67 lines
1.8 KiB
Rust
Raw Normal View History

use std::{
collections::{HashMap, VecDeque},
fmt::Display,
fs::File,
io::Read,
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Error {
ConnectionSetup(String),
Connection(String),
Send(String),
Receive(String),
Signal(String),
}
impl std::error::Error for Error {
fn cause(&self) -> Option<&dyn std::error::Error> {
self.source()
}
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::ConnectionSetup(inner) => write!(f, "Connection Setup | {}", inner),
Error::Connection(inner) => write!(f, "Connection | {}", inner),
Error::Send(inner) => write!(f, "Send | {}", inner),
Error::Receive(inner) => write!(f, "Receive | {}", inner),
Error::Signal(inner) => write!(f, "Signal | {}", inner),
}
}
}
#[derive(Debug, Clone)]
struct TOML {
header: String,
fields: HashMap<String, String>,
}
fn naive_toml_parser(file_location: &str) -> TOML {
let mut toml_file = File::open(file_location).unwrap();
let mut toml_ingredients = String::default();
toml_file.read_to_string(&mut toml_ingredients).unwrap();
let mut toml_ingredients = toml_ingredients.lines().collect::<VecDeque<&str>>();
let header = toml_ingredients
.pop_front()
.unwrap()
.replace('[', "")
.replace(']', "")
.trim_end()
.to_string();
let mut fields = HashMap::new();
let _ = toml_ingredients.iter().map(|ingredient| {
ingredient.split_once('=').map(|(key, value)| {
let key = key.replace('"', "").trim().to_string();
let value = value.replace('"', "").trim().to_string();
fields.insert(key, value);
})
});
TOML { header, fields }
}