Several small idiomatic changes to the edn crate, r=rnewman,jsantell

This commit is contained in:
Victor Porof 2017-02-02 11:00:35 +01:00
commit f3f353661f
2 changed files with 53 additions and 22 deletions

View file

@ -216,16 +216,44 @@ fn to_ord(value: &Value) -> i32 {
pub struct Pair(Value, Value); pub struct Pair(Value, Value);
/// Converts `name` into a plain or namespaced value symbol, depending on
/// whether or not `namespace` is given.
///
/// # Examples
///
/// ```
/// # use edn::types::to_symbol;
/// # use edn::types::Value;
/// # use edn::symbols;
/// let value = to_symbol("foo", "bar");
/// assert_eq!(value, Value::NamespacedSymbol(symbols::NamespacedSymbol::new("foo", "bar")));
///
/// let value = to_symbol(None, "baz");
/// assert_eq!(value, Value::PlainSymbol(symbols::PlainSymbol::new("baz")));
/// ```
pub fn to_symbol<'a, T: Into<Option<&'a str>>>(namespace: T, name: &str) -> Value { pub fn to_symbol<'a, T: Into<Option<&'a str>>>(namespace: T, name: &str) -> Value {
if let Some(ns) = namespace.into() { namespace.into().map_or_else(
return Value::NamespacedSymbol(symbols::NamespacedSymbol::new(ns, name)); || Value::PlainSymbol(symbols::PlainSymbol::new(name)),
} |ns| Value::NamespacedSymbol(symbols::NamespacedSymbol::new(ns, name)))
return Value::PlainSymbol(symbols::PlainSymbol::new(name));
} }
/// Converts `name` into a plain or namespaced value keyword, depending on
/// whether or not `namespace` is given.
///
/// # Examples
///
/// ```
/// # use edn::types::to_keyword;
/// # use edn::types::Value;
/// # use edn::symbols;
/// let value = to_keyword("foo", "bar");
/// assert_eq!(value, Value::NamespacedKeyword(symbols::NamespacedKeyword::new("foo", "bar")));
///
/// let value = to_keyword(None, "baz");
/// assert_eq!(value, Value::Keyword(symbols::Keyword::new("baz")));
/// ```
pub fn to_keyword<'a, T: Into<Option<&'a str>>>(namespace: T, name: &str) -> Value { pub fn to_keyword<'a, T: Into<Option<&'a str>>>(namespace: T, name: &str) -> Value {
if let Some(ns) = namespace.into() { namespace.into().map_or_else(
return Value::NamespacedKeyword(symbols::NamespacedKeyword::new(ns, name)); || Value::Keyword(symbols::Keyword::new(name)),
} |ns| Value::NamespacedKeyword(symbols::NamespacedKeyword::new(ns, name)))
return Value::Keyword(symbols::Keyword::new(name));
} }

View file

@ -25,22 +25,22 @@ use edn::utils;
// Helper for making wrapped keywords with a namespace. // Helper for making wrapped keywords with a namespace.
fn k_ns(ns: &str, name: &str) -> Value { fn k_ns(ns: &str, name: &str) -> Value {
return NamespacedKeyword(symbols::NamespacedKeyword::new(ns, name)); NamespacedKeyword(symbols::NamespacedKeyword::new(ns, name))
} }
// Helper for making wrapped keywords without a namespace. // Helper for making wrapped keywords without a namespace.
fn k_plain(name: &str) -> Value { fn k_plain(name: &str) -> Value {
return Keyword(symbols::Keyword::new(name)); Keyword(symbols::Keyword::new(name))
} }
// Helper for making wrapped symbols with a namespace // Helper for making wrapped symbols with a namespace
fn s_ns(ns: &str, name: &str) -> Value { fn s_ns(ns: &str, name: &str) -> Value {
return NamespacedSymbol(symbols::NamespacedSymbol::new(ns, name)); NamespacedSymbol(symbols::NamespacedSymbol::new(ns, name))
} }
// Helper for making wrapped symbols without a namespace // Helper for making wrapped symbols without a namespace
fn s_plain(name: &str) -> Value { fn s_plain(name: &str) -> Value {
return PlainSymbol(symbols::PlainSymbol::new(name)); PlainSymbol(symbols::PlainSymbol::new(name))
} }
#[test] #[test]
@ -374,10 +374,10 @@ fn test_map() {
assert!(map("#{1 #{2 nil} \"hi\"").is_err()); assert!(map("#{1 #{2 nil} \"hi\"").is_err());
} }
/// The test_query_* functions contain the queries taken from the old Clojure implementation of Mentat. /// The `test_query_*` functions contain the queries taken from the old Clojure implementation of Mentat.
/// 2 changes have been applied, which should be checked and maybe fixed /// 2 changes have been applied, which should be checked and maybe fixed
/// TODO: Decide if these queries should be placed in a vector wrapper. Is that implied? /// TODO: Decide if these queries should be placed in a vector wrapper. Is that implied?
/// Secondly, see note in test_query_starred_pages on the use of ' /// Secondly, see note in `test_query_starred_pages` on the use of '
#[test] #[test]
fn test_query_active_sessions() { fn test_query_active_sessions() {
let test = "[ let test = "[
@ -926,9 +926,7 @@ fn test_is_and_as_type_helper_functions() {
])), ])),
]; ];
for i in 0..values.len() { for (i, value) in values.iter().enumerate() {
let value = &values[i];
let is_result = [ let is_result = [
value.is_nil(), value.is_nil(),
value.is_boolean(), value.is_boolean(),
@ -946,12 +944,17 @@ fn test_is_and_as_type_helper_functions() {
value.is_map(), value.is_map(),
]; ];
for j in 0..values.len() { assert_eq!(values.len(), is_result.len());
assert_eq!(j == i, is_result[j]);
for (j, result) in is_result.iter().enumerate() {
assert_eq!(j == i, *result);
} }
if i == 0 { assert_eq!(value.as_nil().unwrap(), ()) } if i == 0 {
else { assert!(!value.as_nil().is_some()) } assert_eq!(value.as_nil().unwrap(), ())
} else {
assert!(!value.as_nil().is_some())
}
def_test_as_type!(value, as_boolean, i == 1, false); def_test_as_type!(value, as_boolean, i == 1, false);
def_test_as_type!(value, as_integer, i == 2, 1i64); def_test_as_type!(value, as_integer, i == 2, 1i64);