use serde::{Deserialize, Serialize};
#[derive(Clone, Serialize, Deserialize, Debug, Default, PartialEq)]
pub struct Constant {
pub name: &'static str,
pub value: String,
}
#[derive(Clone, Serialize, Debug)]
pub struct Constants {
pub constants: Vec<Constant>,
}
impl Constants {
pub fn constant(&self, name: &str) -> Option<Constant> {
self.constants
.iter()
.find(|constant| constant.name == name)
.cloned()
}
pub fn constants(&self) -> &Vec<Constant> {
&self.constants
}
pub fn new() -> Self {
let constants = vec![
Constant {
name: "EULER",
value: EULER.to_string(),
},
Constant {
name: "GAMMA",
value: GAMMA.to_string(),
},
Constant {
name: "HASH_ALGORITHM",
value: HASH_ALGORITHM.to_string(),
},
Constant {
name: "HASH_COST",
value: HASH_COST.to_string(),
},
Constant {
name: "HASH_LENGTH",
value: HASH_LENGTH.to_string(),
},
Constant {
name: "PHI",
value: PHI.to_string(),
},
Constant {
name: "PI",
value: PI.to_string(),
},
Constant {
name: "PLANCK",
value: PLANCK.to_string(),
},
Constant {
name: "SILVER_RATIO",
value: SILVER_RATIO.to_string(),
},
Constant {
name: "SPECIAL_CHARS",
value: SPECIAL_CHARS.iter().collect::<String>(),
},
Constant {
name: "SQRT2",
value: SQRT2.to_string(),
},
Constant {
name: "SQRT3",
value: SQRT3.to_string(),
},
Constant {
name: "SQRT5",
value: SQRT5.to_string(),
},
];
Self { constants }
}
pub fn is_valid(&self) -> bool {
self.constants()
.iter()
.all(|constant| !constant.name.is_empty() && !constant.value.is_empty())
}
}
impl Default for Constants {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, Serialize)]
pub enum ConstantValue {
Float(f64),
String(String),
U32(u32),
Usize(usize),
CharArray(&'static [char]),
}
pub const EULER: f64 = std::f64::consts::E;
pub const GAMMA: f64 = 0.577_215_664_901_532_9_f64;
pub const HASH_ALGORITHM: &str = "Blake3";
pub const HASH_COST: u32 = 8;
pub const HASH_LENGTH: usize = 32;
pub const PHI: f64 = (1.0 + SQRT5) / 2.0;
pub const PI: f64 = std::f64::consts::PI;
pub const PLANCK: f64 = 6.626_070_15e-34_f64;
pub const SILVER_RATIO: f64 = 1.0 + SQRT2;
pub const SPECIAL_CHARS: &[char] = &[
'!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '=', '[', ']', '{', '}', '|', ';',
':', '"', '<', '>', ',', '.', '?', '/', '~', '`',
];
pub const SQRT2: f64 = std::f64::consts::SQRT_2;
pub const SQRT3: f64 = 1.732_050_807_568_877_2_f64;
pub const SQRT5: f64 = 2.236_067_977_499_79_f64;