Skip to content

Commit

Permalink
chore(renderer): Add doc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Muscraft committed Dec 4, 2023
1 parent d87d015 commit 7e60193
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/renderer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
//! The renderer for [`Snippet`]s
//!
//! # Example
//! ```
//! use annotate_snippets::renderer::Renderer;
//! use annotate_snippets::snippet::{Annotation, AnnotationType, Slice, Snippet};
//! let snippet = Snippet {
//! title: Some(Annotation {
//! label: Some("mismatched types"),
//! id: None,
//! annotation_type: AnnotationType::Error,
//! }),
//! footer: vec![],
//! slices: vec![
//! Slice {
//! source: "Foo",
//! line_start: 51,
//! origin: Some("src/format.rs"),
//! fold: false,
//! annotations: vec![],
//! },
//! Slice {
//! source: "Faa",
//! line_start: 129,
//! origin: Some("src/display.rs"),
//! fold: false,
//! annotations: vec![],
//! },
//! ],
//! };
//!
//! let renderer = Renderer::styled();
//! println!("{}", renderer.render(snippet));
pub mod margin;

use crate::display_list::DisplayList;
Expand All @@ -6,6 +40,7 @@ use anstyle::{AnsiColor, Effects, Style};
use margin::Margin;
use std::fmt::Display;

/// A renderer for [`Snippet`]s
#[derive(Clone)]
pub struct Renderer {
anonymized_line_numbers: bool,
Expand All @@ -14,6 +49,7 @@ pub struct Renderer {
}

impl Renderer {
/// Render a snippet into a `Display`able object
pub fn render<'a>(&'a self, snippet: Snippet<'a>) -> impl Display + 'a {
DisplayList::new(
snippet,
Expand Down Expand Up @@ -50,51 +86,90 @@ impl Renderer {
}
}

/// Anonymize line numbers
///
/// This enables (or disables) line number anonymization. When enabled, line numbers are replaced
/// with `LL`.
///
/// # Example
///
/// ```text
/// --> $DIR/whitespace-trimming.rs:4:193
/// |
/// LL | ... let _: () = 42;
/// | ^^ expected (), found integer
/// |
/// ```
pub const fn anonymized_line_numbers(mut self, anonymized_line_numbers: bool) -> Self {
self.anonymized_line_numbers = anonymized_line_numbers;
self
}

/// Set the margin for the output
///
/// This controls the various margins of the output.
///
/// # Example
///
/// ```text
/// error: expected type, found `22`
/// --> examples/footer.rs:29:25
/// |
/// 26 | ... annotations: vec![SourceAnnotation {
/// | ---------------- info: while parsing this struct
/// ...
/// 29 | ... range: <22, 25>,
/// | ^^
/// |
/// ```
pub const fn margin(mut self, margin: Option<Margin>) -> Self {
self.margin = margin;
self
}

/// Set the output style for `error`
pub const fn error(mut self, style: Style) -> Self {
self.stylesheet.error = style;
self
}

/// Set the output style for `warning`
pub const fn warning(mut self, style: Style) -> Self {
self.stylesheet.warning = style;
self
}

/// Set the output style for `info`
pub const fn info(mut self, style: Style) -> Self {
self.stylesheet.info = style;
self
}

/// Set the output style for `note`
pub const fn note(mut self, style: Style) -> Self {
self.stylesheet.note = style;
self
}

/// Set the output style for `help`
pub const fn help(mut self, style: Style) -> Self {
self.stylesheet.help = style;
self
}

/// Set the output style for line numbers
pub const fn line_no(mut self, style: Style) -> Self {
self.stylesheet.line_no = style;
self
}

/// Set the output style for emphasis
pub const fn emphasis(mut self, style: Style) -> Self {
self.stylesheet.emphasis = style;
self
}

/// Set the output style for none
pub const fn none(mut self, style: Style) -> Self {
self.stylesheet.none = style;
self
Expand Down

0 comments on commit 7e60193

Please sign in to comment.