ruci/engine/
copy_protection.rs

1use core::fmt::{Display, Formatter};
2use crate::errors::MessageParseError;
3use crate::dev_macros::{from_str_parts, impl_message, message_from_impl};
4use crate::MessageParseErrorKind;
5use super::{pointers, traits};
6
7#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9/// Engine's copy protection. GUIs should respect this.
10///
11/// Sent after [`UciOk`](super::UciOk).
12///
13/// <https://backscattering.de/chess/uci/#engine-copyprotection>
14pub enum CopyProtection {
15    Ok,
16    Error,
17}
18
19impl_message!(CopyProtection);
20message_from_impl!(engine CopyProtection);
21from_str_parts!(impl CopyProtection for parts -> Result {
22    for part in parts {
23        // TODO: Should it be lowercased (case insensitive)?
24        match part.trim() {
25            "ok" => return Ok(Self::Ok),
26            "error" => return Ok(Self::Error),
27            _ => ()
28        }
29    }
30
31    Err(MessageParseError {
32        expected: "ok or error",
33        kind: MessageParseErrorKind::ValueParseError
34    })
35});
36
37impl Display for CopyProtection {
38    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
39        f.write_str("copyprotection ")?;
40        
41        match self {
42            Self::Ok => f.write_str("ok")?,
43            Self::Error => f.write_str("error")?,
44        }
45        
46        Ok(())
47    }
48}
49
50#[cfg(test)]
51#[allow(clippy::unwrap_used)]
52mod tests {
53    use super::CopyProtection;
54    use alloc::string::ToString;
55    use crate::{MessageParseError, MessageParseErrorKind};
56    use crate::dev_macros::{assert_from_str_message, assert_message_to_from_str};
57
58    #[test]
59    fn to_from_str_ok() {
60        let m = CopyProtection::Ok;
61        let str = "copyprotection ok";
62        assert_message_to_from_str!(engine m, str);
63    }
64
65    #[test]
66    fn to_from_str_error() {
67        let m = CopyProtection::Error;
68        let str = "copyprotection error";
69        assert_message_to_from_str!(engine m, str);
70    }
71
72    #[test]
73    fn parse_error() {
74        assert_from_str_message!(engine "copyprotection why   \t are you here? 🤨\n\n", Err::<CopyProtection, MessageParseError>(MessageParseError {
75            expected: "ok or error",
76            kind: MessageParseErrorKind::ValueParseError,
77        }));
78    }
79}