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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
use core::ops::Deref;
use parking_lot::Mutex;
use std::{collections::HashSet, path::PathBuf, vec};

use crate::{
    wrapper::IORef, FromEnvironment, IORefT, IsProperty, Key, Property, PropertyError,
    PropertySource, SalakContext, SubKey, SubKeys, PREFIX,
};
#[cfg(feature = "derive")]
#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
use crate::{DescFromEnvironment, KeyDesc, PrefixedFromEnvironment, SalakDescContext};
use crate::{Res, Void};

enum PS<'a> {
    Ref(&'a Box<dyn PropertySource>),
    Own(Box<dyn PropertySource>),
}

impl Deref for PS<'_> {
    type Target = dyn PropertySource;

    fn deref(&self) -> &Self::Target {
        match self {
            PS::Own(f) => f.as_ref(),
            PS::Ref(f) => f.as_ref(),
        }
    }
}

pub(crate) struct PropertyRegistryInternal<'a> {
    name: &'a str,
    providers: Vec<PS<'a>>,
}

impl PropertySource for PropertyRegistryInternal<'_> {
    fn name(&self) -> &str {
        self.name
    }

    #[inline]
    fn get_property(&self, key: &Key<'_>) -> Option<Property<'_>> {
        self.providers.iter().find_map(|p| p.get_property(key))
    }

    fn is_empty(&self) -> bool {
        self.providers.is_empty() || self.providers.iter().all(|f| f.is_empty())
    }

    fn get_sub_keys<'a>(&'a self, key: &Key<'_>, sub_keys: &mut SubKeys<'a>) {
        self.providers
            .iter()
            .for_each(|f| f.get_sub_keys(key, sub_keys));
    }
}

impl<'a> PropertyRegistryInternal<'a> {
    pub(crate) fn register_by_ref(&mut self, provider: Box<dyn PropertySource>) {
        if !provider.is_empty() {
            #[cfg(feature = "log")]
            log::info!("Register source {}.", provider.name());
            self.providers.push(PS::Own(provider));
        }
    }

    pub(crate) fn register<P: PropertySource + Send + Sync + 'static>(
        mut self,
        provider: P,
    ) -> Self {
        self.register_by_ref(Box::new(provider));
        self
    }

    pub(crate) fn new(name: &'a str) -> Self {
        Self {
            name,
            providers: vec![],
        }
    }

    fn get(
        &'a self,
        key: &mut Key<'_>,
        def: Option<Property<'a>>,
    ) -> Result<Option<Property<'a>>, PropertyError> {
        let tmp;
        let v = match self.get_property(key).or(def) {
            Some(Property::S(v)) => v,
            Some(Property::O(v)) => {
                tmp = v;
                &tmp[..]
            }
            v => return Ok(v),
        };
        let mut history = HashSet::new();
        history.insert(key.as_str().to_string());
        Ok(Some(self.resolve(key, v, &mut history)?))
    }

    #[inline]
    fn merge(val: Option<String>, new: &str) -> String {
        match val {
            Some(mut v) => {
                v.push_str(new);
                v
            }
            None => new.to_owned(),
        }
    }

    #[inline]
    fn resolve(
        &self,
        key: &Key<'_>,
        mut val: &str,
        history: &mut HashSet<String>,
    ) -> Result<Property<'_>, PropertyError> {
        let mut stack = vec!["".to_owned()];
        let pat: &[_] = &['$', '\\', '}'];

        while let Some(pos) = val.find(pat) {
            match &val[pos..=pos] {
                "$" => {
                    let pos_1 = pos + 1;
                    if val.len() == pos_1 || &val[pos_1..=pos_1] != "{" {
                        return Err(PropertyError::ResolveFail(key.as_str().to_string()));
                    }
                    let last = stack.pop();
                    stack.push(Self::merge(last, &val[..pos]));
                    stack.push("".to_owned());
                    val = &val[pos + 2..];
                }
                "\\" => {
                    let pos_1 = pos + 1;
                    if val.len() == pos_1 {
                        return Err(PropertyError::ResolveFail(key.as_str().to_string()));
                    }
                    let last = stack.pop();
                    let mut v = Self::merge(last, &val[..pos]);
                    v.push_str(&val[pos_1..=pos_1]);
                    stack.push(v);
                    val = &val[pos + 2..];
                }
                "}" => {
                    let last = stack.pop();
                    let v = Self::merge(last, &val[..pos]);
                    let (key, def) = match v.find(':') {
                        Some(pos) => (&v[..pos], Some(&v[pos + 1..])),
                        _ => (&v[..], None),
                    };
                    if !history.insert(key.to_string()) {
                        return Err(PropertyError::RecursiveFail(key.to_owned()));
                    }
                    let v = if let Some(p) = self.get(&mut Key::from_str(key), None)? {
                        String::from_property(p)?
                    } else if let Some(d) = def {
                        d.to_owned()
                    } else {
                        return Err(PropertyError::ResolveNotFound(key.to_string()));
                    };
                    history.remove(key);
                    let v = Self::merge(stack.pop(), &v);
                    stack.push(v);
                    val = &val[pos + 1..];
                }
                _ => return Err(PropertyError::ResolveFail(key.as_str().to_string())),
            }
        }
        if let Some(mut v) = stack.pop() {
            if stack.is_empty() {
                v.push_str(val);
                return Ok(Property::O(v));
            }
        }
        Err(PropertyError::ResolveFail(key.as_str().to_string()))
    }

    pub(crate) fn reload(&self, iorefs: &'a Mutex<Vec<Box<dyn IORefT + Send>>>) -> Res<bool> {
        let mut flag = false;
        let registry = PropertyRegistryInternal {
            name: "reload",
            providers: self
                .providers
                .iter()
                .map(|f| match f.reload_source() {
                    Ok(None) => Ok(match f {
                        PS::Own(v) => PS::Ref(&*v),
                        PS::Ref(v) => PS::Ref(*v),
                    }),
                    Ok(Some(v)) => {
                        flag = true;
                        Ok(PS::Own(v))
                    }
                    Err(err) => Err(err),
                })
                .collect::<Result<Vec<PS<'_>>, PropertyError>>()?,
        };

        let guard = iorefs.lock();
        for io in guard.iter() {
            io.reload_ref(&registry, iorefs)?;
        }
        Ok(flag)
    }

    #[inline]
    pub(crate) fn require<T: FromEnvironment>(
        &self,
        sub_key: &str,
        iorefs: &'a Mutex<Vec<Box<dyn IORefT + Send>>>,
    ) -> Res<T> {
        let mut key = Key::new();
        SalakContext::new(&self, iorefs, &mut key).require_def(sub_key, None)
    }
}
#[cfg(feature = "derive")]
#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
impl<'a> SalakDescContext<'a> {
    pub(crate) fn new(key: &'a mut Key<'a>, descs: &'a mut Vec<KeyDesc>) -> Self {
        let current = KeyDesc::new(key.as_str().to_string(), "String", None, None, None);
        Self {
            key,
            descs,
            current,
        }
    }

    /// Add key description.
    #[inline]
    pub fn add_key_desc<T: DescFromEnvironment>(
        &mut self,
        sub_key: &'a str,
        required: Option<bool>,
        def: Option<&'a str>,
        desc: Option<String>,
    ) {
        self.add_key_desc_internal::<T, &str>(sub_key, required, def, desc)
    }

    pub(crate) fn add_key_desc_internal<T: DescFromEnvironment, K: Into<SubKey<'a>>>(
        &mut self,
        sub_key: K,
        required: Option<bool>,
        def: Option<&'a str>,
        desc: Option<String>,
    ) {
        self.into_sub_key(sub_key);
        let key = self.key.as_generic();
        let bak = std::mem::replace(
            &mut self.current,
            KeyDesc::new(key, std::any::type_name::<T>(), required, def, desc),
        );
        T::key_desc(self);
        let desc = std::mem::replace(&mut self.current, bak);
        if !desc.ignore {
            self.descs.push(desc);
        }
        self.key.pop();
    }
    fn into_sub_key<K: Into<SubKey<'a>>>(&mut self, k: K) {
        self.key.push(k.into());
    }
}

impl<'a> SalakContext<'a> {
    /// Parse property from env.
    #[inline]
    pub fn require_def<T: FromEnvironment>(
        &mut self,
        sub_key: &'a str,
        def: Option<Property<'_>>,
    ) -> Res<T> {
        self.require_def_internal(sub_key, def)
    }

    #[inline]
    pub(crate) fn require_def_internal<T: FromEnvironment, K: Into<SubKey<'a>>>(
        &mut self,
        sub_key: K,
        def: Option<Property<'_>>,
    ) -> Res<T> {
        let flag = self.into_sub_key(sub_key);
        let val = match self.registry.get(self.key, def) {
            Ok(val) => Ok(T::from_env(val, self)),
            Err(e) => Err(e),
        };
        if flag {
            self.key.pop();
        }
        match val? {
            Err(PropertyError::ParseFail(None, v)) if !self.key.as_str().is_empty() => Err(
                PropertyError::ParseFail(Some(self.key.as_str().to_string()), v),
            ),
            val => val,
        }
    }

    pub(crate) fn get_sub_keys(&mut self) -> SubKeys<'a> {
        let mut sub_keys = SubKeys::new();
        self.registry.get_sub_keys(&mut self.key, &mut sub_keys);
        sub_keys
    }

    #[inline]
    pub(crate) fn current_key(&self) -> &str {
        self.key.as_str()
    }

    fn into_sub_key<K: Into<SubKey<'a>>>(&mut self, k: K) -> bool {
        let v = k.into();
        let flag = !v.is_empty();
        if flag {
            self.key.push(v);
        }
        return flag;
    }

    pub(crate) fn new(
        registry: &'a PropertyRegistryInternal<'a>,
        iorefs: &'a Mutex<Vec<Box<dyn IORefT + Send>>>,
        key: &'a mut Key<'a>,
    ) -> Self {
        Self {
            registry,
            key,
            iorefs,
        }
    }

    #[inline]
    pub(crate) fn register_ioref<T: Clone + FromEnvironment + Send + 'static>(
        &self,
        ioref: &IORef<T>,
    ) {
        let mut guard = self.iorefs.lock();
        let io = ioref.clone();
        guard.push(Box::new(io));
    }
}

impl<T: FromEnvironment> FromEnvironment for Option<T> {
    fn from_env(val: Option<Property<'_>>, env: &mut SalakContext<'_>) -> Res<Self> {
        match T::from_env(val, env) {
            Ok(v) => Ok(Some(v)),
            Err(PropertyError::NotFound(_)) => Ok(None),
            Err(err) => Err(err),
        }
    }
}

#[cfg(feature = "derive")]
#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
impl<T: DescFromEnvironment> DescFromEnvironment for Option<T> {
    fn key_desc(env: &mut SalakDescContext<'_>) {
        env.current.set_required(false);
        T::key_desc(env);
    }
}

pub(crate) struct FileConfig {
    dir: Option<String>,
    name: String,
    profile: String,
    env_profile: PropertyRegistryInternal<'static>,
    env_default: PropertyRegistryInternal<'static>,
}

impl FromEnvironment for FileConfig {
    fn from_env(_: Option<Property<'_>>, env: &mut SalakContext<'_>) -> Res<Self> {
        Ok(FileConfig {
            dir: env.require_def("dir", None)?,
            name: env.require_def("filename", Some(Property::S("app")))?,
            profile: env.require_def("profile", Some(Property::S("default")))?,
            env_profile: PropertyRegistryInternal::new("profile-files"),
            env_default: PropertyRegistryInternal::new("default-files"),
        })
    }
}

#[cfg(feature = "derive")]
#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
impl DescFromEnvironment for FileConfig {
    fn key_desc(env: &mut SalakDescContext<'_>) {
        env.add_key_desc::<Option<String>>("dir", None, None, None);
        env.add_key_desc::<String>("filename", Some(false), Some("app"), None);
        env.add_key_desc::<String>("profile", Some(false), Some("default"), None);
    }
}

#[cfg(feature = "derive")]
impl PrefixedFromEnvironment for FileConfig {
    fn prefix() -> &'static str {
        PREFIX
    }
}

impl FileConfig {
    #[allow(dead_code)]
    pub(crate) fn new(
        env: &PropertyRegistryInternal<'_>,
        iorefs: &Mutex<Vec<Box<dyn IORefT + Send>>>,
    ) -> Res<Self> {
        env.require::<FileConfig>(PREFIX, iorefs)
    }

    #[allow(dead_code)]
    pub(crate) fn register_to_env(self, env: &mut PropertyRegistryInternal<'_>) {
        env.register_by_ref(Box::new(self.env_profile));
        env.register_by_ref(Box::new(self.env_default));
    }

    #[allow(dead_code)]
    pub(crate) fn build<F: Fn(FileItem) -> Res<S>, S: PropertySource + 'static>(
        &mut self,
        ext: &str,
        f: F,
    ) -> Void {
        fn make<F: Fn(FileItem) -> Res<S>, S: PropertySource + 'static>(
            f: F,
            file: String,
            dir: &Option<String>,
            env: &mut PropertyRegistryInternal<'_>,
        ) -> Void {
            let mut path = PathBuf::new();
            if let Some(d) = &dir {
                path.push(d);
            }
            path.push(file);
            if path.exists() {
                env.register_by_ref(Box::new((f)(FileItem(path))?));
            }
            Ok(())
        }

        make(
            &f,
            format!("{}-{}.{}", self.name, self.profile, ext),
            &self.dir,
            &mut self.env_profile,
        )?;
        make(
            &f,
            format!("{}.{}", self.name, ext),
            &self.dir,
            &mut self.env_default,
        )
    }
}

#[derive(Debug, Clone)]
pub(crate) struct FileItem(PathBuf);

#[allow(dead_code)]
impl FileItem {
    pub(crate) fn load(&self) -> Res<String> {
        Ok(std::fs::read_to_string(self.0.clone())?)
    }

    pub(crate) fn name(&self) -> String {
        self.0.as_path().display().to_string()
    }
}

#[cfg(test)]
mod tests {
    use raw_ioref::IORef;

    use crate::{
        source::{Key, SubKeys},
        *,
    };

    struct Reload(u64);

    impl PropertySource for Reload {
        fn name(&self) -> &str {
            "reload"
        }

        fn get_property(&self, _: &Key<'_>) -> Option<Property<'_>> {
            Some(Property::I(self.0 as i64))
        }

        fn get_sub_keys<'a>(&'a self, _: &Key<'_>, _: &mut SubKeys<'a>) {}

        fn is_empty(&self) -> bool {
            false
        }
        fn reload_source(&self) -> Result<Option<Box<dyn PropertySource>>, PropertyError> {
            Ok(Some(Box::new(Reload(self.0 + 1))))
        }
    }

    #[test]
    fn reload_test() {
        let mut env = Salak::new().unwrap();
        env.register(Reload(0));
        let u8ref = env.require::<IORef<u8>>("").unwrap();
        assert_eq!(0, u8ref.get_val().unwrap());
        env.reload().unwrap();
        assert_eq!(1, u8ref.get_val().unwrap());
        env.reload().unwrap();
        assert_eq!(1, u8ref.get_val().unwrap());
    }
}