summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2017-12-09 17:22:35 +0100
committerGitHub <noreply@github.com>2017-12-09 17:22:35 +0100
commit026b74dc55bb8862abb070ba3ffc018cf4960cbb (patch)
tree3fc53e020c4076a211c188ea101c9c11787f571e
parentc157347f3f657de692392f36d6a2ee8469d95a44 (diff)
parent69c92645b207cddaad4a29d6b7f9847c04900514 (diff)
Merge pull request #24 from matthiasbeyer/high-level-fns
High level fns
-rw-r--r--CHANGELOG.md7
-rw-r--r--src/delete.rs10
-rw-r--r--src/error.rs10
-rw-r--r--src/lib.rs1
-rw-r--r--src/read.rs55
-rw-r--r--src/util.rs14
6 files changed, 88 insertions, 9 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 80e3814..a33fd38 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,13 @@
This changelog was started with the 0.4.0 release, so there are no logs before
that version.
+# Next
+
+* Added method for requesting a type directly from the TOML document:
+ The method returns the requested type directly, or fails with
+ `Err(_)` and appropriate message:
+ `document.read_string(path) -> Result<String, Error>` (for example)
+
# 0.4.0
* Updated the `error-chain` dependency from `0.10` to `0.11`.
diff --git a/src/delete.rs b/src/delete.rs
index 1146a36..4a97809 100644
--- a/src/delete.rs
+++ b/src/delete.rs
@@ -78,15 +78,7 @@ impl TomlValueDeleteExt for Value {
#[inline]
fn name_of_val(val: Option<&Value>) -> &'static str {
- val.map(|v| match v {
- &Value::Array(_) => "Array",
- &Value::Boolean(_) => "Boolean",
- &Value::Datetime(_) => "Datetime",
- &Value::Float(_) => "Float",
- &Value::Integer(_) => "Integer",
- &Value::String(_) => "String",
- &Value::Table(_) => "Table",
- }).unwrap_or("None")
+ val.map(::util::name_of_val).unwrap_or("None")
}
if last_token.is_none() {
diff --git a/src/error.rs b/src/error.rs
index 7cfca59..895f4ea 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -81,5 +81,15 @@ error_chain! {
display("Cannot delete in array at {}, array has length {}", idx, arrlen)
}
+ TypeError(requested: &'static str, got: &'static str) {
+ description("Type error")
+ display("Type Error. Requested {}, but got {}", requested, got)
+ }
+
+ NotAvailable(query: String) {
+ description("Value missing error")
+ display("Value at '{}' not there", query)
+ }
+
}
}
diff --git a/src/lib.rs b/src/lib.rs
index 61cf752..ec46278 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -31,6 +31,7 @@ pub mod set;
pub mod insert;
pub mod delete;
pub mod value;
+mod util;
// private modules
diff --git a/src/read.rs b/src/read.rs
index 3d4ff39..391d5ec 100644
--- a/src/read.rs
+++ b/src/read.rs
@@ -43,6 +43,41 @@ impl<'doc> TomlValueReadExt<'doc> for Value {
}
+pub trait TomlValueReadTypeExt<'doc> {
+ fn read_string(&'doc self, query: &str) -> Result<String>;
+ fn read_int(&'doc self, query: &str) -> Result<i64>;
+ fn read_float(&'doc self, query: &str) -> Result<f64>;
+ fn read_bool(&'doc self, query: &str) -> Result<bool>;
+}
+
+macro_rules! make_type_getter {
+ ($fnname:ident, $rettype:ty, $typename:expr, $matcher:pat => $implementation:expr) => {
+ fn $fnname(&'doc self, query: &str) -> Result<$rettype> {
+ self.read_with_seperator(query, '.').and_then(|o| match o {
+ $matcher => $implementation,
+ Some(o) => Err(ErrorKind::TypeError($typename, ::util::name_of_val(&o)).into()),
+ None => Err(ErrorKind::NotAvailable(String::from(query)).into()),
+ })
+ }
+ };
+}
+
+impl<'doc, T> TomlValueReadTypeExt<'doc> for T
+ where T: TomlValueReadExt<'doc>
+{
+ make_type_getter!(read_string, String, "String",
+ Some(&Value::String(ref obj)) => Ok(obj.clone()));
+
+ make_type_getter!(read_int, i64, "Integer",
+ Some(&Value::Integer(obj)) => Ok(obj));
+
+ make_type_getter!(read_float, f64, "Float",
+ Some(&Value::Float(obj)) => Ok(obj));
+
+ make_type_getter!(read_bool, bool, "Boolean",
+ Some(&Value::Boolean(obj)) => Ok(obj));
+}
+
#[cfg(test)]
mod test {
use super::*;
@@ -209,3 +244,23 @@ mod test {
}
+#[cfg(test)]
+mod high_level_fn_test {
+ use super::*;
+ use toml::from_str as toml_from_str;
+
+ #[test]
+ fn test_read_table_value() {
+ let toml : Value = toml_from_str(r#"
+ [table]
+ a = 1
+ "#).unwrap();
+
+ let val = toml.read_int(&String::from("table.a"));
+
+ assert!(val.is_ok());
+ assert_eq!(val.unwrap(), 1);
+ }
+
+}
+
diff --git a/src/util.rs b/src/util.rs
new file mode 100644
index 0000000..5c54806
--- /dev/null
+++ b/src/util.rs
@@ -0,0 +1,14 @@
+use toml::Value;
+
+pub fn name_of_val(val: &Value) -> &'static str {
+ match *val {
+ Value::Array(_) => "Array",
+ Value::Boolean(_) => "Boolean",
+ Value::Datetime(_) => "Datetime",
+ Value::Float(_) => "Float",
+ Value::Integer(_) => "Integer",
+ Value::String(_) => "String",
+ Value::Table(_) => "Table",
+ }
+}
+