Skip to content

Commit bc17c26

Browse files
author
Zibi Braniecki
committed
Add documentation to src/formatter
1 parent 97671cb commit bc17c26

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

src/formatter/mod.rs

+33
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
//! DisplayListFormatter is a module handling the formatting of a
2+
//! [DisplayList](super::display_list::DisplayList) into
3+
//! a formatted string.
4+
//!
5+
//! Besides formatting into a string it also uses a `style::Stylesheet` to
6+
//! provide additional styling like colors and emphasis to the text.
7+
18
pub mod style;
29

310
use display_list::*;
@@ -14,6 +21,32 @@ fn repeat_char(c: char, n: usize) -> String {
1421
s.repeat(n)
1522
}
1623

24+
/// DisplayListFormatter' constructor accepts a single argument which
25+
/// allows the formatter to optionally apply colors and emphasis
26+
/// using `ansi_term` crate.
27+
///
28+
/// Example:
29+
///
30+
/// ```
31+
/// use annotate_snippets::formatter::DisplayListFormatter;
32+
/// use annotate_snippets::display_list::{DisplayList, DisplayLine, DisplaySourceLine};
33+
///
34+
/// let dlf = DisplayListFormatter::new(false); // Don't use colors
35+
///
36+
/// let dl = DisplayList {
37+
/// body: vec![
38+
/// DisplayLine::Source {
39+
/// lineno: Some(192),
40+
/// inline_marks: vec![],
41+
/// line: DisplaySourceLine::Content {
42+
/// text: "Example line of text".into(),
43+
/// range: (0, 21)
44+
/// }
45+
/// }
46+
/// ]
47+
/// };
48+
/// assert_eq!(dlf.format(dl), "192 | Example line of text");
49+
/// ```
1750
pub struct DisplayListFormatter {
1851
stylesheet: Box<Stylesheet>,
1952
}

src/formatter/style.rs

+76
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,98 @@
1+
//! Set of structures required to implement a stylesheet for
2+
//! [DisplayListFormatter](super::DisplayListFormatter).
3+
//!
4+
//! In order to provide additional styling information for the
5+
//! formatter, a structs can implement `Stylesheet` and `Style`
6+
//! traits.
7+
//!
8+
//! Example:
9+
//!
10+
//! ```
11+
//! use annotate_snippets::formatter::style::{Stylesheet, StyleClass, Style};
12+
//!
13+
//! struct HTMLStyle {
14+
//! prefix: String,
15+
//! postfix: String,
16+
//! };
17+
//!
18+
//! impl HTMLStyle {
19+
//! fn new(prefix: &str, postfix: &str) -> Self {
20+
//! HTMLStyle {
21+
//! prefix: prefix.into(),
22+
//! postfix: postfix.into()
23+
//! }
24+
//! }
25+
//! };
26+
//!
27+
//! impl Style for HTMLStyle {
28+
//! fn paint(&self, text: String) -> String {
29+
//! format!("{}{}{}", self.prefix, text, self.postfix)
30+
//! }
31+
//!
32+
//! fn bold(&self) -> Box<Style> {
33+
//! Box::new(HTMLStyle {
34+
//! prefix: format!("{}<b>", self.prefix),
35+
//! postfix: format!("</b>{}", self.postfix),
36+
//! })
37+
//! }
38+
//! }
39+
//!
40+
//! struct HTMLStylesheet {};
41+
//!
42+
//!
43+
//! impl Stylesheet for HTMLStylesheet {
44+
//! fn get_style(&self, class: StyleClass) -> Box<Style> {
45+
//! let s = match class {
46+
//! StyleClass::Error => HTMLStyle::new("<span style='color:red'>", "</span>"),
47+
//! StyleClass::Warning => HTMLStyle::new("<span style='color:orange'>", "</span>"),
48+
//! StyleClass::Info => HTMLStyle::new("<span style='color:yellow'>", "</span>"),
49+
//! StyleClass::Note => HTMLStyle::new("<span style='color:blue'>", "</span>"),
50+
//! StyleClass::Help => HTMLStyle::new("<span style='color:green'>", "</span>"),
51+
//! StyleClass::LineNo => HTMLStyle::new("<strong>", "</strong>"),
52+
//! StyleClass::Emphasis => HTMLStyle::new("<i>", "</i>"),
53+
//! StyleClass::None => HTMLStyle::new("", ""),
54+
//! };
55+
//! Box::new(s)
56+
//! }
57+
//! }
58+
//! ```
59+
60+
/// StyleClass is a collection of named variants of style classes
61+
/// that DisplayListFormatter uses.
162
pub enum StyleClass {
63+
/// Message indicating an error.
264
Error,
65+
/// Message indicating a warning.
366
Warning,
67+
/// Message indicating an information.
468
Info,
69+
/// Message indicating a note.
570
Note,
71+
/// Message indicating a help.
672
Help,
773

74+
/// Style for line numbers.
875
LineNo,
976

77+
/// Parts of the text that are to be emphasised.
1078
Emphasis,
1179

80+
/// Parts of the text that are regular. Usually a no-op.
1281
None,
1382
}
1483

84+
/// This trait implements a return value for the `Stylesheet::get_style`.
1585
pub trait Style {
86+
/// The method used by the DisplayListFormatter to style the message.
1687
fn paint(&self, text: String) -> String;
88+
/// The method used by the DisplayListFormatter to display the message
89+
/// in bold font.
1790
fn bold(&self) -> Box<Style>;
1891
}
1992

93+
/// Trait to annotate structs that can provide `Style` implementations for
94+
/// every `StyleClass` variant.
2095
pub trait Stylesheet {
96+
/// Returns a `Style` implementer based on the requested `StyleClass` variant.
2197
fn get_style(&self, class: StyleClass) -> Box<Style>;
2298
}

0 commit comments

Comments
 (0)