wit_encoder/
docs.rs

1use std::fmt;
2
3use crate::{Render, RenderOpts};
4
5/// Documentation
6#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
9pub struct Docs {
10    contents: String,
11}
12
13impl Docs {
14    pub fn new(contents: impl Into<String>) -> Self {
15        Self {
16            contents: contents.into(),
17        }
18    }
19
20    pub fn contents(&self) -> &str {
21        self.contents.as_ref()
22    }
23
24    pub fn set_contents(&mut self, contents: impl Into<String>) {
25        self.contents = contents.into();
26    }
27}
28
29impl<S> From<S> for Docs
30where
31    S: Into<String>,
32{
33    fn from(value: S) -> Self {
34        Self {
35            contents: value.into(),
36        }
37    }
38}
39
40impl Render for Docs {
41    fn render(&self, f: &mut fmt::Formatter<'_>, opts: &RenderOpts) -> fmt::Result {
42        for line in self.contents.lines() {
43            write!(f, "{}/// {}\n", opts.spaces(), line)?;
44        }
45        Ok(())
46    }
47}