Skip to content

Commit c3bd0c3

Browse files
committed
fix!: Use explicit Range<usize>
BREAKING CHANGE: This changes (usize, usize) into Range<usize>
1 parent b65b8ca commit c3bd0c3

File tree

8 files changed

+35
-28
lines changed

8 files changed

+35
-28
lines changed

benches/simple.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ fn create_snippet(renderer: Renderer) {
3838
SourceAnnotation {
3939
label: "expected `Option<String>` because of return type",
4040
annotation_type: AnnotationType::Warning,
41-
range: (5, 19),
41+
range: 5..19,
4242
},
4343
SourceAnnotation {
4444
label: "expected enum `std::option::Option`",
4545
annotation_type: AnnotationType::Error,
46-
range: (26, 724),
46+
range: 26..724,
4747
},
4848
],
4949
}],

examples/expected_type.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ fn main() {
2020
SourceAnnotation {
2121
label: "",
2222
annotation_type: AnnotationType::Error,
23-
range: (193, 195),
23+
range: 193..195,
2424
},
2525
SourceAnnotation {
2626
label: "while parsing this struct",
2727
annotation_type: AnnotationType::Info,
28-
range: (34, 50),
28+
range: 34..50,
2929
},
3030
],
3131
}],

examples/footer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn main() {
2121
fold: false,
2222
annotations: vec![SourceAnnotation {
2323
label: "expected struct `annotate_snippets::snippet::Slice`, found reference",
24-
range: (21, 24),
24+
range: 21..24,
2525
annotation_type: AnnotationType::Error,
2626
}],
2727
}],

examples/format.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ fn main() {
3232
SourceAnnotation {
3333
label: "expected `Option<String>` because of return type",
3434
annotation_type: AnnotationType::Warning,
35-
range: (5, 19),
35+
range: 5..19,
3636
},
3737
SourceAnnotation {
3838
label: "expected enum `std::option::Option`",
3939
annotation_type: AnnotationType::Error,
40-
range: (26, 724),
40+
range: 26..724,
4141
},
4242
],
4343
}],

src/renderer/display_list.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use crate::snippet;
3535
use itertools::FoldWhile::{Continue, Done};
3636
use itertools::Itertools;
3737
use std::fmt::{Display, Write};
38+
use std::ops::Range;
3839
use std::{cmp, fmt};
3940

4041
use crate::renderer::{stylesheet::Stylesheet, Margin, Style};
@@ -768,7 +769,7 @@ fn format_slice(
768769
has_footer: bool,
769770
margin: Option<Margin>,
770771
) -> Vec<DisplayLine<'_>> {
771-
let main_range = slice.annotations.first().map(|x| x.range.0);
772+
let main_range = slice.annotations.first().map(|x| x.range.start);
772773
let origin = slice.origin;
773774
let need_empty_header = origin.is_some() || is_first;
774775
let mut body = format_body(slice, need_empty_header, has_footer, margin);
@@ -951,8 +952,8 @@ fn format_body(
951952
let source_len = slice.source.len();
952953
if let Some(bigger) = slice.annotations.iter().find_map(|x| {
953954
// Allow highlighting one past the last character in the source.
954-
if source_len + 1 < x.range.1 {
955-
Some(x.range)
955+
if source_len + 1 < x.range.end {
956+
Some(&x.range)
956957
} else {
957958
None
958959
}
@@ -1017,8 +1018,8 @@ fn format_body(
10171018
_ => DisplayAnnotationType::from(annotation.annotation_type),
10181019
};
10191020
match annotation.range {
1020-
(start, _) if start > line_end_index => true,
1021-
(start, end)
1021+
Range { start, .. } if start > line_end_index => true,
1022+
Range { start, end }
10221023
if start >= line_start_index && end <= line_end_index
10231024
|| start == line_end_index && end - start <= 1 =>
10241025
{
@@ -1047,7 +1048,7 @@ fn format_body(
10471048
annotation_line_count += 1;
10481049
false
10491050
}
1050-
(start, end)
1051+
Range { start, end }
10511052
if start >= line_start_index
10521053
&& start <= line_end_index
10531054
&& end > line_end_index =>
@@ -1091,7 +1092,7 @@ fn format_body(
10911092
}
10921093
true
10931094
}
1094-
(start, end) if start < line_start_index && end > line_end_index => {
1095+
Range { start, end } if start < line_start_index && end > line_end_index => {
10951096
if let DisplayLine::Source {
10961097
ref mut inline_marks,
10971098
..
@@ -1106,7 +1107,7 @@ fn format_body(
11061107
}
11071108
true
11081109
}
1109-
(start, end)
1110+
Range { start, end }
11101111
if start < line_start_index
11111112
&& end >= line_start_index
11121113
&& end <= line_end_index =>
@@ -1375,7 +1376,7 @@ mod tests {
13751376
let line_2 = "This is line 2";
13761377
let source = [line_1, line_2].join("\n");
13771378
// In line 2
1378-
let range = (22, 24);
1379+
let range = 22..24;
13791380
let input = snippet::Snippet {
13801381
title: None,
13811382
footer: vec![],
@@ -1384,7 +1385,7 @@ mod tests {
13841385
line_start: 5402,
13851386
origin: None,
13861387
annotations: vec![snippet::SourceAnnotation {
1387-
range,
1388+
range: range.clone(),
13881389
label: "Test annotation",
13891390
annotation_type: snippet::AnnotationType::Info,
13901391
}],
@@ -1425,7 +1426,10 @@ mod tests {
14251426
style: DisplayTextStyle::Regular,
14261427
}],
14271428
},
1428-
range: (range.0 - (line_1.len() + 1), range.1 - (line_1.len() + 1)),
1429+
range: (
1430+
range.start - (line_1.len() + 1),
1431+
range.end - (line_1.len() + 1),
1432+
),
14291433
annotation_type: DisplayAnnotationType::Info,
14301434
annotation_part: DisplayAnnotationPart::Standalone,
14311435
},
@@ -1475,7 +1479,7 @@ mod tests {
14751479
footer: vec![],
14761480
slices: vec![snippet::Slice {
14771481
annotations: vec![snippet::SourceAnnotation {
1478-
range: (0, source.len() + 2),
1482+
range: 0..source.len() + 2,
14791483
label,
14801484
annotation_type: snippet::AnnotationType::Error,
14811485
}],
@@ -1502,7 +1506,7 @@ mod tests {
15021506
line_start: 1,
15031507
origin: Some("<current file>"),
15041508
annotations: vec![snippet::SourceAnnotation {
1505-
range: (19, 23),
1509+
range: 19..23,
15061510
label: "oops",
15071511
annotation_type: snippet::AnnotationType::Error,
15081512
}],

src/snippet.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
//! };
3232
//! ```
3333
34+
use std::ops::Range;
35+
3436
/// Primary structure provided for formatting
3537
#[derive(Debug, Default)]
3638
pub struct Snippet<'a> {
@@ -71,7 +73,7 @@ pub enum AnnotationType {
7173
#[derive(Debug)]
7274
pub struct SourceAnnotation<'a> {
7375
/// The byte range of the annotation in the `source` string
74-
pub range: (usize, usize),
76+
pub range: Range<usize>,
7577
pub label: &'a str,
7678
pub annotation_type: AnnotationType,
7779
}

tests/fixtures/deserialize.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use serde::{Deserialize, Deserializer, Serialize};
2+
use std::ops::Range;
23

34
use annotate_snippets::{
45
renderer::Margin, Annotation, AnnotationType, Renderer, Slice, Snippet, SourceAnnotation,
@@ -122,7 +123,7 @@ where
122123
#[derive(Serialize, Deserialize)]
123124
#[serde(remote = "SourceAnnotation")]
124125
pub struct SourceAnnotationDef<'a> {
125-
pub range: (usize, usize),
126+
pub range: Range<usize>,
126127
#[serde(borrow)]
127128
pub label: &'a str,
128129
#[serde(with = "AnnotationTypeDef")]

tests/formatter.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn test_i_29() {
1414
line_start: 1,
1515
origin: Some("<current file>"),
1616
annotations: vec![SourceAnnotation {
17-
range: (19, 23),
17+
range: 19..23,
1818
label: "oops",
1919
annotation_type: AnnotationType::Error,
2020
}],
@@ -41,7 +41,7 @@ fn test_point_to_double_width_characters() {
4141
line_start: 1,
4242
origin: Some("<current file>"),
4343
annotations: vec![SourceAnnotation {
44-
range: (12, 16),
44+
range: 12..16,
4545
label: "world",
4646
annotation_type: AnnotationType::Error,
4747
}],
@@ -69,7 +69,7 @@ fn test_point_to_double_width_characters_across_lines() {
6969
line_start: 1,
7070
origin: Some("<current file>"),
7171
annotations: vec![SourceAnnotation {
72-
range: (4, 15),
72+
range: 4..15,
7373
label: "Good morning",
7474
annotation_type: AnnotationType::Error,
7575
}],
@@ -100,12 +100,12 @@ fn test_point_to_double_width_characters_multiple() {
100100
origin: Some("<current file>"),
101101
annotations: vec![
102102
SourceAnnotation {
103-
range: (0, 6),
103+
range: 0..6,
104104
label: "Sushi1",
105105
annotation_type: AnnotationType::Error,
106106
},
107107
SourceAnnotation {
108-
range: (11, 15),
108+
range: 11..15,
109109
label: "Sushi2",
110110
annotation_type: AnnotationType::Note,
111111
},
@@ -136,7 +136,7 @@ fn test_point_to_double_width_characters_mixed() {
136136
line_start: 1,
137137
origin: Some("<current file>"),
138138
annotations: vec![SourceAnnotation {
139-
range: (12, 23),
139+
range: 12..23,
140140
label: "New world",
141141
annotation_type: AnnotationType::Error,
142142
}],

0 commit comments

Comments
 (0)