Skip to content

Commit

Permalink
Derive serde traits via FromStr/Display
Browse files Browse the repository at this point in the history
  • Loading branch information
t4ccer committed Aug 31, 2024
1 parent b855f1c commit bd86e96
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
22 changes: 22 additions & 0 deletions cgt/nom_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,28 @@ macro_rules! impl_from_str_via_nom {
}
}
}

#[cfg(feature = "serde")]
impl serde::Serialize for $t {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&self.to_string())
}
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for $t {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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;
1 change: 0 additions & 1 deletion cgt/numeric/dyadic_rational_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion cgt/numeric/rational.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 0 additions & 3 deletions cgt/short/partizan/canonical_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<CanonicalForm>,
Expand Down Expand Up @@ -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,
}
Expand Down
22 changes: 13 additions & 9 deletions cgt_cli/src/commands/snort/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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(())
}
Expand Down

0 comments on commit bd86e96

Please sign in to comment.