plugx_config/parser/
closure.rs1use crate::parser::Parser;
55use plugx_input::Input;
56use std::fmt::{Debug, Display, Formatter};
57
58pub type BoxedParserFn = Box<dyn Fn(&[u8]) -> anyhow::Result<Input> + Send + Sync>;
60pub type BoxedValidatorFn = Box<dyn Fn(&[u8]) -> Option<bool> + Send + Sync>;
62
63pub struct Closure {
65 name: String,
66 parser: BoxedParserFn,
67 validator: BoxedValidatorFn,
68 supported_format_list: Vec<String>,
69}
70
71impl Display for Closure {
72 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
73 f.write_str(self.name.as_str())
74 }
75}
76
77impl Debug for Closure {
78 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
79 f.debug_struct("ConfigurationParserFn")
80 .field("name", &self.name)
81 .field("supported_format_list", &self.supported_format_list)
82 .finish()
83 }
84}
85
86impl Closure {
87 pub fn new<N: AsRef<str>, F: AsRef<str>>(
88 name: N,
89 supported_format: F,
90 parser: BoxedParserFn,
91 ) -> Self {
92 Self {
93 name: name.as_ref().to_string(),
94 parser,
95 validator: Box::new(|_| None),
96 supported_format_list: [supported_format.as_ref().to_string()].to_vec(),
97 }
98 }
99
100 pub fn set_parser(&mut self, parser: BoxedParserFn) {
101 self.parser = parser;
102 }
103
104 pub fn with_parser(mut self, parser: BoxedParserFn) -> Self {
105 self.set_parser(parser);
106 self
107 }
108
109 pub fn set_validator(&mut self, validator: BoxedValidatorFn) {
110 self.validator = validator;
111 }
112
113 pub fn with_validator(mut self, validator: BoxedValidatorFn) -> Self {
114 self.set_validator(validator);
115 self
116 }
117
118 pub fn set_format_list<N: AsRef<str>>(&mut self, format_list: &[N]) {
119 self.supported_format_list = format_list
120 .iter()
121 .map(|format| format.as_ref().to_string())
122 .collect();
123 }
124
125 pub fn with_format_list<N: AsRef<str>>(mut self, format_list: &[N]) -> Self {
126 self.set_format_list(format_list);
127 self
128 }
129}
130
131impl Parser for Closure {
132 fn supported_format_list(&self) -> Vec<String> {
133 self.supported_format_list.clone()
134 }
135
136 fn try_parse(&self, bytes: &[u8]) -> anyhow::Result<Input> {
137 (self.parser)(bytes)
138 }
139
140 fn is_format_supported(&self, bytes: &[u8]) -> Option<bool> {
141 (self.validator)(bytes)
142 }
143}