Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c2bebe8

Browse files
authored
Merge pull request #232 from Muscraft/use-cow
fix: Take Into<Cow> instead of &str
2 parents 8280ae0 + 37542ef commit c2bebe8

File tree

5 files changed

+119
-69
lines changed

5 files changed

+119
-69
lines changed

‎src/level.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
33
use crate::renderer::stylesheet::Stylesheet;
44
use crate::snippet::{ERROR_TXT, HELP_TXT, INFO_TXT, NOTE_TXT, WARNING_TXT};
5-
use crate::Title;
5+
use crate::{OptionCow,Title};
66
use anstyle::Style;
7+
use std::borrow::Cow;
78

89
/// Default `error:` [`Level`]
910
pub const ERROR: Level<'_> = Level {
@@ -38,7 +39,7 @@ pub const HELP: Level<'_> = Level {
3839
/// [`Title`] severity level
3940
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
4041
pub struct Level<'a> {
41-
pub(crate) name: Option<Option<&'a str>>,
42+
pub(crate) name: Option<Option<Cow<'a, str>>>,
4243
pub(crate) level: LevelInner,
4344
}
4445

@@ -56,9 +57,9 @@ impl<'a> Level<'a> {
5657
/// not allowed to be passed to this function.
5758
///
5859
/// </div>
59-
pub fn text(self, text: Option<&'astr>) -> Level<'a> {
60+
pub fn text(self, text: implInto<OptionCow<'a>>) -> Level<'a> {
6061
Level {
61-
name: Some(text),
62+
name: Some(text.into().0),
6263
level: self.level,
6364
}
6465
}
@@ -72,11 +73,11 @@ impl<'a> Level<'a> {
7273
/// not allowed to be passed to this function.
7374
///
7475
/// </div>
75-
pub fn title(self, title: &'a str) -> Title<'a> {
76+
pub fn title(self, title: implInto<Cow<'a, str>>) -> Title<'a> {
7677
Title {
7778
level: self,
7879
id: None,
79-
title,
80+
title: title.into(),
8081
is_pre_styled: false,
8182
}
8283
}
@@ -89,18 +90,18 @@ impl<'a> Level<'a> {
8990
/// used to normalize untrusted text before it is passed to this function.
9091
///
9192
/// </div>
92-
pub fn pre_styled_title(self, title: &'a str) -> Title<'a> {
93+
pub fn pre_styled_title(self, title: implInto<Cow<'a, str>>) -> Title<'a> {
9394
Title {
9495
level: self,
9596
id: None,
96-
title,
97+
title: title.into(),
9798
is_pre_styled: true,
9899
}
99100
}
100101

101-
pub(crate) fn as_str(&self) -> &'a str {
102-
match (self.name, self.level) {
103-
(Some(Some(name)), _) => name,
102+
pub(crate) fn as_str(&'aself) -> &'a str {
103+
match (&self.name, self.level) {
104+
(Some(Some(name)), _) => name.as_ref(),
104105
(Some(None), _) => "",
105106
(None, LevelInner::Error) => ERROR_TXT,
106107
(None, LevelInner::Warning) => WARNING_TXT,

‎src/renderer/mod.rs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,14 @@ impl Renderer {
229229
.find_map(|s| match &s {
230230
Element::Cause(cause) => {
231231
if cause.markers.iter().any(|m| m.kind.is_primary()) {
232-
Some(cause.path)
232+
Some(cause.path.as_ref())
233233
} else {
234234
None
235235
}
236236
}
237237
Element::Origin(origin) => {
238238
if origin.primary {
239-
Some(Some(origin.path))
239+
Some(Some(&origin.path))
240240
} else {
241241
None
242242
}
@@ -248,8 +248,8 @@ impl Renderer {
248248
.elements
249249
.iter()
250250
.find_map(|s| match &s {
251-
Element::Cause(cause) => Some(cause.path),
252-
Element::Origin(origin) => Some(Some(origin.path)),
251+
Element::Cause(cause) => Some(cause.path.as_ref()),
252+
Element::Origin(origin) => Some(Some(&origin.path)),
253253
_ => None,
254254
})
255255
.unwrap_or_default(),
@@ -269,7 +269,7 @@ impl Renderer {
269269
let mut max_depth = 0;
270270
for e in &group.elements {
271271
if let Element::Cause(cause) = e {
272-
let source_map = SourceMap::new(cause.source, cause.line_start);
272+
let source_map = SourceMap::new(&cause.source, cause.line_start);
273273
let (depth, annotated_lines) =
274274
source_map.annotated_lines(cause.markers.clone(), cause.fold);
275275
max_depth = max(max_depth, depth);
@@ -340,7 +340,7 @@ impl Renderer {
340340
}
341341
Element::Suggestion(suggestion) => {
342342
let source_map =
343-
SourceMap::new(suggestion.source, suggestion.line_start);
343+
SourceMap::new(&suggestion.source, suggestion.line_start);
344344
self.emit_suggestion_default(
345345
&mut buffer,
346346
suggestion,
@@ -426,10 +426,10 @@ impl Renderer {
426426
let labels_inner = cause
427427
.markers
428428
.iter()
429-
.filter_map(|ann| match ann.label {
429+
.filter_map(|ann| match &ann.label {
430430
Some(msg) if ann.kind.is_primary() => {
431431
if !msg.trim().is_empty() {
432-
Some(msg.to_owned())
432+
Some(msg.to_string())
433433
} else {
434434
None
435435
}
@@ -442,11 +442,11 @@ impl Renderer {
442442
labels = Some(labels_inner);
443443
}
444444

445-
if let Some(path) = cause.path {
446-
let mut origin = Origin::new(path);
445+
if let Some(path) = &cause.path {
446+
let mut origin = Origin::new(path.as_ref());
447447
origin.primary = true;
448448

449-
let source_map = SourceMap::new(cause.source, cause.line_start);
449+
let source_map = SourceMap::new(&cause.source, cause.line_start);
450450
let (_depth, annotated_lines) =
451451
source_map.annotated_lines(cause.markers.clone(), cause.fold);
452452

@@ -531,7 +531,7 @@ impl Renderer {
531531
if title.level.name != Some(None) {
532532
buffer.append(buffer_msg_line_offset, title.level.as_str(), label_style);
533533
label_width += title.level.as_str().len();
534-
if let Some(Id { id: Some(id), url }) = title.id {
534+
if let Some(Id { id: Some(id), url }) = &title.id {
535535
buffer.append(buffer_msg_line_offset, "[", label_style);
536536
if let Some(url) = url.as_ref() {
537537
buffer.append(
@@ -575,9 +575,9 @@ impl Renderer {
575575
});
576576

577577
let (title_str, style) = if title.is_pre_styled {
578-
(title.title.to_owned(), ElementStyle::NoStyle)
578+
(title.title.to_string(), ElementStyle::NoStyle)
579579
} else {
580-
(normalize_whitespace(title.title), title_element_style)
580+
(normalize_whitespace(&title.title), title_element_style)
581581
};
582582
for (i, text) in title_str.lines().enumerate() {
583583
if i != 0 {
@@ -654,7 +654,7 @@ impl Renderer {
654654
format!("{}:{}:{}", origin.path, line, col)
655655
}
656656
(Some(line), None) => format!("{}:{}", origin.path, line),
657-
_ => origin.path.to_owned(),
657+
_ => origin.path.to_string(),
658658
};
659659

660660
buffer.append(buffer_msg_line_offset, &str, ElementStyle::LineAndColumn);
@@ -671,17 +671,17 @@ impl Renderer {
671671
buffer: &mut StyledBuffer,
672672
max_line_num_len: usize,
673673
snippet: &Snippet<'_, Annotation<'_>>,
674-
primary_path: Option<&str>,
674+
primary_path: Option<&Cow<'_,str>>,
675675
sm: &SourceMap<'_>,
676676
annotated_lines: &[AnnotatedLineInfo<'_>],
677677
multiline_depth: usize,
678678
is_cont: bool,
679679
) {
680-
if let Some(path) = snippet.path {
681-
let mut origin = Origin::new(path);
680+
if let Some(path) = &snippet.path {
681+
let mut origin = Origin::new(path.as_ref());
682682
// print out the span location and spacer before we print the annotated source
683683
// to do this, we need to know if this span will be primary
684-
let is_primary = primary_path == Some(origin.path);
684+
let is_primary = primary_path == Some(&origin.path);
685685

686686
if is_primary {
687687
origin.primary = true;
@@ -1394,7 +1394,7 @@ impl Renderer {
13941394
} else {
13951395
(pos + 2, annotation.start.display.saturating_sub(left))
13961396
};
1397-
if let Some(label) = annotation.label {
1397+
if let Some(label) = &annotation.label {
13981398
buffer.puts(line_offset + pos, code_offset + col, label, style);
13991399
}
14001400
}
@@ -1535,7 +1535,7 @@ impl Renderer {
15351535
suggestion: &Snippet<'_, Patch<'_>>,
15361536
max_line_num_len: usize,
15371537
sm: &SourceMap<'_>,
1538-
primary_path: Option<&str>,
1538+
primary_path: Option<&Cow<'_,str>>,
15391539
is_cont: bool,
15401540
) {
15411541
let suggestions = sm.splice_lines(suggestion.markers.clone());
@@ -1558,8 +1558,8 @@ impl Renderer {
15581558
ElementStyle::LineNumber,
15591559
);
15601560
}
1561-
if suggestion.path != primary_path {
1562-
if let Some(path) = suggestion.path {
1561+
if suggestion.path.as_ref() != primary_path {
1562+
if let Some(path) = suggestion.path.as_ref() {
15631563
let (loc, _) = sm.span_to_locations(parts[0].span.clone());
15641564
// --> file.rs:line:col
15651565
// |
@@ -1781,7 +1781,7 @@ impl Renderer {
17811781
// ...or trailing spaces. Account for substitutions containing unicode
17821782
// characters.
17831783
let sub_len: usize = str_width(if is_whitespace_addition {
1784-
part.replacement
1784+
&part.replacement
17851785
} else {
17861786
part.replacement.trim()
17871787
});
@@ -1802,7 +1802,7 @@ impl Renderer {
18021802
let padding: usize = max_line_num_len + 3;
18031803
for p in underline_start..underline_end {
18041804
if matches!(show_code_change, DisplaySuggestion::Underline)
1805-
&& is_different(sm, part.replacement, part.span.clone())
1805+
&& is_different(sm, &part.replacement, part.span.clone())
18061806
{
18071807
// If this is a replacement, underline with `~`, if this is an addition
18081808
// underline with `+`.
@@ -1903,7 +1903,7 @@ impl Renderer {
19031903
}
19041904

19051905
// length of the code after substitution
1906-
let full_sub_len = str_width(part.replacement) as isize;
1906+
let full_sub_len = str_width(&part.replacement) as isize;
19071907

19081908
// length of the code to be substituted
19091909
let snippet_len = span_end_pos as isize - span_start_pos as isize;
@@ -2631,7 +2631,7 @@ pub(crate) struct LineAnnotation<'a> {
26312631
pub kind: AnnotationKind,
26322632

26332633
/// Optional label to display adjacent to the annotation.
2634-
pub label: Option<&'a str>,
2634+
pub label: Option<Cow<'a, str>>,
26352635

26362636
/// Is this a single line, multiline or multiline span minimized down to a
26372637
/// smaller span.
@@ -2658,7 +2658,7 @@ impl LineAnnotation<'_> {
26582658
}
26592659

26602660
pub(crate) fn has_label(&self) -> bool {
2661-
if let Some(label) = self.label {
2661+
if let Some(label) = &self.label {
26622662
// Consider labels with no text as effectively not being there
26632663
// to avoid weird output with unnecessary vertical lines, like:
26642664
//

‎src/renderer/source_map.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::renderer::{char_width, is_different, num_overlap, LineAnnotation, LineAnnotationType};
22
use crate::{Annotation, AnnotationKind, Patch};
3+
use std::borrow::Cow;
34
use std::cmp::{max, min};
45
use std::ops::Range;
56

@@ -453,14 +454,14 @@ impl<'a> SourceMap<'a> {
453454
.replacement
454455
.split('\n')
455456
.next()
456-
.unwrap_or(part.replacement)
457+
.unwrap_or(&part.replacement)
457458
.chars()
458459
.map(|c| match c {
459460
'\t' => 4,
460461
_ => 1,
461462
})
462463
.sum();
463-
if !is_different(self, part.replacement, part.span.clone()) {
464+
if !is_different(self, &part.replacement, part.span.clone()) {
464465
// Account for cases where we are suggesting the same code that's already
465466
// there. This shouldn't happen often, but in some cases for multipart
466467
// suggestions it's much easier to handle it here than in the origin.
@@ -470,7 +471,7 @@ impl<'a> SourceMap<'a> {
470471
end: (cur_lo.char as isize + acc + len) as usize,
471472
});
472473
}
473-
buf.push_str(part.replacement);
474+
buf.push_str(&part.replacement);
474475
// Account for the difference between the width of the current code and the
475476
// snippet being suggested, so that the *later* suggestions are correctly
476477
// aligned on the screen. Note that cur_hi and cur_lo can be on different
@@ -514,7 +515,7 @@ pub(crate) struct MultilineAnnotation<'a> {
514515
pub start: Loc,
515516
pub end: Loc,
516517
pub kind: AnnotationKind,
517-
pub label: Option<&'a str>,
518+
pub label: Option<Cow<'a, str>>,
518519
pub overlaps_exactly: bool,
519520
pub highlight_source: bool,
520521
}
@@ -555,7 +556,7 @@ impl<'a> MultilineAnnotation<'a> {
555556
},
556557
end: self.end,
557558
kind: self.kind,
558-
label: self.label,
559+
label: self.label.clone(),
559560
annotation_type: LineAnnotationType::MultilineEnd(self.depth),
560561
highlight_source: self.highlight_source,
561562
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /