Skip to content

Commit

Permalink
refactor(core): change serde code
Browse files Browse the repository at this point in the history
  • Loading branch information
SARDONYX-sard committed Feb 16, 2024
1 parent dc65c87 commit d323347
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 345 deletions.
64 changes: 42 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dar2oar_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ compact_str = { version = "0.7.1", features = ["serde"] }
jwalk = "0.8.1" # To parallel traverse dir recursivly
nom = { version = "7.1.3", features = ["alloc"] } # Syntax
serde = { version = "1.0", features = ["derive"] } # Implement (De)Serializer
serde-untagged = "0.1"
serde_json = "1.0" # Json converter
thiserror = "1.0.48" # define errors type
tokio = { version = "1.33.0", features = [
Expand Down
6 changes: 3 additions & 3 deletions dar2oar_core/src/condition_parser/dar_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,13 @@ impl TryFrom<&FnArg<'_>> for Direction {
fn try_from(value: &FnArg<'_>) -> Result<Self, Self::Error> {
match value {
FnArg::Number(num) => Ok(match *num {
NumberLiteral::Hex(num) => (num as u64)
NumberLiteral::Hex(num) => (num as f64)
.try_into()
.map_err(|e: &str| ParseError::UnexpectedValue(e.into(), "0..=4".into()))?,
NumberLiteral::Decimal(num) => (num as u64)
NumberLiteral::Decimal(num) => (num as f64)
.try_into()
.map_err(|e: &str| ParseError::UnexpectedValue(e.into(), "0..=4".into()))?,
NumberLiteral::Float(num) => (num as u64)
NumberLiteral::Float(num) => (num as f64)
.try_into()
.map_err(|e: &str| ParseError::UnexpectedValue(e.into(), "0..=4".into()))?,
}),
Expand Down
71 changes: 2 additions & 69 deletions dar2oar_core/src/values/actor_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ pub struct ActorValue {
/// - Actor Value Percentage (0-1) => "Percentage"
///
/// default: `ActorValue`
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum ActorValueType {
/// Value
#[default]
#[serde(rename = "Value")]
ActorValue,
/// Base
Base,
Expand All @@ -38,80 +39,12 @@ pub enum ActorValueType {
Percentage,
}

impl TryFrom<&str> for ActorValueType {
type Error = &'static str;

fn try_from(value: &str) -> Result<Self, Self::Error> {
Ok(match value {
"Value" => Self::ActorValue,
"Base" => Self::Base,
"Max" => Self::Max,
"Percentage" => Self::Percentage,
_ => return Err("Invalid actor value type"),
})
}
}

impl From<ActorValueType> for &str {
fn from(value: ActorValueType) -> Self {
match value {
ActorValueType::ActorValue => "Value",
ActorValueType::Base => "Base",
ActorValueType::Max => "Max",
ActorValueType::Percentage => "Percentage",
}
}
}

impl Serialize for ActorValueType {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(self.clone().into())
}
}

impl<'de> Deserialize<'de> for ActorValueType {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let type_str = String::deserialize(deserializer)?;
type_str
.as_str()
.try_into()
.map_err(serde::de::Error::custom)
}
}

#[cfg(test)]
mod tests {
use super::*;
use anyhow::Result;
use pretty_assertions::assert_eq;

#[test]
fn test_try_from_str_valid() {
let value = "Value";
let result = ActorValueType::try_from(value);
assert_eq!(result, Ok(ActorValueType::ActorValue));
}

#[test]
fn test_try_from_str_invalid() {
let value = "InvalidValue";
let result = ActorValueType::try_from(value);
assert_eq!(result, Err("Invalid actor value type"));
}

#[test]
fn test_into_str() {
let value = ActorValueType::Base;
let result: &str = value.into();
assert_eq!(result, "Base");
}

#[test]
fn test_serialize() -> Result<()> {
let value = ActorValueType::Max;
Expand Down
Loading

0 comments on commit d323347

Please sign in to comment.