1 //! Set of structures required to implement a stylesheet
2 //!
3 //! In order to provide additional styling information for the
4 //! formatter, a structs can implement `Stylesheet` and `Style`
5 //! traits.
6 //!
7 use std::fmt;
8 
9 /// StyleClass is a collection of named variants of style classes
10 pub enum StyleClass {
11     /// Message indicating an error.
12     Error,
13     /// Message indicating a warning.
14     Warning,
15     /// Message indicating an information.
16     Info,
17     /// Message indicating a note.
18     Note,
19     /// Message indicating a help.
20     Help,
21 
22     /// Style for line numbers.
23     LineNo,
24 
25     /// Parts of the text that are to be emphasised.
26     Emphasis,
27 
28     /// Parts of the text that are regular. Usually a no-op.
29     None,
30 }
31 
32 /// This trait implements a return value for the `Stylesheet::get_style`.
33 pub trait Style {
34     /// The method used to write text with formatter
paint(&self, text: &str, f: &mut fmt::Formatter<'_>) -> fmt::Result35     fn paint(&self, text: &str, f: &mut fmt::Formatter<'_>) -> fmt::Result;
36     /// The method used to write display function with formatter
paint_fn<'a>( &self, c: Box<dyn FnOnce(&mut fmt::Formatter<'_>) -> fmt::Result + 'a>, f: &mut fmt::Formatter<'_>, ) -> fmt::Result37     fn paint_fn<'a>(
38         &self,
39         c: Box<dyn FnOnce(&mut fmt::Formatter<'_>) -> fmt::Result + 'a>,
40         f: &mut fmt::Formatter<'_>,
41     ) -> fmt::Result;
42     /// The method used by the `Formatter` to display the message in bold font.
bold(&self) -> Box<dyn Style>43     fn bold(&self) -> Box<dyn Style>;
44 }
45 
46 /// Trait to annotate structs that can provide `Style` implementations for
47 /// every `StyleClass` variant.
48 pub trait Stylesheet {
49     /// Returns a `Style` implementer based on the requested `StyleClass` variant.
get_style(&self, class: StyleClass) -> Box<dyn Style>50     fn get_style(&self, class: StyleClass) -> Box<dyn Style>;
51 }
52