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
use lzlist::{LzList,Lz};
use std::ops::Deref;
use flag;
use brace;
use get::SGetter;
use std::path::PathBuf;

pub struct Cfg{
    list:LzList,
    loc:PathBuf,
}

impl Cfg{
    //Will return a Cfg even if there are no items.
    pub fn load(loc:&str)->Cfg{
        let rloc = &brace::env_replace(loc);
        match LzList::load(rloc) {
            Ok(r)=>Cfg{
                list:r,
                loc:PathBuf::from(rloc),
            },
            Err(_)=>Cfg{
                list:LzList::empty(),
                loc:PathBuf::from(&brace::env("$PWD")),
            }
        }
    }

    //load_first
    pub fn load_first<'a,L,S>(fgname:&str,locs:L)->Cfg
        where L:IntoIterator<Item=S>,
            S:AsRef<str>,
    {
        let floc = flag::Fg{}.get_s(fgname);
        match floc{
            Some(s)=>return Cfg::load(&s),
            _=>{},
        }

        for l in locs {
            let l2 = &brace::env_replace(&l.as_ref());
            match LzList::load(l2){
                Ok(r)=>return Cfg{list:r,loc:PathBuf::from(l2)},
                _=>{},
            }
        }
        Cfg{
            list:LzList::empty(),
            loc:PathBuf::from(""),
        }
    }

    pub fn localize(&self,s:&str)->PathBuf{
        let mut res = self.folder();
        res.push(s);
        res
    }

    pub fn folder(&self)->PathBuf{
        match self.loc.parent(){
            Some(r)=>PathBuf::from(r),
            _ => PathBuf::new(),
        }
    }
    pub fn lz_by_name(&self,s:&str)->Option<Lz>{
        self.list.lz_by_name(s)
    }
}

impl <'a> SGetter<(&'a str,&'a str)> for Cfg{
    fn get_s(&self, fc:(&str,&str))->Option<String>{
        let (fg,ct) = fc; 
        let fres = flag::Fg{}.get_s(fg);
        match fres {
            Some(_)=>return fres,
            _=>{},
        }
        self.list.get_s(ct)
    }
}