]>
Commit | Line | Data |
---|---|---|
f20569fa XL |
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: &str) -> 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. | |
62 | pub enum StyleClass { | |
63 | /// Message indicating an error. | |
64 | Error, | |
65 | /// Message indicating a warning. | |
66 | Warning, | |
67 | /// Message indicating an information. | |
68 | Info, | |
69 | /// Message indicating a note. | |
70 | Note, | |
71 | /// Message indicating a help. | |
72 | Help, | |
73 | ||
74 | /// Style for line numbers. | |
75 | LineNo, | |
76 | ||
77 | /// Parts of the text that are to be emphasised. | |
78 | Emphasis, | |
79 | ||
80 | /// Parts of the text that are regular. Usually a no-op. | |
81 | None, | |
82 | } | |
83 | ||
84 | /// This trait implements a return value for the `Stylesheet::get_style`. | |
85 | pub trait Style { | |
86 | /// The method used by the DisplayListFormatter to style the message. | |
87 | fn paint(&self, text: &str) -> String; | |
88 | /// The method used by the DisplayListFormatter to display the message | |
89 | /// in bold font. | |
90 | fn bold(&self) -> Box<dyn Style>; | |
91 | } | |
92 | ||
93 | /// Trait to annotate structs that can provide `Style` implementations for | |
94 | /// every `StyleClass` variant. | |
95 | pub trait Stylesheet { | |
96 | /// Returns a `Style` implementer based on the requested `StyleClass` variant. | |
97 | fn get_style(&self, class: StyleClass) -> Box<dyn Style>; | |
98 | } |