diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-02-03 12:22:02 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2020-02-03 12:22:02 +0100 |
| commit | 3150fd56437ecf8b2a5902c18e3f9ace800b768c (patch) | |
| tree | db8a7e9fc868145804db97da81bd0669aaf55454 /src/syntax/span.rs | |
| parent | 40ea35cbe7482ce04096c4d63a848c8601cc1848 (diff) | |
Better Debug/Display and Derives 🧽
Diffstat (limited to 'src/syntax/span.rs')
| -rw-r--r-- | src/syntax/span.rs | 47 |
1 files changed, 14 insertions, 33 deletions
diff --git a/src/syntax/span.rs b/src/syntax/span.rs index ad1358cf..7a051d99 100644 --- a/src/syntax/span.rs +++ b/src/syntax/span.rs @@ -1,6 +1,6 @@ //! Spans map elements to the part of source code they originate from. -use std::fmt::{self, Debug, Display, Formatter}; +use std::fmt::{self, Debug, Formatter}; use std::ops::{Add, Sub}; use serde::Serialize; @@ -60,6 +60,12 @@ impl Sub for Position { } } +impl Debug for Position { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + write!(f, "{}:{}", self.line, self.column) + } +} + /// Locates a slice of source code. #[derive(Copy, Clone, Eq, PartialEq, Hash, Serialize)] pub struct Span { @@ -103,8 +109,14 @@ impl Span { } } +impl Debug for Span { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + write!(f, "({:?} -> {:?})", self.start, self.end) + } +} + /// A value with the span it corresponds to in the source code. -#[derive(Copy, Clone, Eq, PartialEq, Hash, Serialize)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Serialize)] pub struct Spanned<T> { /// The value. pub v: T, @@ -143,34 +155,3 @@ pub type SpanVec<T> = Vec<Spanned<T>>; pub fn offset_spans<T>(vec: SpanVec<T>, start: Position) -> impl Iterator<Item=Spanned<T>> { vec.into_iter().map(move |s| s.map_span(|span| span.offset(start))) } - -impl Display for Position { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "{}:{}", self.line, self.column) - } -} - -impl Display for Span { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "({}, {})", self.start, self.end) - } -} - -impl<T> Display for Spanned<T> where T: std::fmt::Display { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "({}, {}, ", self.span.start, self.span.end)?; - self.v.fmt(f)?; - write!(f, ")") - } -} - -impl<T> Debug for Spanned<T> where T: std::fmt::Debug { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "({}, {}, ", self.span.start, self.span.end)?; - self.v.fmt(f)?; - write!(f, ")") - } -} - -debug_display!(Position); -debug_display!(Span); |
