cubing_core/kpuzzle/
definition.rs

1use serde::{Deserialize, Serialize};
2
3use std::fmt::Debug;
4pub(crate) use std::{collections::HashMap, fmt::Display};
5
6use crate::alg::{Alg, Move};
7
8#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
9pub struct KPuzzleOrbitName(pub String);
10
11impl From<&str> for KPuzzleOrbitName {
12    fn from(value: &str) -> Self {
13        KPuzzleOrbitName(value.to_owned())
14    }
15}
16
17impl Display for KPuzzleOrbitName {
18    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19        write!(f, "{}", self.0)
20    }
21}
22
23// use super::super::{pattern::KPatternData, transformation::KTransformationData};
24#[derive(Debug, Serialize, Deserialize)]
25#[serde(rename_all = "camelCase")]
26pub struct KPuzzleOrbitDefinition {
27    pub orbit_name: KPuzzleOrbitName,
28    pub num_pieces: u8,       // TODO
29    pub num_orientations: u8, // TODO
30}
31
32#[derive(Debug, Serialize, Deserialize)]
33#[serde(rename_all = "camelCase")]
34pub struct KPuzzleDefinition {
35    pub name: String,
36    pub orbits: Vec<KPuzzleOrbitDefinition>,
37    pub default_pattern: KPatternData,
38    pub moves: HashMap<Move, KTransformationData>,
39    #[serde(skip_serializing_if = "Option::is_none")]
40    pub derived_moves: Option<HashMap<Move, Alg>>,
41}
42
43#[derive(
44    PartialEq,
45    Serialize,
46    Deserialize,
47    Clone, // TODO
48)]
49#[serde(rename_all = "camelCase")]
50pub struct KPatternOrbitData {
51    pub pieces: Vec<u8>,
52    pub orientation: Vec<u8>,
53    pub orientation_mod: Option<Vec<u8>>,
54}
55
56struct SameLineDebugVecU8<'a>(&'a Vec<u8>);
57
58impl Debug for SameLineDebugVecU8<'_> {
59    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60        write!(
61            f,
62            "[{}]",
63            self.0
64                .iter()
65                .map(|n| n.to_string())
66                .collect::<Vec<String>>()
67                .join(", ")
68        )
69    }
70}
71
72impl Debug for KPatternOrbitData {
73    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
74        f.debug_struct("KPatternOrbitData")
75            .field("pieces", &SameLineDebugVecU8(&self.pieces))
76            .field("orientation", &SameLineDebugVecU8(&self.orientation))
77            .field(
78                "orientation_mod",
79                &self.orientation_mod.as_ref().map(SameLineDebugVecU8),
80            )
81            .finish()
82    }
83}
84
85pub type KPatternData = HashMap<KPuzzleOrbitName, KPatternOrbitData>;
86
87#[derive(Clone, PartialEq, Serialize, Deserialize)]
88#[serde(rename_all = "camelCase")]
89pub struct KTransformationOrbitData {
90    pub permutation: Vec<u8>,
91    pub orientation_delta: Vec<u8>,
92}
93
94impl Debug for KTransformationOrbitData {
95    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
96        f.debug_struct("KTransformationOrbitData")
97            .field("permutation", &SameLineDebugVecU8(&self.permutation))
98            .field(
99                "orientation_delta",
100                &SameLineDebugVecU8(&self.orientation_delta),
101            )
102            .finish()
103    }
104}
105
106// TODO: Use `Move` as the key?
107pub type KTransformationData = HashMap<KPuzzleOrbitName, KTransformationOrbitData>;