Skip to content

Commit

Permalink
Use handlebars instead of rumblebars, moved code around a little bit
Browse files Browse the repository at this point in the history
  • Loading branch information
tailhook committed Sep 25, 2015
1 parent e935ba7 commit e94a2ef
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 219 deletions.
118 changes: 24 additions & 94 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ hlua = "0.1.6"
quick-error = "0.1.3"
liquid = "0.1.1"
argparse = "0.2.0"
walker = "1.0.0"
rumblebars = { git = "git://github.com/nicolas-cherel/rumblebars" }
handlebars = "0.10.1"
log = "*"
env_logger = "0.3.1"
tempfile = "1.1.1"
rustc-serialize = "0.3.16"
quire = { git = "git://github.com/tailhook/rust-quire" }
yaml-rust = "0.2.2"
scan_dir = "0.1.0"
scan_dir = "0.2.0"
123 changes: 123 additions & 0 deletions src/config/meta.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
use std::fs::File;
use std::io::Read;
use std::str::FromStr;
use std::path::Path;
use std::collections::BTreeMap;

use rustc_serialize::json::Json;
use yaml_rust::{Yaml, YamlLoader};
use scan_dir;

use super::{MetadataError, MetadataErrors};


fn convert_yaml(yaml: Yaml, path: &Path) -> Result<Json, MetadataError> {
use super::MetadataError::{Float, BadYamlKey, BadYamlValue};
use yaml_rust::Yaml as Y;
use rustc_serialize::json::Json as J;
let json = match yaml {
Y::Real(x) => J::F64(try!(FromStr::from_str(&x)
.map_err(|e| Float(e, path.to_path_buf())))),
Y::Integer(x) => J::I64(x),
Y::String(x) => J::String(x),
Y::Boolean(x) => J::Boolean(x),
Y::Array(a) => {
let mut r = vec![];
for x in a.into_iter() {
r.push(try!(convert_yaml(x, path)));
}
J::Array(r)
}
Y::Hash(m) => {
let mut r = BTreeMap::new();
for (k, v) in m.into_iter() {
let k = match k {
Y::String(x) => x,
Y::Real(x) => x,
Y::Integer(x) => format!("{}", x),
Y::Boolean(x) => format!("{}", x),
e => return Err(BadYamlKey(e, path.to_path_buf())),
};
r.insert(k, try!(convert_yaml(v, path)));
}
J::Object(r)
}
Y::Null => J::Null,
v @ Y::Alias(_) | v @ Y::BadValue => {
return Err(BadYamlValue(v, path.to_path_buf()))
}
};
Ok(json)
}

fn read_entry(path: &Path, ext: &str)
-> Result<Option<Json>, MetadataError>
{
use super::MetadataError::{FileRead, JsonParse, YamlParse};
let value = match ext {
"yaml" | "yml" => {
debug!("Reading YAML metadata from {:?}", path);
let mut buf = String::with_capacity(1024);
try!(File::open(path)
.and_then(|mut f| f.read_to_string(&mut buf))
.map_err(|e| FileRead(e, path.to_path_buf())));
let mut yaml = try!(YamlLoader::load_from_str(&buf)
.map_err(|e| YamlParse(e, path.to_path_buf())));
if yaml.len() < 1 {
Some(Json::Null)
} else {
Some(try!(convert_yaml(yaml.remove(0), path)))
}
}
"json" => {
debug!("Reading JSON metadata from {:?}", path);
let mut f = try!(File::open(path)
.map_err(|e| FileRead(e, path.to_path_buf())));
Some(try!(Json::from_reader(&mut f)
.map_err(|e| JsonParse(e, path.to_path_buf()))))
}
"txt" => {
debug!("Reading text metadata from {:?}", path);
let mut buf = String::with_capacity(100);
try!(File::open(path)
.and_then(|mut f| f.read_to_string(&mut buf))
.map_err(|e| FileRead(e, path.to_path_buf())));
Some(Json::String(buf))
}
_ => None,
};
Ok(value)
}

pub fn read_dir(path: &Path) -> Result<Json, MetadataErrors> {
use super::MetadataError::ScanDir;

let mut data = BTreeMap::new();
let mut errors = vec!();
scan_dir::ScanDir::files().read(path, |iter| {
for (entry, name) in iter {
let fpath = entry.path();
let ext = fpath.extension().map(|x| x.to_str()).unwrap();
if ext.is_none() { continue; }
match read_entry(&entry.path(), ext.unwrap()) {
Ok(Some(value)) => {
let stem = fpath.file_stem().unwrap().to_str().unwrap();
data.insert(stem.to_string(), value);
}
Ok(None) => {}
Err(e) => {
errors.push(e);
}
}
}
}).map_err(|e| errors.push(ScanDir(e))).ok();

if errors.len() > 0 {
Err(MetadataErrors {
errors: errors,
partial: Json::Object(data),
})
} else {
Ok(Json::Object(data))
}
}

0 comments on commit e94a2ef

Please sign in to comment.