|
| 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. |
1 | 62 | pub enum StyleClass {
|
| 63 | + /// Message indicating an error. |
2 | 64 | Error,
|
| 65 | + /// Message indicating a warning. |
3 | 66 | Warning,
|
| 67 | + /// Message indicating an information. |
4 | 68 | Info,
|
| 69 | + /// Message indicating a note. |
5 | 70 | Note,
|
| 71 | + /// Message indicating a help. |
6 | 72 | Help,
|
7 | 73 |
|
| 74 | + /// Style for line numbers. |
8 | 75 | LineNo,
|
9 | 76 |
|
| 77 | + /// Parts of the text that are to be emphasised. |
10 | 78 | Emphasis,
|
11 | 79 |
|
| 80 | + /// Parts of the text that are regular. Usually a no-op. |
12 | 81 | None,
|
13 | 82 | }
|
14 | 83 |
|
| 84 | +/// This trait implements a return value for the `Stylesheet::get_style`. |
15 | 85 | pub trait Style {
|
| 86 | + /// The method used by the DisplayListFormatter to style the message. |
16 | 87 | fn paint(&self, text: String) -> String;
|
| 88 | + /// The method used by the DisplayListFormatter to display the message |
| 89 | + /// in bold font. |
17 | 90 | fn bold(&self) -> Box<Style>;
|
18 | 91 | }
|
19 | 92 |
|
| 93 | +/// Trait to annotate structs that can provide `Style` implementations for |
| 94 | +/// every `StyleClass` variant. |
20 | 95 | pub trait Stylesheet {
|
| 96 | + /// Returns a `Style` implementer based on the requested `StyleClass` variant. |
21 | 97 | fn get_style(&self, class: StyleClass) -> Box<Style>;
|
22 | 98 | }
|
0 commit comments