Skip to content

Commit

Permalink
Change Snippet.fragments -> Snippet.fragment (#1243)
Browse files Browse the repository at this point in the history
* Change Snippet.fragments -> Snippet.fragment
* Apply suggestions from code review

Co-authored-by: Liam Warfield <lwarfield@arista.com>
  • Loading branch information
liamwarfield and Liam Warfield authored Jan 3, 2022
1 parent 3129d86 commit 17e00df
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
6 changes: 3 additions & 3 deletions examples/snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ fn highlight(snippet: Snippet) -> String {
let mut start_from = 0;

for fragment_range in snippet.highlighted() {
result.push_str(&snippet.fragments()[start_from..fragment_range.start]);
result.push_str(&snippet.fragment()[start_from..fragment_range.start]);
result.push_str(" --> ");
result.push_str(&snippet.fragments()[fragment_range.clone()]);
result.push_str(&snippet.fragment()[fragment_range.clone()]);
result.push_str(" <-- ");
start_from = fragment_range.end;
}

result.push_str(&snippet.fragments()[start_from..]);
result.push_str(&snippet.fragment()[start_from..]);
result
}
32 changes: 16 additions & 16 deletions src/snippet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl FragmentCandidate {
/// Contains a fragment of a document, and some highlighed parts inside it.
#[derive(Debug)]
pub struct Snippet {
fragments: String,
fragment: String,
highlighted: Vec<Range<usize>>,
}

Expand All @@ -64,7 +64,7 @@ impl Snippet {
/// Create a new, empty, `Snippet`
pub fn empty() -> Snippet {
Snippet {
fragments: String::new(),
fragment: String::new(),
highlighted: Vec::new(),
}
}
Expand All @@ -75,21 +75,21 @@ impl Snippet {
let mut start_from: usize = 0;

for item in self.highlighted.iter() {
html.push_str(&encode_minimal(&self.fragments[start_from..item.start]));
html.push_str(&encode_minimal(&self.fragment[start_from..item.start]));
html.push_str(HIGHLIGHTEN_PREFIX);
html.push_str(&encode_minimal(&self.fragments[item.clone()]));
html.push_str(&encode_minimal(&self.fragment[item.clone()]));
html.push_str(HIGHLIGHTEN_POSTFIX);
start_from = item.end;
}
html.push_str(&encode_minimal(
&self.fragments[start_from..self.fragments.len()],
&self.fragment[start_from..self.fragment.len()],
));
html
}

/// Returns a fragment from the `Snippet`.
pub fn fragments(&self) -> &str {
&self.fragments
/// Returns the fragment of text used in the snippet.
pub fn fragment(&self) -> &str {
&self.fragment
}

/// Returns a list of higlighted positions from the `Snippet`.
Expand Down Expand Up @@ -168,14 +168,14 @@ fn select_best_fragment_combination(fragments: &[FragmentCandidate], text: &str)
.map(|item| item.start - fragment.start_offset..item.end - fragment.start_offset)
.collect();
Snippet {
fragments: fragment_text.to_string(),
fragment: fragment_text.to_string(),
highlighted,
}
} else {
// when there no fragments to chose from,
// for now create a empty snippet
Snippet {
fragments: String::new(),
fragment: String::new(),
highlighted: vec![],
}
}
Expand Down Expand Up @@ -329,7 +329,7 @@ Survey in 2016, 2017, and 2018."#;
}
let snippet = select_best_fragment_combination(&fragments[..], TEST_TEXT);
assert_eq!(
snippet.fragments,
snippet.fragment,
"Rust is a systems programming language sponsored by\n\
Mozilla which describes it as a \"safe"
);
Expand Down Expand Up @@ -391,7 +391,7 @@ Survey in 2016, 2017, and 2018."#;
}

let snippet = select_best_fragment_combination(&fragments[..], text);
assert_eq!(snippet.fragments, "c d");
assert_eq!(snippet.fragment, "c d");
assert_eq!(snippet.to_html(), "<b>c</b> d");
}

Expand All @@ -413,7 +413,7 @@ Survey in 2016, 2017, and 2018."#;
}

let snippet = select_best_fragment_combination(&fragments[..], text);
assert_eq!(snippet.fragments, "e f");
assert_eq!(snippet.fragment, "e f");
assert_eq!(snippet.to_html(), "e <b>f</b>");
}

Expand All @@ -436,7 +436,7 @@ Survey in 2016, 2017, and 2018."#;
}

let snippet = select_best_fragment_combination(&fragments[..], text);
assert_eq!(snippet.fragments, "e f g");
assert_eq!(snippet.fragment, "e f g");
assert_eq!(snippet.to_html(), "e <b>f</b> g");
}

Expand All @@ -452,7 +452,7 @@ Survey in 2016, 2017, and 2018."#;
assert_eq!(fragments.len(), 0);

let snippet = select_best_fragment_combination(&fragments[..], text);
assert_eq!(snippet.fragments, "");
assert_eq!(snippet.fragment, "");
assert_eq!(snippet.to_html(), "");
}

Expand All @@ -465,7 +465,7 @@ Survey in 2016, 2017, and 2018."#;
assert_eq!(fragments.len(), 0);

let snippet = select_best_fragment_combination(&fragments[..], text);
assert_eq!(snippet.fragments, "");
assert_eq!(snippet.fragment, "");
assert_eq!(snippet.to_html(), "");
}

Expand Down

0 comments on commit 17e00df

Please sign in to comment.