use symbolic_expressions;
use symbolic_expressions::{IntoSexp, Sexp, SexpError};
use formatter::KicadFormatter;
use symbolic_expressions::iteratom::*;
use shellexpand;
use error::KicadError;
#[derive(Debug, Clone)]
pub struct FpLibTable {
pub libs: Vec<Lib>,
}
#[derive(Debug, Clone)]
pub struct Lib {
pub name: String,
pub type_: String,
pub uri: String,
pub options: String,
pub descr: String,
}
impl Lib {
pub fn get_expanded_uri(&self) -> Result<String, KicadError> {
let s = shellexpand::full(&self.uri)?;
Ok(s.into())
}
}
impl IntoSexp for FpLibTable {
fn into_sexp(&self) -> Sexp {
let mut v = Sexp::start("fp_lib_table");
for e in &self.libs {
v.push(e.into_sexp())
}
v
}
}
impl IntoSexp for Lib {
fn into_sexp(&self) -> Sexp {
let mut v = Sexp::start("lib");
v.push(("name", &self.name));
v.push(("type", &self.type_));
v.push(("uri", &self.uri));
v.push(("options", &self.options));
v.push(("descr", &self.descr));
v
}
}
impl FromSexp for FpLibTable {
fn from_sexp(s: &Sexp) -> Result<FpLibTable, SexpError> {
let mut i = IterAtom::new(s, "fp_lib_table")?;
let libs = i.vec()?;
Ok(FpLibTable { libs: libs })
}
}
impl FromSexp for Lib {
fn from_sexp(s: &Sexp) -> Result<Lib, SexpError> {
let mut i = IterAtom::new(s, "lib")?;
let name = i.s_in_list("name")?;
let type_ = i.s_in_list("type")?;
let uri = i.s_in_list("uri")?;
let options = i.s_in_list("options")?;
let descr = i.s_in_list("descr")?;
Ok(Lib {
name: name,
type_: type_,
uri: uri,
options: options,
descr: descr,
})
}
}
pub fn parse(s: &str) -> Result<FpLibTable, SexpError> {
let t = symbolic_expressions::parser::parse_str(s)?;
let s = symbolic_expressions::from_sexp(&t)?;
Ok(s)
}
pub fn to_string(fp_lib_table: &FpLibTable, indent_level: i64) -> Result<String, KicadError> {
let formatter = KicadFormatter::new(indent_level);
symbolic_expressions::ser::to_string_with_formatter(&fp_lib_table.into_sexp(), formatter)
.map_err(From::from)
}