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
//! Types for user flags which can appear in rooms.
use std::fmt;

use serde::de::{Deserializer, Error, Unexpected, Visitor};

/// Single flag.
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct Flag {
    /// The name of the flag, unique per user.
    pub name: String,
    /// The primary color of the flag.
    pub primary_color: FlagColor,
    /// The secondary color of the flag.
    pub secondary_color: FlagColor,
    /// The X position of the flag.
    pub x: u32,
    /// The Y position of the flag.
    pub y: u32,
}

/// All possible colors a flag can have.
#[repr(u8)]
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub enum FlagColor {
    /// COLOR_RED = 1
    Red = 1,
    /// COLOR_PURPLE = 2
    Purple = 2,
    /// COLOR_BLUE = 3
    Blue = 3,
    /// COLOR_CYAN = 4
    Cyan = 4,
    /// COLOR_GREEN = 5
    Green = 5,
    /// COLOR_YELLOW = 6
    Yellow = 6,
    /// COLOR_ORANGE = 7
    Orange = 7,
    /// COLOR_BROWN = 8
    Brown = 8,
    /// COLOR_GREY = 9
    Grey = 9,
    /// COLOR_WHITE = 10
    White = 10,
}

/// An error resulting from trying to convert a [`FlagColor`] that's out of bounds.
///
/// [`FlagColor`]: enum.FlagColor.html
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct FlagColorError;

impl FlagColor {
    /// Converts an integer color code into a flag color.
    #[inline]
    pub fn from(v: u8) -> Result<Self, FlagColorError> {
        match v {
            1 => Ok(FlagColor::Red),
            2 => Ok(FlagColor::Purple),
            3 => Ok(FlagColor::Blue),
            4 => Ok(FlagColor::Cyan),
            5 => Ok(FlagColor::Green),
            6 => Ok(FlagColor::Yellow),
            7 => Ok(FlagColor::Orange),
            8 => Ok(FlagColor::Brown),
            9 => Ok(FlagColor::Grey),
            10 => Ok(FlagColor::White),
            _ => Err(FlagColorError),
        }
    }

    #[inline]
    fn from_serde<E: Error>(v: u8) -> Result<Self, E> {
        match v {
            1 => Ok(FlagColor::Red),
            2 => Ok(FlagColor::Purple),
            3 => Ok(FlagColor::Blue),
            4 => Ok(FlagColor::Cyan),
            5 => Ok(FlagColor::Green),
            6 => Ok(FlagColor::Yellow),
            7 => Ok(FlagColor::Orange),
            8 => Ok(FlagColor::Brown),
            9 => Ok(FlagColor::Grey),
            10 => Ok(FlagColor::White),
            other => Err(E::invalid_value(
                Unexpected::Unsigned(other as u64),
                &"an integer between 1 and 10",
            )),
        }
    }
}

struct FlagStringVisitor;

impl<'de> Visitor<'de> for FlagStringVisitor {
    type Value = Vec<Flag>;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("a string with flag formatting (`Flag571~2~3~14~7|`...)")
    }

    #[inline]
    fn visit_unit<E>(self) -> Result<Self::Value, E>
    where
        E: Error,
    {
        Ok(Vec::new())
    }

    #[inline]
    fn visit_none<E>(self) -> Result<Self::Value, E>
    where
        E: Error,
    {
        Ok(Vec::new())
    }

    #[inline]
    fn visit_some<D>(self, d: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        d.deserialize_str(self)
    }

    #[inline]
    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
    where
        E: Error,
    {
        if v.is_empty() {
            return Ok(Vec::new());
        }

        // TODO: nocopy version of this maybe?
        v.split('|')
            .map(|flag_str| {
                let mut iter = flag_str.split('~');

                macro_rules! next {
                    () => {{
                        iter.next().ok_or_else(|| {
                            E::invalid_type(
                                Unexpected::Str(flag_str),
                                &"a string in the format of name~4~2~4~2",
                            )
                        })?
                    }};
                }

                macro_rules! next_u8 {
                    () => {{
                        let next = next!();
                        next.parse().map_err(|_| {
                            E::invalid_type(Unexpected::Str(next), &"an unsigned integer")
                        })?
                    }};
                }

                macro_rules! next_color {
                    () => {{
                        FlagColor::from_serde(next_u8!())?
                    }};
                }

                Ok(Flag {
                    name: next!().to_owned(),
                    primary_color: next_color!(),
                    secondary_color: next_color!(),
                    x: next_u8!(),
                    y: next_u8!(),
                })
            })
            .collect::<Result<Vec<_>, _>>()
    }
}

pub(super) fn deserialize_flags<'de, D>(deserializer: D) -> Result<Vec<Flag>, D::Error>
where
    D: Deserializer<'de>,
{
    deserializer.deserialize_option(FlagStringVisitor)
}