Files
ReactionSystems/src/rsprocess/translator.rs

153 lines
3.3 KiB
Rust
Raw Normal View History

//! Module for translation and keeping track of strings.
2025-07-10 15:02:14 +02:00
use std::collections::HashMap;
2025-07-09 21:44:04 +02:00
use serde::{Serialize, Deserialize};
/// precision for printing frequencies
pub static PRECISION: &usize = &2;
2025-06-16 14:46:04 +02:00
pub type IdType = u32;
2025-07-10 15:02:14 +02:00
/// Structure that keeps track of association string and id. Ids given
/// sequentially from 0.
2025-07-09 21:44:04 +02:00
#[derive(Clone, Debug, Serialize, Deserialize)]
2025-06-16 14:46:04 +02:00
pub struct Translator {
strings: HashMap<String, IdType>,
2025-06-18 11:28:04 +02:00
reverse: HashMap<IdType, String>,
last_id: IdType,
2025-06-16 14:46:04 +02:00
}
impl Translator {
pub fn new() -> Self {
2025-06-18 11:28:04 +02:00
Translator {
2025-07-13 17:28:13 +02:00
strings: HashMap::from([("*".into(), 0)]),
reverse: HashMap::from([(0, "*".into())]),
last_id: 1,
2025-06-18 11:28:04 +02:00
}
2025-06-16 14:46:04 +02:00
}
/// converts a string into an id
2025-06-18 11:28:04 +02:00
pub fn encode(&mut self, s: impl Into<String>) -> IdType {
let s = s.into();
let id = *(self.strings.entry(s.clone()).or_insert({
2025-06-18 11:28:04 +02:00
self.last_id += 1;
self.last_id
}));
self.reverse.insert(id, s.clone());
id
2025-06-18 11:28:04 +02:00
}
2025-06-16 14:46:04 +02:00
2025-07-12 02:42:28 +02:00
pub fn encode_not_mut(&self, s: impl Into<String>) -> Option<IdType> {
self.strings.get(&s.into()).copied()
}
/// converts an id into the corresponding string
2025-07-02 07:30:05 +02:00
pub fn decode(&self, el: IdType) -> Option<String> {
self.reverse
.get(&el)
.map(|x| x.to_string())
2025-06-18 11:28:04 +02:00
}
2025-06-16 14:46:04 +02:00
}
impl Default for Translator {
fn default() -> Self {
Translator::new()
}
}
impl PartialEq for Translator {
fn eq(&self, other: &Self) -> bool {
for (s, id) in self.strings.iter() {
match other.strings.get(s) {
None => return false,
Some(id2) if id != id2 => return false,
_ => {}
}
}
true
}
}
// -----------------------------------------------------------------------------
// print structures
2025-06-18 11:28:04 +02:00
// -----------------------------------------------------------------------------
pub trait PrintableWithTranslator {
fn print(&self, f: &mut fmt::Formatter, translator: &Translator)
-> fmt::Result;
}
pub struct Formatter<'a, T> {
data: &'a T,
translator: &'a Translator,
}
impl<'a, T> fmt::Display for Formatter<'a, T>
where
T: PrintableWithTranslator
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.data.print(f, self.translator)
}
}
impl<'a, T> Formatter<'a, T> {
pub fn from(
translator: &'a Translator,
data: &'a T
) -> Self {
Self { data, translator }
}
}
2025-07-01 19:22:50 +02:00
use super::{
structure::{
RSassert
2025-07-01 19:22:50 +02:00
},
};
2025-06-18 11:28:04 +02:00
use std::fmt;
macro_rules! translator_structure {
($name:ident, $type:ty, $dataname:ident, $print_func:ident) => {
#[derive(Clone, Debug)]
#[allow(dead_code)]
pub struct $name<'a> {
translator: &'a Translator,
$dataname: &'a $type,
}
#[allow(dead_code)]
impl <'a>$name<'a> {
pub fn from(translator: &'a Translator, $dataname: &'a $type) -> Self {
$name { translator, $dataname }
}
}
impl<'a> fmt::Display for $name<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
$print_func(f, self.translator, self.$dataname)
}
}
2025-06-18 11:28:04 +02:00
};
}
// RSassert
2025-06-18 11:28:04 +02:00
#[allow(unused_variables)]
fn print_assert(
f: &mut fmt::Formatter,
translator: &Translator,
assert: &RSassert
) -> fmt::Result {
2025-06-18 11:28:04 +02:00
todo!()
}
translator_structure!(RSassertDisplay, RSassert, assert, print_assert);