use serde::{Deserialize, Serialize};
#[non_exhaustive]
#[derive(
Clone,
Debug,
Eq,
Hash,
Ord,
PartialEq,
PartialOrd,
Serialize,
Deserialize,
)]
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 get_value(&self, name: &str) -> Option<ConstantValue> {
if let Some(constant) = self.constant(name) {
match name {
"HASH_COST" => {
constant.value.parse().map(ConstantValue::U32).ok()
}
"HASH_LENGTH" => constant
.value
.parse()
.map(ConstantValue::Usize)
.ok(),
_ => {
if let Ok(float_value) =
constant.value.parse::<f64>()
{
Some(ConstantValue::Float(float_value))
} else if let Some(char_array) =
Self::get_char_array(name)
{
Some(ConstantValue::CharArray(char_array))
} else {
Some(ConstantValue::String(
constant.value.clone(),
))
}
}
}
} else {
None
}
}
fn get_char_array(name: &str) -> Option<&'static [char]> {
match name {
"SPECIAL_CHARS" => Some(SPECIAL_CHARS),
_ => None,
}
}
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: "APERY",
value: APERY.to_string(),
},
Constant {
name: "AVOGADRO",
value: AVOGADRO.to_string(),
},
Constant {
name: "BOLTZMANN",
value: BOLTZMANN.to_string(),
},
Constant {
name: "CATALAN",
value: CATALAN.to_string(),
},
Constant {
name: "COULOMB",
value: COULOMB.to_string(),
},
Constant {
name: "EULER",
value: EULER.to_string(),
},
Constant {
name: "FARADAY",
value: FARADAY.to_string(),
},
Constant {
name: "GAMMA",
value: GAMMA.to_string(),
},
Constant {
name: "GAS_CONSTANT",
value: GAS_CONSTANT.to_string(),
},
Constant {
name: "GLAISHER_KINKELIN",
value: GLAISHER_KINKELIN.to_string(),
},
Constant {
name: "GRAVITATIONAL_CONSTANT",
value: GRAVITATIONAL_CONSTANT.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: "KHINCHIN",
value: KHINCHIN.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: "PLANCK_REDUCED",
value: PLANCK_REDUCED.to_string(),
},
Constant {
name: "SILVER_RATIO",
value: SILVER_RATIO.to_string(),
},
Constant {
name: "SPEED_OF_LIGHT",
value: SPEED_OF_LIGHT.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(),
},
Constant {
name: "TAU",
value: TAU.to_string(),
},
Constant {
name: "VACUUM_PERMEABILITY",
value: VACUUM_PERMEABILITY.to_string(),
},
Constant {
name: "VACUUM_PERMITTIVITY",
value: VACUUM_PERMITTIVITY.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 APERY: f64 = 1.2020569031595942;
pub const AVOGADRO: f64 = 6.02214076e23;
pub const BOLTZMANN: f64 = 1.380648e-23;
pub const CATALAN: f64 = 0.915_965_594_177_219;
pub const COULOMB: f64 = 8.9875517923e9;
pub const EULER: f64 = std::f64::consts::E;
pub const FARADAY: f64 = 96485.33212;
pub const GAMMA: f64 = 0.5772156649015329;
pub const GAS_CONSTANT: f64 = 8.314462618;
pub const GLAISHER_KINKELIN: f64 = 1.2824271291006226;
pub const GRAVITATIONAL_CONSTANT: f64 = 6.67430e-11;
pub const HASH_ALGORITHM: &str = "Blake3";
pub const HASH_COST: u32 = 8;
pub const HASH_LENGTH: usize = 32;
pub const KHINCHIN: f64 = 2.6854520010653064;
pub const PHI: f64 = (1.0 + SQRT5) / 2.0;
pub const PI: f64 = std::f64::consts::PI;
pub const PLANCK: f64 = 6.62607015e-34;
pub const PLANCK_REDUCED: f64 = PLANCK / (2.0 * PI);
pub const SILVER_RATIO: f64 = 1.0 + SQRT2;
pub const SPEED_OF_LIGHT: f64 = 299792458.0;
pub const SPECIAL_CHARS: &[char] = &[
'!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '=',
'[', ']', '{', '}', '|', ';', ':', '"', '<', '>', ',', '.', '?',
'/', '~', '`',
];
pub const SQRT2: f64 = std::f64::consts::SQRT_2;
pub const SQRT3: f64 = 1.7320508075688772;
pub const SQRT5: f64 = 2.236_067_977_499_79;
pub const TAU: f64 = std::f64::consts::TAU;
pub const VACUUM_PERMEABILITY: f64 = 1.25663706212e-6;
pub const VACUUM_PERMITTIVITY: f64 = 8.8541878128e-12;