Partly implement Display for edn::Value.
This commit is contained in:
parent
2e203e151f
commit
a1230db0ae
2 changed files with 64 additions and 16 deletions
|
@ -8,6 +8,8 @@
|
||||||
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||||
// specific language governing permissions and limitations under the License.
|
// specific language governing permissions and limitations under the License.
|
||||||
|
|
||||||
|
use std::fmt::{Display, Formatter};
|
||||||
|
|
||||||
/// A simplification of Clojure's Symbol.
|
/// A simplification of Clojure's Symbol.
|
||||||
#[derive(Clone,Debug,Eq,Hash,Ord,PartialOrd,PartialEq)]
|
#[derive(Clone,Debug,Eq,Hash,Ord,PartialOrd,PartialEq)]
|
||||||
pub struct PlainSymbol(pub String);
|
pub struct PlainSymbol(pub String);
|
||||||
|
@ -73,7 +75,7 @@ impl PlainSymbol {
|
||||||
let n = name.into();
|
let n = name.into();
|
||||||
assert!(!n.is_empty(), "Symbols cannot be unnamed.");
|
assert!(!n.is_empty(), "Symbols cannot be unnamed.");
|
||||||
|
|
||||||
return PlainSymbol(n);
|
PlainSymbol(n)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,7 +84,7 @@ impl NamespacedSymbol {
|
||||||
assert!(!name.is_empty(), "Symbols cannot be unnamed.");
|
assert!(!name.is_empty(), "Symbols cannot be unnamed.");
|
||||||
assert!(!namespace.is_empty(), "Symbols cannot have an empty non-null namespace.");
|
assert!(!namespace.is_empty(), "Symbols cannot have an empty non-null namespace.");
|
||||||
|
|
||||||
return NamespacedSymbol { name: name.to_string(), namespace: namespace.to_string() };
|
NamespacedSymbol { name: name.to_string(), namespace: namespace.to_string() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,7 +93,7 @@ impl Keyword {
|
||||||
let n = name.into();
|
let n = name.into();
|
||||||
assert!(!n.is_empty(), "Keywords cannot be unnamed.");
|
assert!(!n.is_empty(), "Keywords cannot be unnamed.");
|
||||||
|
|
||||||
return Keyword(n);
|
Keyword(n)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,7 +103,7 @@ impl NamespacedKeyword {
|
||||||
assert!(!namespace.is_empty(), "Keywords cannot have an empty non-null namespace.");
|
assert!(!namespace.is_empty(), "Keywords cannot have an empty non-null namespace.");
|
||||||
|
|
||||||
// TODO: debug asserts to ensure that neither field matches [ :/].
|
// TODO: debug asserts to ensure that neither field matches [ :/].
|
||||||
return NamespacedKeyword { name: name.to_string(), namespace: namespace.to_string() };
|
NamespacedKeyword { name: name.to_string(), namespace: namespace.to_string() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,7 +111,7 @@ impl NamespacedKeyword {
|
||||||
// Note that we don't currently do any escaping.
|
// Note that we don't currently do any escaping.
|
||||||
//
|
//
|
||||||
|
|
||||||
impl ToString for PlainSymbol {
|
impl Display for PlainSymbol {
|
||||||
/// Print the symbol in EDN format.
|
/// Print the symbol in EDN format.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
|
@ -118,12 +120,12 @@ impl ToString for PlainSymbol {
|
||||||
/// # use edn::symbols::PlainSymbol;
|
/// # use edn::symbols::PlainSymbol;
|
||||||
/// assert_eq!("baz", PlainSymbol::new("baz").to_string());
|
/// assert_eq!("baz", PlainSymbol::new("baz").to_string());
|
||||||
/// ```
|
/// ```
|
||||||
fn to_string(&self) -> String {
|
fn fmt(&self, f: &mut Formatter) -> ::std::fmt::Result {
|
||||||
return format!("{}", self.0);
|
write!(f, "{}", self.0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToString for NamespacedSymbol {
|
impl Display for NamespacedSymbol {
|
||||||
/// Print the symbol in EDN format.
|
/// Print the symbol in EDN format.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
|
@ -132,12 +134,12 @@ impl ToString for NamespacedSymbol {
|
||||||
/// # use edn::symbols::NamespacedSymbol;
|
/// # use edn::symbols::NamespacedSymbol;
|
||||||
/// assert_eq!("bar/baz", NamespacedSymbol::new("bar", "baz").to_string());
|
/// assert_eq!("bar/baz", NamespacedSymbol::new("bar", "baz").to_string());
|
||||||
/// ```
|
/// ```
|
||||||
fn to_string(&self) -> String {
|
fn fmt(&self, f: &mut Formatter) -> ::std::fmt::Result {
|
||||||
return format!("{}/{}", self.namespace, self.name);
|
write!(f, "{}/{}", self.namespace, self.name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToString for Keyword {
|
impl Display for Keyword {
|
||||||
/// Print the keyword in EDN format.
|
/// Print the keyword in EDN format.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
|
@ -146,12 +148,12 @@ impl ToString for Keyword {
|
||||||
/// # use edn::symbols::Keyword;
|
/// # use edn::symbols::Keyword;
|
||||||
/// assert_eq!(":baz", Keyword::new("baz").to_string());
|
/// assert_eq!(":baz", Keyword::new("baz").to_string());
|
||||||
/// ```
|
/// ```
|
||||||
fn to_string(&self) -> String {
|
fn fmt(&self, f: &mut Formatter) -> ::std::fmt::Result {
|
||||||
return format!(":{}", self.0);
|
write!(f, ":{}", self.0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToString for NamespacedKeyword {
|
impl Display for NamespacedKeyword {
|
||||||
/// Print the keyword in EDN format.
|
/// Print the keyword in EDN format.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
|
@ -160,7 +162,7 @@ impl ToString for NamespacedKeyword {
|
||||||
/// # use edn::symbols::NamespacedKeyword;
|
/// # use edn::symbols::NamespacedKeyword;
|
||||||
/// assert_eq!(":bar/baz", NamespacedKeyword::new("bar", "baz").to_string());
|
/// assert_eq!(":bar/baz", NamespacedKeyword::new("bar", "baz").to_string());
|
||||||
/// ```
|
/// ```
|
||||||
fn to_string(&self) -> String {
|
fn fmt(&self, f: &mut Formatter) -> ::std::fmt::Result {
|
||||||
return format!(":{}/{}", self.namespace, self.name);
|
write!(f, ":{}/{}", self.namespace, self.name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
use std::collections::{BTreeSet, BTreeMap, LinkedList};
|
use std::collections::{BTreeSet, BTreeMap, LinkedList};
|
||||||
use std::cmp::{Ordering, Ord, PartialOrd};
|
use std::cmp::{Ordering, Ord, PartialOrd};
|
||||||
|
use std::fmt::{Display, Formatter};
|
||||||
|
|
||||||
use symbols;
|
use symbols;
|
||||||
use num::BigInt;
|
use num::BigInt;
|
||||||
|
@ -41,6 +42,51 @@ pub enum Value {
|
||||||
|
|
||||||
use self::Value::*;
|
use self::Value::*;
|
||||||
|
|
||||||
|
impl Display for Value {
|
||||||
|
fn fmt(&self, f: &mut Formatter) -> ::std::fmt::Result {
|
||||||
|
match *self {
|
||||||
|
Keyword(ref v) => v.fmt(f),
|
||||||
|
NamespacedKeyword(ref v) => v.fmt(f),
|
||||||
|
PlainSymbol(ref v) => v.fmt(f),
|
||||||
|
NamespacedSymbol(ref v) => v.fmt(f),
|
||||||
|
|
||||||
|
Integer(v) => write!(f, "{}", v),
|
||||||
|
BigInteger(ref v) => write!(f, "{}N", v),
|
||||||
|
Float(OrderedFloat(v)) => write!(f, "{}", v), // TODO: make sure float syntax is correct.
|
||||||
|
// TODO: NaN.
|
||||||
|
Text(ref v) => write!(f, "{}", v), // TODO: EDN escaping.
|
||||||
|
Vector(ref v) => {
|
||||||
|
try!(write!(f, "["));
|
||||||
|
for x in v {
|
||||||
|
try!(write!(f, " {}", x));
|
||||||
|
}
|
||||||
|
write!(f, " ]")
|
||||||
|
}
|
||||||
|
_ =>
|
||||||
|
write!(f, "{}",
|
||||||
|
match *self {
|
||||||
|
Nil => "null",
|
||||||
|
Boolean(b) => if b { "true" } else { "false" },
|
||||||
|
_ => unimplemented!(),
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_print_edn() {
|
||||||
|
assert_eq!("[ 1 2 [ 3.1 ] [ ] :five :six/seven eight nine/ten true ]",
|
||||||
|
Value::Vector(vec!(Value::Integer(1),
|
||||||
|
Value::Integer(2),
|
||||||
|
Value::Vector(vec!(Value::Float(OrderedFloat(3.1)))),
|
||||||
|
Value::Vector(vec!()),
|
||||||
|
Value::Keyword(symbols::Keyword::new("five")),
|
||||||
|
Value::NamespacedKeyword(symbols::NamespacedKeyword::new("six", "seven")),
|
||||||
|
Value::PlainSymbol(symbols::PlainSymbol::new("eight")),
|
||||||
|
Value::NamespacedSymbol(symbols::NamespacedSymbol::new("nine", "ten")),
|
||||||
|
Value::Boolean(true))).to_string());
|
||||||
|
}
|
||||||
|
|
||||||
impl Value {
|
impl Value {
|
||||||
pub fn is_keyword(&self) -> bool {
|
pub fn is_keyword(&self) -> bool {
|
||||||
match *self {
|
match *self {
|
||||||
|
|
Loading…
Reference in a new issue