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
use crate::errors::*;
use serde::de::{self, Deserializer, Visitor};
use serde::Deserialize;
use smart_default::SmartDefault;
use std::fmt;
use std::str::FromStr;

#[derive(Debug, Clone, PartialEq, Eq, SmartDefault)]
pub enum Separator {
    #[default]
    Native,
    Custom(String),
}

impl FromStr for Separator {
    type Err = Error;

    fn from_str(separator: &str) -> Result<Self, Self::Err> {
        Ok(if separator == "native" {
            Self::Native
        } else {
            Self::Custom(separator.into())
        })
    }
}

impl<'de> Deserialize<'de> for Separator {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct SeparatorVisitor;

        impl Visitor<'_> for SeparatorVisitor {
            type Value = Separator;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("a separator string or 'native'")
            }

            fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                s.parse().serde_error()
            }
        }

        deserializer.deserialize_any(SeparatorVisitor)
    }
}