Skip to main content

easydoc_writer/
doc_image.rs

1//! 向文档插入图片的配置。
2
3use std::path::PathBuf;
4
5/// 向文档插入图片的配置。
6pub struct DocImage {
7    /// Path to the image file.
8    pub path: PathBuf,
9    /// Desired width in pixels (applied via `Pic::new_with_dimensions`).
10    pub(crate) width: Option<u32>,
11    /// Desired height in pixels.
12    pub(crate) height: Option<u32>,
13    alt_text: Option<String>,
14}
15
16impl DocImage {
17    /// 创建图片配置。
18    #[must_use]
19    pub fn new(path: impl Into<PathBuf>) -> Self {
20        Self {
21            path: path.into(),
22            width: None,
23            height: None,
24            alt_text: None,
25        }
26    }
27
28    /// 设置图片宽度(像素)。
29    #[must_use]
30    pub fn width(mut self, w: u32) -> Self {
31        self.width = Some(w);
32        self
33    }
34
35    /// 设置图片高度(像素)。
36    #[must_use]
37    pub fn height(mut self, h: u32) -> Self {
38        self.height = Some(h);
39        self
40    }
41
42    /// 设置替代文本。
43    #[must_use]
44    pub fn alt_text(mut self, text: impl Into<String>) -> Self {
45        self.alt_text = Some(text.into());
46        self
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn doc_image_builder() {
56        let img = DocImage::new("/tmp/test.png")
57            .width(100)
58            .height(200)
59            .alt_text("test image");
60        assert_eq!(img.path, std::path::PathBuf::from("/tmp/test.png"));
61        assert_eq!(img.width, Some(100));
62        assert_eq!(img.height, Some(200));
63    }
64}