Rudimentary printing of EDN values. (#209) r=jsantell
* Add a little From helper for edn::parse::ParseError. Not used yet. * Ignore more things. * Partly implement Display for edn::Value.
This commit is contained in:
parent
506c83c160
commit
c6fa14c0c8
4 changed files with 72 additions and 17 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -3,6 +3,7 @@
|
|||
*.jar
|
||||
*jar
|
||||
*~
|
||||
*.rs.bk
|
||||
.s*
|
||||
.*.sw*
|
||||
*.rs.bak
|
||||
|
@ -49,7 +50,7 @@ pom.xml.asc
|
|||
/release-node/datomish/
|
||||
/release-node/goog/
|
||||
/release-node/honeysql/
|
||||
|
||||
/edn/target/
|
||||
/fixtures/*.db-shm
|
||||
/fixtures/*.db-wal
|
||||
/query-parser/out/
|
||||
|
|
|
@ -8,6 +8,8 @@
|
|||
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations under the License.
|
||||
|
||||
use std::fmt::{Display, Formatter};
|
||||
|
||||
/// A simplification of Clojure's Symbol.
|
||||
#[derive(Clone,Debug,Eq,Hash,Ord,PartialOrd,PartialEq)]
|
||||
pub struct PlainSymbol(pub String);
|
||||
|
@ -73,7 +75,7 @@ impl PlainSymbol {
|
|||
let n = name.into();
|
||||
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!(!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();
|
||||
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.");
|
||||
|
||||
// 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.
|
||||
//
|
||||
|
||||
impl ToString for PlainSymbol {
|
||||
impl Display for PlainSymbol {
|
||||
/// Print the symbol in EDN format.
|
||||
///
|
||||
/// # Examples
|
||||
|
@ -118,12 +120,12 @@ impl ToString for PlainSymbol {
|
|||
/// # use edn::symbols::PlainSymbol;
|
||||
/// assert_eq!("baz", PlainSymbol::new("baz").to_string());
|
||||
/// ```
|
||||
fn to_string(&self) -> String {
|
||||
return format!("{}", self.0);
|
||||
fn fmt(&self, f: &mut Formatter) -> ::std::fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToString for NamespacedSymbol {
|
||||
impl Display for NamespacedSymbol {
|
||||
/// Print the symbol in EDN format.
|
||||
///
|
||||
/// # Examples
|
||||
|
@ -132,12 +134,12 @@ impl ToString for NamespacedSymbol {
|
|||
/// # use edn::symbols::NamespacedSymbol;
|
||||
/// assert_eq!("bar/baz", NamespacedSymbol::new("bar", "baz").to_string());
|
||||
/// ```
|
||||
fn to_string(&self) -> String {
|
||||
return format!("{}/{}", self.namespace, self.name);
|
||||
fn fmt(&self, f: &mut Formatter) -> ::std::fmt::Result {
|
||||
write!(f, "{}/{}", self.namespace, self.name)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToString for Keyword {
|
||||
impl Display for Keyword {
|
||||
/// Print the keyword in EDN format.
|
||||
///
|
||||
/// # Examples
|
||||
|
@ -146,12 +148,12 @@ impl ToString for Keyword {
|
|||
/// # use edn::symbols::Keyword;
|
||||
/// assert_eq!(":baz", Keyword::new("baz").to_string());
|
||||
/// ```
|
||||
fn to_string(&self) -> String {
|
||||
return format!(":{}", self.0);
|
||||
fn fmt(&self, f: &mut Formatter) -> ::std::fmt::Result {
|
||||
write!(f, ":{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToString for NamespacedKeyword {
|
||||
impl Display for NamespacedKeyword {
|
||||
/// Print the keyword in EDN format.
|
||||
///
|
||||
/// # Examples
|
||||
|
@ -160,7 +162,7 @@ impl ToString for NamespacedKeyword {
|
|||
/// # use edn::symbols::NamespacedKeyword;
|
||||
/// assert_eq!(":bar/baz", NamespacedKeyword::new("bar", "baz").to_string());
|
||||
/// ```
|
||||
fn to_string(&self) -> String {
|
||||
return format!(":{}/{}", self.namespace, self.name);
|
||||
fn fmt(&self, f: &mut Formatter) -> ::std::fmt::Result {
|
||||
write!(f, ":{}/{}", self.namespace, self.name)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
|
||||
use std::collections::{BTreeSet, BTreeMap, LinkedList};
|
||||
use std::cmp::{Ordering, Ord, PartialOrd};
|
||||
use std::fmt::{Display, Formatter};
|
||||
|
||||
use symbols;
|
||||
use num::BigInt;
|
||||
|
@ -41,6 +42,51 @@ pub enum 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 {
|
||||
pub fn is_keyword(&self) -> bool {
|
||||
match *self {
|
||||
|
|
|
@ -30,6 +30,12 @@ pub enum QueryParseError {
|
|||
FindParseError(FindParseError),
|
||||
}
|
||||
|
||||
impl From<edn::parse::ParseError> for QueryParseError {
|
||||
fn from(err: edn::parse::ParseError) -> QueryParseError {
|
||||
QueryParseError::EdnParseError(err)
|
||||
}
|
||||
}
|
||||
|
||||
pub type FindParseResult = Result<FindSpec, FindParseError>;
|
||||
pub type QueryParseResult = Result<FindQuery, QueryParseError>;
|
||||
|
||||
|
|
Loading…
Reference in a new issue