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
//! Custom configuration parser with [Fn].
//!
//! ### Example
//! In the following example we implement a complete [HJSON](https://hjson.github.io/) parser.
//!
//! ```rust
//! use plugx_config::{
//!     ext::{plugx_input::Input, anyhow::anyhow},
//!     error::ConfigurationParserError,
//!     parser::{ConfigurationParser, closure::ConfigurationParserFn}
//! };
//!
//! let parser_fn = |bytes: &[u8]| -> anyhow::Result<Input> {
//!     deser_hjson::from_slice(bytes).map_err(|error| anyhow!(error))
//! };
//!
//! let parser_name = "HJSON";
//! let parser_format = "hjson";
//! let parser = ConfigurationParserFn::new("HJSNO", "hjson", Box::new(parser_fn));
//! let bytes = br#"
//! {
//!     hello: ["w", "o", "l", "d"]
//!     foo: {
//!         bar: {
//!             baz: Qux
//!             abc: 3.14
//!         }
//!         xyz: false
//!     }
//! }
//! "#;
//! let parsed = parser.parse(bytes).unwrap();
//! assert!(
//!     parsed.as_map().len() == 2 &&
//!     parsed.as_map().contains_key("foo") &&
//!     parsed.as_map().contains_key("hello")
//! );
//! let foo = parsed.as_map().get("foo").unwrap();
//! assert!(
//!     foo.as_map().len() == 2 &&
//!     foo.as_map().contains_key("bar") &&
//!     foo.as_map().contains_key("xyz")
//! );
//! let bar = foo.as_map().get("bar").unwrap();
//! assert_eq!(bar.as_map().get("baz").unwrap(), &"Qux".into());
//! assert_eq!(bar.as_map().get("abc").unwrap(), &3.14.into());
//! let xyz = foo.as_map().get("xyz").unwrap();
//! assert_eq!(xyz, &false.into());
//! let list = ["w", "o", "l", "d"].into();
//! assert_eq!(parsed.as_map().get("hello").unwrap(), &list);
//! ```
//!

use crate::parser::ConfigurationParser;
use plugx_input::Input;
use std::fmt::{Debug, Display, Formatter};

/// A `|&[u8]| -> Result<Input, ConfigurationParserError>` [Fn] to parse contents.
pub type BoxedParserFn = Box<dyn Fn(&[u8]) -> anyhow::Result<Input> + Send + Sync>;
/// A `|&[u8]| -> Option<bool>` [Fn] to validate contents.
pub type BoxedValidatorFn = Box<dyn Fn(&[u8]) -> Option<bool> + Send + Sync>;

/// Builder struct.
pub struct ConfigurationParserFn {
    name: String,
    parser: BoxedParserFn,
    validator: BoxedValidatorFn,
    supported_format_list: Vec<String>,
}

impl Display for ConfigurationParserFn {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str(
            self.supported_format_list
                .iter()
                .last()
                .map_or("unknown", |format| format.as_str()),
        )
    }
}

impl Debug for ConfigurationParserFn {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ConfigurationParserFn")
            .field("name", &self.name)
            .field("supported_format_list", &self.supported_format_list)
            .finish()
    }
}

impl ConfigurationParserFn {
    pub fn new<N: AsRef<str>, F: AsRef<str>>(
        name: N,
        supported_format: F,
        parser: BoxedParserFn,
    ) -> Self {
        Self {
            name: name.as_ref().to_string(),
            parser,
            validator: Box::new(|_| None),
            supported_format_list: [supported_format.as_ref().to_string()].to_vec(),
        }
    }

    pub fn set_parser(&mut self, parser: BoxedParserFn) {
        self.parser = parser;
    }

    pub fn with_parser(mut self, parser: BoxedParserFn) -> Self {
        self.set_parser(parser);
        self
    }

    pub fn set_validator(&mut self, validator: BoxedValidatorFn) {
        self.validator = validator;
    }

    pub fn with_validator(mut self, validator: BoxedValidatorFn) -> Self {
        self.set_validator(validator);
        self
    }

    pub fn set_format_list<N: AsRef<str>>(&mut self, format_list: &[N]) {
        self.supported_format_list = format_list
            .iter()
            .map(|format| format.as_ref().to_string())
            .collect();
    }

    pub fn with_format_list<N: AsRef<str>>(mut self, format_list: &[N]) -> Self {
        self.set_format_list(format_list);
        self
    }
}

impl ConfigurationParser for ConfigurationParserFn {
    fn name(&self) -> String {
        self.name.clone()
    }

    fn supported_format_list(&self) -> Vec<String> {
        self.supported_format_list.clone()
    }

    fn try_parse(&self, bytes: &[u8]) -> anyhow::Result<Input> {
        (self.parser)(bytes)
    }

    fn is_format_supported(&self, bytes: &[u8]) -> Option<bool> {
        (self.validator)(bytes)
    }
}