aboutsummaryrefslogtreecommitdiff
path: root/alacritty_terminal/src/ansi.rs
blob: b596cc828bfab36e7a426d1323275457cc011073 (plain)
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
//! ANSI Terminal Stream Parsing.

pub use vte::ansi::*;

#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
pub struct CursorShapeShim(CursorShape);

impl Default for CursorShapeShim {
    fn default() -> CursorShapeShim {
        CursorShapeShim(CursorShape::Block)
    }
}

impl From<CursorShapeShim> for CursorShape {
    fn from(value: CursorShapeShim) -> Self {
        value.0
    }
}

struct CursorShapeVisitor;

impl<'de> serde::de::Visitor<'de> for CursorShapeVisitor {
    type Value = CursorShapeShim;

    fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str("one of `Block`, `Underline`, `Beam`")
    }

    fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        match s.to_lowercase().as_str() {
            "block" => Ok(CursorShapeShim(CursorShape::Block)),
            "underline" => Ok(CursorShapeShim(CursorShape::Underline)),
            "beam" => Ok(CursorShapeShim(CursorShape::Beam)),
            _ => Err(E::custom(format!(
                "unknown variant `{0}`, expected {1}",
                s, "one of `Block`, `Underline`, `Beam`"
            ))),
        }
    }
}

impl<'de> serde::Deserialize<'de> for CursorShapeShim {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_str(CursorShapeVisitor)
    }
}

impl alacritty_config::SerdeReplace for CursorShapeShim {
    fn replace(
        &mut self,
        key: &str,
        value: serde_yaml::Value,
    ) -> Result<(), Box<dyn std::error::Error>> {
        if !key.is_empty() {
            return Err(format!("Fields \"{0}\" do not exist", key).into());
        }

        *self = serde::Deserialize::deserialize(value)?;
        Ok(())
    }
}