strings not used internally11
This commit is contained in:
@@ -8,6 +8,49 @@
|
||||
//! would reject, and each side renders the shared error as its own error type.
|
||||
|
||||
pub use rusty_money::iso;
|
||||
pub use crate::proto::komp_ac::table_definition::MoneyRounding;
|
||||
|
||||
impl MoneyRounding {
|
||||
pub fn as_storage_str(self) -> &'static str {
|
||||
match self {
|
||||
Self::None => "none",
|
||||
Self::HalfUp => "half_up",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::str::FromStr for MoneyRounding {
|
||||
type Err = serde::de::value::Error;
|
||||
|
||||
fn from_str(value: &str) -> Result<Self, Self::Err> {
|
||||
match value {
|
||||
"none" => Ok(Self::None),
|
||||
"half_up" => Ok(Self::HalfUp),
|
||||
_ => Err(serde::de::Error::custom(format!("Invalid money rounding: {value}"))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<String> for MoneyRounding {
|
||||
type Error = serde::de::value::Error;
|
||||
|
||||
fn try_from(value: String) -> Result<Self, Self::Error> {
|
||||
value.parse()
|
||||
}
|
||||
}
|
||||
|
||||
pub mod rounding_serde {
|
||||
use super::MoneyRounding;
|
||||
use serde::{Deserialize, Deserializer, Serializer};
|
||||
|
||||
pub fn serialize<S: Serializer>(value: &MoneyRounding, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
serializer.serialize_str(value.as_storage_str())
|
||||
}
|
||||
|
||||
pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<MoneyRounding, D::Error> {
|
||||
String::deserialize(deserializer)?.parse().map_err(serde::de::Error::custom)
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolves a canonical uppercase ISO-4217 alphabetic code to its currency.
|
||||
///
|
||||
@@ -40,6 +83,34 @@ pub fn iso_currency_codes() -> Vec<&'static str> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn rounding_storage_and_json_boundaries_are_strict() {
|
||||
#[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)]
|
||||
struct StoredPolicy {
|
||||
#[serde(with = "super::rounding_serde")]
|
||||
rounding: MoneyRounding,
|
||||
}
|
||||
|
||||
for (text, policy) in [("none", MoneyRounding::None), ("half_up", MoneyRounding::HalfUp)] {
|
||||
assert_eq!(text.parse::<MoneyRounding>().unwrap(), policy);
|
||||
assert_eq!(MoneyRounding::try_from(text.to_string()).unwrap(), policy);
|
||||
assert_eq!(policy.as_storage_str(), text);
|
||||
let stored = StoredPolicy { rounding: policy };
|
||||
let json = serde_json::json!({ "rounding": text });
|
||||
assert_eq!(serde_json::to_value(&stored).unwrap(), json);
|
||||
assert_eq!(serde_json::from_value::<StoredPolicy>(json).unwrap(), stored);
|
||||
}
|
||||
for text in ["", "NONE", "HalfUp", "half-up", " half_up", "half_up ", "unknown"] {
|
||||
assert!(text.parse::<MoneyRounding>().is_err());
|
||||
assert!(serde_json::from_value::<StoredPolicy>(serde_json::json!({ "rounding": text })).is_err());
|
||||
}
|
||||
for value in [serde_json::Value::Null, serde_json::json!(0), serde_json::json!(1)] {
|
||||
assert!(serde_json::from_value::<StoredPolicy>(serde_json::json!({ "rounding": value })).is_err());
|
||||
}
|
||||
assert!(MoneyRounding::try_from(-1).is_err());
|
||||
assert!(MoneyRounding::try_from(2).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn canonical_codes_resolve_to_their_currency() {
|
||||
assert_eq!(require_iso_currency_code("EUR").unwrap(), iso::EUR);
|
||||
|
||||
Reference in New Issue
Block a user