2025-07-09 19:34:15 +02:00
|
|
|
//! Module for translation and keeping track of strings.
|
2025-07-10 15:02:14 +02:00
|
|
|
|
2025-08-24 02:01:24 +02:00
|
|
|
use std::collections::HashMap;
|
2025-07-09 21:44:04 +02:00
|
|
|
use serde::{Serialize, Deserialize};
|
2025-07-03 11:09:58 +02:00
|
|
|
|
2025-07-09 19:34:15 +02:00
|
|
|
/// precision for printing frequencies
|
2025-08-24 02:01:24 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2025-07-09 19:34:15 +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 {
|
2025-06-19 23:48:16 +02:00
|
|
|
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
|
|
|
|
|
}));
|
2025-06-19 23:48:16 +02:00
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-09 19:34:15 +02:00
|
|
|
/// converts an id into the corresponding string
|
2025-07-02 07:30:05 +02:00
|
|
|
pub fn decode(&self, el: IdType) -> Option<String> {
|
2025-06-19 23:48:16 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2025-08-24 02:01:24 +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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-07-02 10:37:29 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
// print structures
|
2025-06-18 11:28:04 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
2025-08-24 02:01:24 +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::{
|
2025-08-24 02:01:24 +02:00
|
|
|
RSassert
|
2025-07-01 19:22:50 +02:00
|
|
|
},
|
|
|
|
|
};
|
2025-06-18 11:28:04 +02:00
|
|
|
use std::fmt;
|
|
|
|
|
|
2025-07-02 10:37:29 +02:00
|
|
|
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,
|
2025-06-19 23:48:16 +02:00
|
|
|
}
|
2025-07-02 10:37:29 +02:00
|
|
|
|
|
|
|
|
#[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
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-07-02 10:37:29 +02:00
|
|
|
// RSassert
|
|
|
|
|
|
2025-06-18 11:28:04 +02:00
|
|
|
#[allow(unused_variables)]
|
2025-06-19 23:48:16 +02:00
|
|
|
fn print_assert(
|
|
|
|
|
f: &mut fmt::Formatter,
|
|
|
|
|
translator: &Translator,
|
|
|
|
|
assert: &RSassert
|
|
|
|
|
) -> fmt::Result {
|
2025-06-18 11:28:04 +02:00
|
|
|
todo!()
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-02 10:37:29 +02:00
|
|
|
translator_structure!(RSassertDisplay, RSassert, assert, print_assert);
|