Skip to content

Commit 189c5f3

Browse files
committed
Give WithLineNumber more powers
1 parent 40be8d4 commit 189c5f3

File tree

1 file changed

+63
-15
lines changed

1 file changed

+63
-15
lines changed

src/span.rs

+63-15
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub struct WithLineNumber<T> {
2929
pub data: T,
3030
}
3131

32-
impl Span for WithLineNumber<&str> {
32+
impl Span for &str {
3333
/// Byte index range into this string.
3434
type Subspan = Range<usize>;
3535
/// Byte index into this string.
@@ -40,63 +40,111 @@ impl Span for WithLineNumber<&str> {
4040
}
4141

4242
fn end(&self) -> Self::Pos {
43-
self.data.len()
43+
self.len()
4444
}
4545

4646
fn slice(&self, range: Range<Self::Pos>) -> Range<usize> {
4747
range
4848
}
4949
}
5050

51-
impl SpanFormatter<WithLineNumber<&str>> for () {
52-
fn first_line(&self, span: &WithLineNumber<&str>) -> WithLineNumber<Range<usize>> {
51+
impl SpanFormatter<&str> for () {
52+
fn first_line(&self, span: &&str) -> WithLineNumber<Range<usize>> {
5353
let start = 0;
5454
let end = span
55-
.data
5655
.as_bytes()
5756
.iter()
5857
.enumerate()
5958
.find(|(_, &b)| b == b'\n')
60-
.map_or_else(|| span.data.len(), |(i, _)| i);
59+
.map_or_else(|| span.len(), |(i, _)| i);
6160
WithLineNumber {
6261
data: start..end,
63-
line_num: span.line_num,
62+
line_num: 1,
6463
}
6564
}
6665

6766
fn next_line(
6867
&self,
69-
span: &WithLineNumber<&str>,
68+
span: &&str,
7069
subspan: &WithLineNumber<Range<usize>>,
7170
) -> Option<WithLineNumber<Range<usize>>> {
7271
let start = subspan.data.end + 1;
7372
let end = span
74-
.data
7573
.get(start..)?
7674
.as_bytes()
7775
.iter()
7876
.enumerate()
7977
.find(|(_, &b)| b == b'\n')
80-
.map_or_else(|| span.data.len(), |(i, _)| i + start);
78+
.map_or_else(|| span.len(), |(i, _)| i + start);
8179
Some(WithLineNumber {
8280
data: start..end,
8381
line_num: subspan.line_num + 1,
8482
})
8583
}
8684

87-
fn count_columns(&self, span: &WithLineNumber<&str>, subspan: &Range<usize>) -> usize {
88-
span.data[subspan.start..subspan.end].chars().count()
85+
fn count_columns(&self, span: &&str, subspan: &Range<usize>) -> usize {
86+
span[subspan.start..subspan.end].chars().count()
8987
}
9088
}
9189

92-
impl SpanWriter<WithLineNumber<&str>> for () {
90+
impl SpanWriter<&str> for () {
9391
fn write(
9492
&self,
9593
w: &mut dyn io::Write,
96-
span: &WithLineNumber<&str>,
94+
span: &&str,
9795
subspan: &Range<usize>,
9896
) -> io::Result<()> {
99-
w.write_all(span.data[subspan.start..subspan.end].as_bytes())
97+
w.write_all(span[subspan.start..subspan.end].as_bytes())
98+
}
99+
}
100+
101+
impl<S: Span> Span for WithLineNumber<S> {
102+
type Subspan = S::Subspan;
103+
type Pos = S::Pos;
104+
105+
fn start(&self) -> S::Pos {
106+
self.data.start()
107+
}
108+
109+
fn end(&self) -> S::Pos {
110+
self.data.end()
111+
}
112+
113+
fn slice(&self, range: Range<S::Pos>) -> S::Subspan {
114+
self.data.slice(range)
115+
}
116+
}
117+
118+
impl<S: Span, SF: SpanFormatter<S>> SpanFormatter<WithLineNumber<S>> for SF {
119+
fn first_line(&self, span: &WithLineNumber<S>) -> WithLineNumber<S::Subspan> {
120+
let wln = self.first_line(&span.data);
121+
WithLineNumber {
122+
data: wln.data,
123+
line_num: wln.line_num + span.line_num - 1,
124+
}
125+
}
126+
127+
fn next_line(
128+
&self,
129+
span: &WithLineNumber<S>,
130+
subspan: &WithLineNumber<S::Subspan>,
131+
) -> Option<WithLineNumber<S::Subspan>> {
132+
self.next_line(&span.data, subspan)
133+
}
134+
135+
fn count_columns(&self, span: &WithLineNumber<S>, subspan: &S::Subspan) -> usize {
136+
self.count_columns(&span.data, subspan)
137+
}
138+
}
139+
140+
impl<S: Span, SW: SpanWriter<S>> SpanWriter<WithLineNumber<S>> for SW {
141+
fn write(
142+
&self,
143+
w: &mut dyn io::Write,
144+
span: &WithLineNumber<S>,
145+
subspan: &S::Subspan,
146+
) -> io::Result<()> {
147+
self.write(w, &span.data, subspan)
100148
}
101149
}
102150

0 commit comments

Comments
 (0)