1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use super::*;

#[inline]
pub fn remove_double_spaces_and_trim(s: &str) -> String {
    s.trim()
        .chars()
        .dedup_by(|&c1, &c2| c1 == ' ' && c2 == ' ')
        .join("")
}

#[inline]
pub fn simplify_fen(fen: &str) -> String {
    remove_double_spaces_and_trim(fen)
}

pub fn flip_board_fen(fen: &str) -> Result<String> {
    // TODO: ep square not flipped.
    let fen = remove_double_spaces_and_trim(fen);
    let (position_fen, rest_fen) = fen.split_once(' ').ok_or(TimecatError::BadFen {
        fen: fen.to_string(),
    })?;
    Ok(format!(
        "{} {rest_fen}",
        position_fen
            .chars()
            .map(|c| match c {
                c if c.is_uppercase() => c.to_ascii_lowercase(),
                c if c.is_lowercase() => c.to_ascii_uppercase(),
                _ => c,
            })
            .collect::<String>(),
    ))
}

#[cfg(feature = "colored")]
impl<T: ToString> CustomColorize for T {
    fn colorize(&self, style_functions: &[ColoredStringFunction]) -> String {
        let self_string = self.to_string();
        if style_functions.is_empty() || !GLOBAL_TIMECAT_STATE.is_colored_output() {
            return self_string;
        }
        let mut colorized_string = self_string.as_str().into();
        for &func in style_functions {
            colorized_string = func(colorized_string);
        }
        colorized_string.to_string()
    }
}

#[cfg(not(feature = "colored"))]
impl<T: ToString> CustomColorize for T {
    #[inline]
    fn colorize(&self, _: &[fn(String) -> String]) -> String {
        self.to_string()
    }
}

impl StringifyScore for Score {
    fn stringify_score_console(self) -> String {
        if self == INFINITY {
            return "INFINITY".to_string();
        }
        if self == -INFINITY {
            return "-INFINITY".to_string();
        }
        if is_checkmate(self) {
            let mut mate_string = String::from(if self.is_positive() { "M" } else { "-M" });
            let mate_distance = (CHECKMATE_SCORE - self.abs() + 1) / 2;
            mate_string += &mate_distance.to_string();
            return mate_string.colorize(CHECKMATE_SCORE_STYLE);
        }
        let to_return = self as f64 / PAWN_VALUE as f64;
        if to_return % 1.0 == 0.0 {
            format!("{}", to_return as i32)
        } else {
            format!("{:.2}", to_return)
        }
    }

    fn stringify_score_uci(self) -> String {
        if self == INFINITY {
            return "inf".to_string();
        }
        if self == -INFINITY {
            return "-inf".to_string();
        }
        if is_checkmate(self) {
            let mut mate_string = String::from("mate ");
            let mut mate_distance = (CHECKMATE_SCORE - self.abs() + 1) / 2;
            if self.is_negative() {
                mate_distance = -mate_distance;
            }
            mate_string += &mate_distance.to_string();
            return mate_string;
        }
        format!("cp {}", (self as i32 * 100) / PAWN_VALUE as i32)
    }

    #[inline]
    fn stringify_score(self) -> String {
        if GLOBAL_TIMECAT_STATE.is_in_console_mode() {
            self.stringify_score_console()
        } else {
            self.stringify_score_uci()
        }
    }
}

impl Stringify for Score {
    #[inline]
    fn stringify(&self) -> String {
        self.stringify_score()
    }
}

impl Stringify for TimecatError {
    #[inline]
    fn stringify(&self) -> String {
        self.stringify_with_optional_raw_input(None)
    }
}

impl StringifyMove for Move {
    fn uci(self) -> String {
        self.to_string()
    }

    fn algebraic(self, position: &BoardPosition, long: bool) -> Result<String> {
        Ok(self.algebraic_and_new_position(position, long)?.0)
    }

    fn stringify_move(self, position: &BoardPosition) -> Result<String> {
        Some(self).stringify_move(position)
    }
}

impl StringifyMove for Option<Move> {
    fn uci(self) -> String {
        match self {
            Some(m) => m.uci(),
            None => String::from("0000"),
        }
    }

    fn algebraic(self, position: &BoardPosition, long: bool) -> Result<String> {
        match self {
            Some(valid_or_null_move) => valid_or_null_move.algebraic(position, long),
            None => Ok("--".to_string()),
        }
    }

    fn stringify_move(self, position: &BoardPosition) -> Result<String> {
        match GLOBAL_TIMECAT_STATE.is_in_console_mode() {
            true => self.algebraic(position, GLOBAL_TIMECAT_STATE.use_long_algebraic_notation()),
            false => Ok(self.uci()),
        }
    }
}

impl StringifyHash for u64 {
    fn stringify_hash(&self) -> String {
        format!("{:x}", self).to_uppercase()
    }
}

impl Stringify for Duration {
    fn stringify(&self) -> String {
        if GLOBAL_TIMECAT_STATE.is_in_uci_mode() {
            return self.as_millis().to_string();
        }
        if self < &Duration::from_secs(1) {
            return self.as_millis().to_string() + " ms";
        }
        let precision = 3;
        let total_secs = self.as_secs_f64();
        for (threshold, unit) in [(86400.0, "day"), (3600.0, "hr"), (60.0, "min")] {
            if total_secs >= threshold {
                let time_unit = total_secs as u128 / threshold as u128;
                let secs = total_secs % threshold;
                let mut string = format!("{} {}", time_unit, unit);
                if time_unit > 1 {
                    string += "s";
                }
                if secs >= 10.0_f64.powi(-(precision as i32)) {
                    string += " ";
                    string += &Duration::from_secs_f64(secs).stringify();
                }
                return string;
            }
        }
        let total_secs_rounded = total_secs.round();
        let mut string = if (total_secs - total_secs_rounded).abs() < 1e-5 {
            format!("{} sec", total_secs_rounded)
        } else {
            format!("{:.1$} sec", total_secs, precision)
        };
        if total_secs > 1.0 {
            string += "s";
        }
        string
    }
}

macro_rules! implement_stringify {
    ($($type:ty),+ $(,)?) => {
        $(
            impl Stringify for $type {
                fn stringify(&self) -> String {
                    self.to_string()
                }
            }
        )*
    };
}

implement_stringify!(Move, ValidOrNullMove, WeightedMove, Color, PieceType, Piece);

impl<T: Stringify> Stringify for Option<T> {
    fn stringify(&self) -> String {
        match self {
            Some(t) => t.stringify(),
            None => String::from(STRINGIFY_NONE),
        }
    }
}

impl<T: Stringify, E: Error> Stringify for std::result::Result<T, E> {
    fn stringify(&self) -> String {
        match self {
            Ok(t) => format!("Ok({})", t.stringify()),
            Err(e) => format!("Err({})", e),
        }
    }
}

impl<T: Stringify> Stringify for [T] {
    fn stringify(&self) -> String {
        format!("[{}]", self.iter().map(|t| t.stringify()).join(", "))
    }
}

impl<T: Stringify> Stringify for Vec<T> {
    fn stringify(&self) -> String {
        self.as_slice().stringify()
    }
}