From f807b16db1869a05f25d61a5e8256712d093d188 Mon Sep 17 00:00:00 2001 From: Nick Alexander Date: Tue, 28 Mar 2017 14:10:15 -0700 Subject: [PATCH] Pre: Make it easier to work with ValueAndSpan. with_spans() is a temporary hack, needed only because I don't care to parse the bootstrap assertions from text right now. --- edn/src/types.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/edn/src/types.rs b/edn/src/types.rs index 1e2267c6..6c49b177 100644 --- a/edn/src/types.rs +++ b/edn/src/types.rs @@ -77,6 +77,37 @@ pub struct ValueAndSpan { pub span: Span, } +impl ValueAndSpan { + pub fn new(spanned_value: SpannedValue, span: I) -> ValueAndSpan where I: Into> { + ValueAndSpan { + inner: spanned_value, + span: span.into().unwrap_or(Span(0, 0)), // Think about this. + } + } + + pub fn into_atom(self) -> Option { + if self.inner.is_atom() { + Some(self) + } else { + None + } + } + + pub fn into_text(self) -> Option { + self.inner.into_text() + } +} + +impl Value { + pub fn with_spans(self) -> ValueAndSpan { + let s = self.to_pretty(120).unwrap(); + use ::parse; + let with_spans = parse::value(&s).unwrap(); + assert_eq!(self, with_spans.clone().without_spans()); + with_spans + } +} + impl From for Value { fn from(src: SpannedValue) -> Value { match src {