kicad_parse_gen/
fp_lib_table.rs

1// (c) 2017 Productize SPRL <joost@productize.be>
2
3// filename: fp-lib-table
4// format: new-style
5
6use symbolic_expressions;
7use symbolic_expressions::{IntoSexp, Sexp, SexpError};
8use formatter::KicadFormatter;
9use symbolic_expressions::iteratom::*;
10use shellexpand;
11use error::KicadError;
12
13/// a fp-lib-table
14#[derive(Debug, Clone)]
15pub struct FpLibTable {
16    /// the library references contained in the fp-lib-table
17    pub libs: Vec<Lib>,
18}
19
20/// a library entry
21#[derive(Debug, Clone)]
22pub struct Lib {
23    /// name of the library
24    pub name: String,
25    /// type of the library (typically Kicad)
26    pub type_: String,
27    /// the URI of the library (can contain shell variables)
28    pub uri: String,
29    /// options
30    pub options: String,
31    /// description
32    pub descr: String,
33}
34
35impl Lib {
36    /// return the URI with environment variables substituted
37    pub fn get_expanded_uri(&self) -> Result<String, KicadError> {
38        let s = shellexpand::full(&self.uri)?;
39        Ok(s.into())
40    }
41}
42
43impl IntoSexp for FpLibTable {
44    fn into_sexp(&self) -> Sexp {
45        let mut v = Sexp::start("fp_lib_table");
46        for e in &self.libs {
47            v.push(e.into_sexp())
48        }
49        v
50    }
51}
52
53impl IntoSexp for Lib {
54    fn into_sexp(&self) -> Sexp {
55        let mut v = Sexp::start("lib");
56        v.push(("name", &self.name));
57        v.push(("type", &self.type_));
58        v.push(("uri", &self.uri));
59        v.push(("options", &self.options));
60        v.push(("descr", &self.descr));
61        v
62    }
63}
64
65impl FromSexp for FpLibTable {
66    fn from_sexp(s: &Sexp) -> Result<FpLibTable, SexpError> {
67        let mut i = IterAtom::new(s, "fp_lib_table")?;
68        let libs = i.vec()?;
69        Ok(FpLibTable { libs: libs })
70    }
71}
72
73impl FromSexp for Lib {
74    fn from_sexp(s: &Sexp) -> Result<Lib, SexpError> {
75        let mut i = IterAtom::new(s, "lib")?;
76        let name = i.s_in_list("name")?;
77        let type_ = i.s_in_list("type")?;
78        let uri = i.s_in_list("uri")?;
79        let options = i.s_in_list("options")?;
80        let descr = i.s_in_list("descr")?;
81        Ok(Lib {
82            name: name,
83            type_: type_,
84            uri: uri,
85            options: options,
86            descr: descr,
87        })
88    }
89}
90
91/// parse a &str to an `FpLibTable`
92pub fn parse(s: &str) -> Result<FpLibTable, SexpError> {
93    let t = symbolic_expressions::parser::parse_str(s)?;
94    let s = symbolic_expressions::from_sexp(&t)?;
95    Ok(s)
96}
97
98/// convert an `FpLibTable` to a formatted symbolic-expressions String
99pub fn to_string(fp_lib_table: &FpLibTable, indent_level: i64) -> Result<String, KicadError> {
100    let formatter = KicadFormatter::new(indent_level);
101    symbolic_expressions::ser::to_string_with_formatter(&fp_lib_table.into_sexp(), formatter)
102        .map_err(From::from)
103}