1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::fmt;

use crate::{Render, RenderOpts};

/// Documentation
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
pub struct Docs {
    contents: String,
}

impl Docs {
    pub fn new(contents: impl Into<String>) -> Self {
        Self {
            contents: contents.into(),
        }
    }
}

impl<S> From<S> for Docs
where
    S: Into<String>,
{
    fn from(value: S) -> Self {
        Self {
            contents: value.into(),
        }
    }
}

impl Render for Docs {
    fn render(&self, f: &mut fmt::Formatter<'_>, opts: &RenderOpts) -> fmt::Result {
        for line in self.contents.lines() {
            write!(f, "{}/// {}\n", opts.spaces(), line)?;
        }
        Ok(())
    }
}