somedoc/model/block/caption.rs
1#[cfg(feature = "fmt_json")]
2use serde::{Deserialize, Serialize};
3use std::ops::Deref;
4
5// ------------------------------------------------------------------------------------------------
6// Public Types
7// ------------------------------------------------------------------------------------------------
8
9///
10/// A `Caption` instance holds simple plain, un-styled, text, it is intended to be included with
11/// blocks that are commonly labeled such as tables, images, etc.
12///
13/// While some formats do allow styled text within captions, this is currently not supported. It
14/// may be possible in the future to provide a styled caption with an inner list of `InlineContent`.
15///
16#[derive(Clone, Debug)]
17#[cfg_attr(feature = "fmt_json", derive(Serialize, Deserialize))]
18pub struct Caption(String);
19
20///
21/// Implemented by values that support a caption.
22///
23pub trait HasCaption {
24 /// Returns `true` if a caption is set, else `false`.
25 fn has_caption(&self) -> bool {
26 self.caption().is_some()
27 }
28
29 /// Return the caption, if present.
30 fn caption(&self) -> &Option<Caption>;
31
32 /// Set the caption to the provided value.
33 fn set_caption(&mut self, caption: Caption) -> &mut Self;
34
35 /// Set the caption to `None`.
36 fn unset_caption(&mut self) -> &mut Self;
37}
38
39// ------------------------------------------------------------------------------------------------
40// Implementations
41// ------------------------------------------------------------------------------------------------
42
43impl Default for Caption {
44 fn default() -> Self {
45 Self(String::new())
46 }
47}
48
49impl From<String> for Caption {
50 fn from(inner: String) -> Self {
51 Self::from(inner.as_str())
52 }
53}
54
55impl From<&str> for Caption {
56 fn from(inner: &str) -> Self {
57 Self(inner.to_string())
58 }
59}
60
61impl Deref for Caption {
62 type Target = str;
63
64 fn deref(&self) -> &Self::Target {
65 &self.0
66 }
67}
68
69inner_impl!(Caption, String);