Skip to content

Commit 16ce754

Browse files
committed
fix!: Rename Slice to Snippet
1 parent abb1bc8 commit 16ce754

File tree

10 files changed

+65
-65
lines changed

10 files changed

+65
-65
lines changed

benches/simple.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ extern crate criterion;
44

55
use criterion::{black_box, Criterion};
66

7-
use annotate_snippets::{Label, Message, Renderer, Slice};
7+
use annotate_snippets::{Label, Message, Renderer, Snippet};
88

99
fn create_snippet(renderer: Renderer) {
1010
let source = r#") -> Option<String> {
@@ -29,8 +29,8 @@ fn create_snippet(renderer: Renderer) {
2929
_ => continue,
3030
}
3131
}"#;
32-
let snippet = Message::error("mismatched types").id("E0308").slice(
33-
Slice::new(source, 51)
32+
let snippet = Message::error("mismatched types").id("E0308").snippet(
33+
Snippet::new(source, 51)
3434
.origin("src/format.rs")
3535
.annotation(
3636
Label::warning("expected `Option<String>` because of return type").span(5..19),

examples/expected_type.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use annotate_snippets::{Label, Message, Renderer, Slice};
1+
use annotate_snippets::{Label, Message, Renderer, Snippet};
22

33
fn main() {
44
let source = r#" annotations: vec![SourceAnnotation {
55
label: "expected struct `annotate_snippets::snippet::Slice`, found reference"
66
,
77
range: <22, 25>,"#;
8-
let snippet = Message::error("expected type, found `22`").slice(
9-
Slice::new(source, 26)
8+
let snippet = Message::error("expected type, found `22`").snippet(
9+
Snippet::new(source, 26)
1010
.origin("examples/footer.rs")
1111
.fold(true)
1212
.annotation(

examples/footer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use annotate_snippets::{Label, Message, Renderer, Slice};
1+
use annotate_snippets::{Label, Message, Renderer, Snippet};
22

33
fn main() {
44
let snippet = Message::error("mismatched types")
55
.id("E0308")
6-
.slice(
7-
Slice::new(" slices: vec![\"A\",", 13)
6+
.snippet(
7+
Snippet::new(" slices: vec![\"A\",", 13)
88
.origin("src/multislice.rs")
99
.annotation(
1010
Label::error(

examples/format.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use annotate_snippets::{Label, Message, Renderer, Slice};
1+
use annotate_snippets::{Label, Message, Renderer, Snippet};
22

33
fn main() {
44
let source = r#") -> Option<String> {
@@ -23,8 +23,8 @@ fn main() {
2323
_ => continue,
2424
}
2525
}"#;
26-
let snippet = Message::error("mismatched types").id("E0308").slice(
27-
Slice::new(source, 51)
26+
let snippet = Message::error("mismatched types").id("E0308").snippet(
27+
Snippet::new(source, 51)
2828
.origin("src/format.rs")
2929
.annotation(
3030
Label::warning("expected `Option<String>` because of return type").span(5..19),

examples/multislice.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use annotate_snippets::{Message, Renderer, Slice};
1+
use annotate_snippets::{Message, Renderer, Snippet};
22

33
fn main() {
44
let snippet = Message::error("mismatched types")
5-
.slice(Slice::new("Foo", 51).origin("src/format.rs"))
6-
.slice(Slice::new("Faa", 129).origin("src/display.rs"));
5+
.snippet(Snippet::new("Foo", 51).origin("src/format.rs"))
6+
.snippet(Snippet::new("Faa", 129).origin("src/display.rs"));
77

88
let renderer = Renderer::styled();
99
anstream::println!("{}", renderer.render(snippet));

src/renderer/display_list.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl<'a> DisplayList<'a> {
110110
title,
111111
id,
112112
footer,
113-
slices,
113+
snippets,
114114
}: snippet::Message<'a>,
115115
stylesheet: &'a Stylesheet,
116116
anonymized_line_numbers: bool,
@@ -120,7 +120,7 @@ impl<'a> DisplayList<'a> {
120120

121121
body.push(format_title(title, id));
122122

123-
for (idx, slice) in slices.into_iter().enumerate() {
123+
for (idx, slice) in snippets.into_iter().enumerate() {
124124
body.append(&mut format_slice(
125125
slice,
126126
idx == 0,
@@ -542,7 +542,7 @@ pub enum DisplayLine<'a> {
542542
/// A source line.
543543
#[derive(Debug, PartialEq)]
544544
pub enum DisplaySourceLine<'a> {
545-
/// A line with the content of the Slice.
545+
/// A line with the content of the Snippet.
546546
Content {
547547
text: &'a str,
548548
range: (usize, usize), // meta information for annotation placement.
@@ -762,7 +762,7 @@ fn format_footer(footer: snippet::Label<'_>) -> Vec<DisplayLine<'_>> {
762762
}
763763

764764
fn format_slice(
765-
slice: snippet::Slice<'_>,
765+
slice: snippet::Snippet<'_>,
766766
is_first: bool,
767767
has_footer: bool,
768768
margin: Option<Margin>,
@@ -942,7 +942,7 @@ fn fold_body(mut body: Vec<DisplayLine<'_>>) -> Vec<DisplayLine<'_>> {
942942
}
943943

944944
fn format_body(
945-
slice: snippet::Slice<'_>,
945+
slice: snippet::Snippet<'_>,
946946
need_empty_header: bool,
947947
has_footer: bool,
948948
margin: Option<Margin>,
@@ -1227,7 +1227,7 @@ mod tests {
12271227
let line_1 = "This is line 1";
12281228
let line_2 = "This is line 2";
12291229
let source = [line_1, line_2].join("\n");
1230-
let input = snippet::Message::error("").slice(snippet::Slice::new(&source, 5402));
1230+
let input = snippet::Message::error("").snippet(snippet::Snippet::new(&source, 5402));
12311231
let output = from_display_lines(vec![
12321232
DisplayLine::Raw(DisplayRawLine::Annotation {
12331233
annotation: Annotation {
@@ -1278,8 +1278,8 @@ mod tests {
12781278
let src_1 = "This is slice 2";
12791279
let src_1_len = src_1.len();
12801280
let input = snippet::Message::error("")
1281-
.slice(snippet::Slice::new(src_0, 5402).origin("file1.rs"))
1282-
.slice(snippet::Slice::new(src_1, 2).origin("file2.rs"));
1281+
.snippet(snippet::Snippet::new(src_0, 5402).origin("file1.rs"))
1282+
.snippet(snippet::Snippet::new(src_1, 2).origin("file2.rs"));
12831283
let output = from_display_lines(vec![
12841284
DisplayLine::Raw(DisplayRawLine::Annotation {
12851285
annotation: Annotation {
@@ -1350,8 +1350,8 @@ mod tests {
13501350
let source = [line_1, line_2].join("\n");
13511351
// In line 2
13521352
let range = 22..24;
1353-
let input = snippet::Message::error("").slice(
1354-
snippet::Slice::new(&source, 5402)
1353+
let input = snippet::Message::error("").snippet(
1354+
snippet::Snippet::new(&source, 5402)
13551355
.annotation(snippet::Label::info("Test annotation").span(range.clone())),
13561356
);
13571357
let output = from_display_lines(vec![
@@ -1455,17 +1455,17 @@ mod tests {
14551455
fn test_i26() {
14561456
let source = "short";
14571457
let label = "label";
1458-
let input = snippet::Message::error("").slice(
1459-
snippet::Slice::new(source, 0)
1458+
let input = snippet::Message::error("").snippet(
1459+
snippet::Snippet::new(source, 0)
14601460
.annotation(snippet::Label::error(label).span(0..source.len() + 2)),
14611461
);
14621462
let _ = DisplayList::new(input, &STYLESHEET, false, None);
14631463
}
14641464

14651465
#[test]
14661466
fn test_i_29() {
1467-
let snippets = snippet::Message::error("oops").slice(
1468-
snippet::Slice::new("First line\r\nSecond oops line", 1)
1467+
let snippets = snippet::Message::error("oops").snippet(
1468+
snippet::Snippet::new("First line\r\nSecond oops line", 1)
14691469
.origin("<current file>")
14701470
.fold(true)
14711471
.annotation(snippet::Label::error("oops").span(19..23)),

src/renderer/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
//!
33
//! # Example
44
//! ```
5-
//! use annotate_snippets::{Renderer, Slice, Message};
5+
//! use annotate_snippets::{Renderer, Snippet, Message};
66
//! let snippet = Message::error("mismatched types")
7-
//! .slice(Slice::new("Foo", 51).origin("src/format.rs"))
8-
//! .slice(Slice::new("Faa", 129).origin("src/display.rs"));
7+
//! .snippet(Snippet::new("Foo", 51).origin("src/format.rs"))
8+
//! .snippet(Snippet::new("Faa", 129).origin("src/display.rs"));
99
//!
1010
//! let renderer = Renderer::styled();
1111
//! println!("{}", renderer.render(snippet));

src/snippet.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
//! use annotate_snippets::*;
77
//!
88
//! Message::error("mismatched types")
9-
//! .slice(Slice::new("Foo", 51).origin("src/format.rs"))
10-
//! .slice(Slice::new("Faa", 129).origin("src/display.rs"));
9+
//! .snippet(Snippet::new("Foo", 51).origin("src/format.rs"))
10+
//! .snippet(Snippet::new("Faa", 129).origin("src/display.rs"));
1111
//! ```
1212
1313
use std::ops::Range;
@@ -16,7 +16,7 @@ use std::ops::Range;
1616
pub struct Message<'a> {
1717
pub(crate) title: Label<'a>,
1818
pub(crate) id: Option<&'a str>,
19-
pub(crate) slices: Vec<Slice<'a>>,
19+
pub(crate) snippets: Vec<Snippet<'a>>,
2020
pub(crate) footer: Vec<Label<'a>>,
2121
}
2222

@@ -25,7 +25,7 @@ impl<'a> Message<'a> {
2525
Self {
2626
title,
2727
id: None,
28-
slices: vec![],
28+
snippets: vec![],
2929
footer: vec![],
3030
}
3131
}
@@ -55,8 +55,8 @@ impl<'a> Message<'a> {
5555
self
5656
}
5757

58-
pub fn slice(mut self, slice: Slice<'a>) -> Self {
59-
self.slices.push(slice);
58+
pub fn snippet(mut self, slice: Snippet<'a>) -> Self {
59+
self.snippets.push(slice);
6060
self
6161
}
6262

@@ -100,7 +100,7 @@ impl<'a> Label<'a> {
100100
self
101101
}
102102

103-
/// Create a [`Annotation`] with the given span for a [`Slice`]
103+
/// Create a [`Annotation`] with the given span for a [`Snippet`]
104104
pub fn span(&self, span: Range<usize>) -> Annotation<'a> {
105105
Annotation {
106106
range: span,
@@ -113,17 +113,17 @@ impl<'a> Label<'a> {
113113
/// Structure containing the slice of text to be annotated and
114114
/// basic information about the location of the slice.
115115
///
116-
/// One `Slice` is meant to represent a single, continuous,
116+
/// One `Snippet` is meant to represent a single, continuous,
117117
/// slice of source code that you want to annotate.
118-
pub struct Slice<'a> {
118+
pub struct Snippet<'a> {
119119
pub(crate) source: &'a str,
120120
pub(crate) line_start: usize,
121121
pub(crate) origin: Option<&'a str>,
122122
pub(crate) annotations: Vec<Annotation<'a>>,
123123
pub(crate) fold: bool,
124124
}
125125

126-
impl<'a> Slice<'a> {
126+
impl<'a> Snippet<'a> {
127127
pub fn new(source: &'a str, line_start: usize) -> Self {
128128
Self {
129129
source,
@@ -162,7 +162,7 @@ pub enum Level {
162162
Help,
163163
}
164164

165-
/// An annotation for a [`Slice`].
165+
/// An annotation for a [`Snippet`].
166166
///
167167
/// This gets created by [`Label::span`].
168168
#[derive(Debug)]

tests/fixtures/deserialize.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use serde::{Deserialize, Deserializer, Serialize};
22
use std::ops::Range;
33

4-
use annotate_snippets::{renderer::Margin, Annotation, Label, Level, Message, Renderer, Slice};
4+
use annotate_snippets::{renderer::Margin, Annotation, Label, Level, Message, Renderer, Snippet};
55

66
#[derive(Deserialize)]
77
pub struct Fixture<'a> {
@@ -25,7 +25,7 @@ pub struct MessageDef<'a> {
2525
pub footer: Vec<Label<'a>>,
2626
#[serde(deserialize_with = "deserialize_slices")]
2727
#[serde(borrow)]
28-
pub slices: Vec<Slice<'a>>,
28+
pub slices: Vec<Snippet<'a>>,
2929
}
3030

3131
impl<'a> From<MessageDef<'a>> for Message<'a> {
@@ -42,7 +42,7 @@ impl<'a> From<MessageDef<'a>> for Message<'a> {
4242
}
4343
snippet = slices
4444
.into_iter()
45-
.fold(snippet, |snippet, slice| snippet.slice(slice));
45+
.fold(snippet, |snippet, slice| snippet.snippet(slice));
4646
snippet = footer
4747
.into_iter()
4848
.fold(snippet, |snippet, label| snippet.footer(label));
@@ -81,23 +81,23 @@ where
8181
.collect())
8282
}
8383

84-
fn deserialize_slices<'de, D>(deserializer: D) -> Result<Vec<Slice<'de>>, D::Error>
84+
fn deserialize_slices<'de, D>(deserializer: D) -> Result<Vec<Snippet<'de>>, D::Error>
8585
where
8686
D: Deserializer<'de>,
8787
{
8888
#[derive(Deserialize)]
8989
struct Wrapper<'a>(
90-
#[serde(with = "SliceDef")]
90+
#[serde(with = "SnippetDef")]
9191
#[serde(borrow)]
92-
SliceDef<'a>,
92+
SnippetDef<'a>,
9393
);
9494

9595
let v = Vec::deserialize(deserializer)?;
9696
Ok(v.into_iter().map(|Wrapper(a)| a.into()).collect())
9797
}
9898

9999
#[derive(Deserialize)]
100-
pub struct SliceDef<'a> {
100+
pub struct SnippetDef<'a> {
101101
#[serde(borrow)]
102102
pub source: &'a str,
103103
pub line_start: usize,
@@ -110,16 +110,16 @@ pub struct SliceDef<'a> {
110110
pub fold: bool,
111111
}
112112

113-
impl<'a> From<SliceDef<'a>> for Slice<'a> {
114-
fn from(val: SliceDef<'a>) -> Self {
115-
let SliceDef {
113+
impl<'a> From<SnippetDef<'a>> for Snippet<'a> {
114+
fn from(val: SnippetDef<'a>) -> Self {
115+
let SnippetDef {
116116
source,
117117
line_start,
118118
origin,
119119
annotations,
120120
fold,
121121
} = val;
122-
let mut slice = Slice::new(source, line_start).fold(fold);
122+
let mut slice = Snippet::new(source, line_start).fold(fold);
123123
if let Some(origin) = origin {
124124
slice = slice.origin(origin)
125125
}

0 commit comments

Comments
 (0)