Skip to main content

acdc_parser/model/
attribution.rs

1use std::ops::Deref;
2
3use serde::{Serialize, ser::Serializer};
4
5use super::inlines::InlineNode;
6
7/// An attribution in a blockquote (the author/source of the quote).
8///
9/// `Attribution` is a newtype wrapper around `Vec<InlineNode>` that provides
10/// convenient access to inline content. Attributions can include formatting,
11/// links, and other inline elements.
12///
13/// # Serialization
14///
15/// Serializes as a plain JSON array of inline nodes.
16#[derive(Clone, Debug, Default, PartialEq)]
17#[non_exhaustive]
18pub struct Attribution(Vec<InlineNode>);
19
20impl Serialize for Attribution {
21    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
22    where
23        S: Serializer,
24    {
25        self.0.serialize(serializer)
26    }
27}
28
29impl Attribution {
30    /// Creates a new `Attribution` with the given inline content.
31    #[must_use]
32    pub fn new(inlines: Vec<InlineNode>) -> Self {
33        Self(inlines)
34    }
35
36    /// Returns `true` if the attribution has no content.
37    #[must_use]
38    pub fn is_empty(&self) -> bool {
39        self.0.is_empty()
40    }
41
42    /// Returns the number of inline nodes in the attribution.
43    #[must_use]
44    pub fn len(&self) -> usize {
45        self.0.len()
46    }
47}
48
49impl From<Vec<InlineNode>> for Attribution {
50    fn from(inlines: Vec<InlineNode>) -> Self {
51        Self(inlines)
52    }
53}
54
55impl AsRef<[InlineNode]> for Attribution {
56    fn as_ref(&self) -> &[InlineNode] {
57        &self.0
58    }
59}
60
61impl Deref for Attribution {
62    type Target = [InlineNode];
63
64    fn deref(&self) -> &Self::Target {
65        &self.0
66    }
67}
68
69impl<'a> IntoIterator for &'a Attribution {
70    type Item = &'a InlineNode;
71    type IntoIter = std::slice::Iter<'a, InlineNode>;
72
73    fn into_iter(self) -> Self::IntoIter {
74        self.0.iter()
75    }
76}
77
78/// A citation title in a blockquote (the title of the cited work).
79///
80/// `CiteTitle` is a newtype wrapper around `Vec<InlineNode>` that provides
81/// convenient access to inline content.
82///
83/// # Serialization
84///
85/// Serializes as a plain JSON array of inline nodes.
86#[derive(Clone, Debug, Default, PartialEq)]
87#[non_exhaustive]
88pub struct CiteTitle(Vec<InlineNode>);
89
90impl Serialize for CiteTitle {
91    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
92    where
93        S: Serializer,
94    {
95        self.0.serialize(serializer)
96    }
97}
98
99impl CiteTitle {
100    /// Creates a new `CiteTitle` with the given inline content.
101    #[must_use]
102    pub fn new(inlines: Vec<InlineNode>) -> Self {
103        Self(inlines)
104    }
105
106    /// Returns `true` if the cite title has no content.
107    #[must_use]
108    pub fn is_empty(&self) -> bool {
109        self.0.is_empty()
110    }
111
112    /// Returns the number of inline nodes in the cite title.
113    #[must_use]
114    pub fn len(&self) -> usize {
115        self.0.len()
116    }
117}
118
119impl From<Vec<InlineNode>> for CiteTitle {
120    fn from(inlines: Vec<InlineNode>) -> Self {
121        Self(inlines)
122    }
123}
124
125impl AsRef<[InlineNode]> for CiteTitle {
126    fn as_ref(&self) -> &[InlineNode] {
127        &self.0
128    }
129}
130
131impl Deref for CiteTitle {
132    type Target = [InlineNode];
133
134    fn deref(&self) -> &Self::Target {
135        &self.0
136    }
137}
138
139impl<'a> IntoIterator for &'a CiteTitle {
140    type Item = &'a InlineNode;
141    type IntoIter = std::slice::Iter<'a, InlineNode>;
142
143    fn into_iter(self) -> Self::IntoIter {
144        self.0.iter()
145    }
146}