From bd86e965e6b60856870a10c1bab79c79bf785f85 Mon Sep 17 00:00:00 2001 From: t4ccer Date: Fri, 30 Aug 2024 18:48:43 -0600 Subject: [PATCH] Derive serde traits via `FromStr`/`Display` --- cgt/nom_utils.rs | 22 ++++++++++++++++++++++ cgt/numeric/dyadic_rational_number.rs | 1 - cgt/numeric/rational.rs | 1 - cgt/short/partizan/canonical_form.rs | 3 --- cgt_cli/src/commands/snort/common.rs | 22 +++++++++++++--------- 5 files changed, 35 insertions(+), 14 deletions(-) diff --git a/cgt/nom_utils.rs b/cgt/nom_utils.rs index 2698066..29a347a 100644 --- a/cgt/nom_utils.rs +++ b/cgt/nom_utils.rs @@ -33,6 +33,28 @@ macro_rules! impl_from_str_via_nom { } } } + + #[cfg(feature = "serde")] + impl serde::Serialize for $t { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serializer.serialize_str(&self.to_string()) + } + } + + #[cfg(feature = "serde")] + impl<'de> serde::Deserialize<'de> for $t { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + use std::str::FromStr; + + Ok($t::from_str(&String::deserialize(deserializer)?).unwrap()) + } + } }; } pub(crate) use impl_from_str_via_nom; diff --git a/cgt/numeric/dyadic_rational_number.rs b/cgt/numeric/dyadic_rational_number.rs index bb413d7..2b0c8d0 100644 --- a/cgt/numeric/dyadic_rational_number.rs +++ b/cgt/numeric/dyadic_rational_number.rs @@ -12,7 +12,6 @@ use std::{ /// Number in form `n/2^m` #[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct DyadicRationalNumber { numerator: i64, denominator_exponent: u32, diff --git a/cgt/numeric/rational.rs b/cgt/numeric/rational.rs index 3f8954f..ef51357 100644 --- a/cgt/numeric/rational.rs +++ b/cgt/numeric/rational.rs @@ -13,7 +13,6 @@ use std::str::FromStr; /// Infinite rational number. #[derive(Debug, Hash, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum Rational { /// Negative infnity, smaller than all other values NegativeInfinity, diff --git a/cgt/short/partizan/canonical_form.rs b/cgt/short/partizan/canonical_form.rs index c11db1f..7d4998d 100644 --- a/cgt/short/partizan/canonical_form.rs +++ b/cgt/short/partizan/canonical_form.rs @@ -25,7 +25,6 @@ use std::{ /// A number-up-star game position that is a sum of a number, up and, nimber. #[derive(Debug, Hash, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Nus { number: DyadicRationalNumber, up_multiple: i32, @@ -321,7 +320,6 @@ impl Display for Nus { /// Left and Right moves from a given position #[derive(Debug, Hash, Clone, PartialEq, Eq)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Moves { /// Left player's moves pub left: Vec, @@ -820,7 +818,6 @@ enum CanonicalFormInner { /// Canonical game form #[repr(transparent)] #[derive(Debug, Hash, Clone, PartialEq, Eq)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct CanonicalForm { inner: CanonicalFormInner, } diff --git a/cgt_cli/src/commands/snort/common.rs b/cgt_cli/src/commands/snort/common.rs index 28f1a1a..0c60a9d 100644 --- a/cgt_cli/src/commands/snort/common.rs +++ b/cgt_cli/src/commands/snort/common.rs @@ -4,7 +4,7 @@ use cgt::{ graph::undirected::Graph, numeric::{dyadic_rational_number::DyadicRationalNumber, rational::Rational}, short::partizan::{ - games::snort::Snort, partizan_game::PartizanGame, + canonical_form::CanonicalForm, games::snort::Snort, partizan_game::PartizanGame, transposition_table::ParallelTranspositionTable, }, }; @@ -30,6 +30,14 @@ pub enum Log { }, } +#[derive(serde::Serialize, serde::Deserialize)] +pub struct EvaluationResult { + pub position: Snort, + pub canonical_form: CanonicalForm, + pub temperature: DyadicRationalNumber, + pub degree: usize, +} + #[derive(Debug, Clone)] pub struct Edge { pub from: u32, @@ -122,7 +130,6 @@ pub fn analyze_position(position: Snort, with_graphviz: bool) -> Result<()> { let degree = position.degree(); let second_degree = position.second_degree(); - let score = temperature.to_rational() - Rational::from(degree as i32); eprintln!("Canonical Form: {}", canonical_form); eprintln!("Temperature: {}", temperature); @@ -134,17 +141,14 @@ pub fn analyze_position(position: Snort, with_graphviz: bool) -> Result<()> { Rational::from(degree as i64) + (Rational::from(second_degree as i64) / Rational::from(2)) ); - let log = Log::HighFitness { - position: Scored { - object: position, - score, - }, - canonical_form: canonical_form.to_string(), + let result = EvaluationResult { + position, + canonical_form, temperature, degree, }; - println!("{}", serde_json::ser::to_string(&log).unwrap()); + println!("{}", serde_json::ser::to_string(&result).unwrap()); Ok(()) }