vimwiki_core/lang/elements/blocks/inline/
comments.rs1use crate::StrictEq;
2use derive_more::{
3 AsRef, Constructor, Deref, DerefMut, Display, From, Index, IndexMut,
4 IntoIterator, IsVariant,
5};
6use serde::{Deserialize, Serialize};
7use std::{borrow::Cow, iter::FromIterator};
8
9#[derive(
10 Clone,
11 Debug,
12 Display,
13 Hash,
14 From,
15 Eq,
16 PartialEq,
17 IsVariant,
18 Serialize,
19 Deserialize,
20)]
21pub enum Comment<'a> {
22 Line(LineComment<'a>),
23 MultiLine(MultiLineComment<'a>),
24}
25
26impl Comment<'_> {
27 pub fn to_borrowed(&self) -> Comment {
28 match self {
29 Self::Line(x) => Comment::from(x.as_borrowed()),
30 Self::MultiLine(x) => Comment::from(x.to_borrowed()),
31 }
32 }
33
34 pub fn into_owned(self) -> Comment<'static> {
35 match self {
36 Self::Line(x) => Comment::from(x.into_owned()),
37 Self::MultiLine(x) => Comment::from(x.into_owned()),
38 }
39 }
40}
41
42impl<'a> StrictEq for Comment<'a> {
43 #[inline]
45 fn strict_eq(&self, other: &Self) -> bool {
46 self == other
47 }
48}
49
50#[derive(
51 AsRef,
52 Clone,
53 Constructor,
54 Debug,
55 Display,
56 From,
57 Eq,
58 PartialEq,
59 Hash,
60 Serialize,
61 Deserialize,
62)]
63#[as_ref(forward)]
64#[display(fmt = "{}", "_0.trim()")]
65pub struct LineComment<'a>(
66 Cow<'a, str>,
68);
69
70impl<'a> LineComment<'a> {
71 pub fn as_str(&self) -> &str {
73 self.0.as_ref()
74 }
75}
76
77impl LineComment<'_> {
78 pub fn as_borrowed(&self) -> LineComment {
79 use self::Cow::*;
80
81 let inner = match &self.0 {
82 Borrowed(x) => *x,
83 Owned(x) => x.as_str(),
84 };
85
86 LineComment(Cow::Borrowed(inner))
87 }
88
89 pub fn into_owned(self) -> LineComment<'static> {
90 LineComment(Cow::from(self.0.into_owned()))
91 }
92}
93
94impl<'a> From<&'a str> for LineComment<'a> {
95 fn from(s: &'a str) -> Self {
96 Self::new(Cow::Borrowed(s))
97 }
98}
99
100impl<'a> From<String> for LineComment<'a> {
101 fn from(s: String) -> Self {
102 Self::new(Cow::Owned(s))
103 }
104}
105
106impl<'a> StrictEq for LineComment<'a> {
107 #[inline]
109 fn strict_eq(&self, other: &Self) -> bool {
110 self == other
111 }
112}
113
114#[derive(
115 AsRef,
116 Clone,
117 Constructor,
118 Debug,
119 Deref,
120 DerefMut,
121 Display,
122 Eq,
123 PartialEq,
124 Hash,
125 Index,
126 IndexMut,
127 IntoIterator,
128 Serialize,
129 Deserialize,
130)]
131#[as_ref(forward)]
132#[display(fmt = "{}", "_0.join(\"\n\")")]
133#[into_iterator(owned, ref, ref_mut)]
134pub struct MultiLineComment<'a>(
135 Vec<Cow<'a, str>>,
137);
138
139impl MultiLineComment<'_> {
140 pub fn to_borrowed(&self) -> MultiLineComment {
141 use self::Cow::*;
142
143 let inner = self
144 .0
145 .iter()
146 .map(|x| {
147 Cow::Borrowed(match x {
148 Borrowed(x) => *x,
149 Owned(x) => x.as_str(),
150 })
151 })
152 .collect();
153
154 MultiLineComment(inner)
155 }
156
157 pub fn into_owned(self) -> MultiLineComment<'static> {
158 let inner = self
159 .0
160 .into_iter()
161 .map(|x| Cow::from(x.into_owned()))
162 .collect();
163
164 MultiLineComment(inner)
165 }
166}
167
168impl<'a> From<&'a str> for MultiLineComment<'a> {
169 fn from(s: &'a str) -> Self {
170 std::iter::once(s).collect()
171 }
172}
173
174impl<'a> From<String> for MultiLineComment<'a> {
175 fn from(s: String) -> Self {
176 std::iter::once(s).collect()
177 }
178}
179
180impl<'a> FromIterator<&'a str> for MultiLineComment<'a> {
181 fn from_iter<I: IntoIterator<Item = &'a str>>(iter: I) -> Self {
184 iter.into_iter().map(Cow::Borrowed).collect()
185 }
186}
187
188impl FromIterator<String> for MultiLineComment<'static> {
189 fn from_iter<I: IntoIterator<Item = String>>(iter: I) -> Self {
192 iter.into_iter().map(Cow::Owned).collect()
193 }
194}
195
196impl<'a> FromIterator<Cow<'a, str>> for MultiLineComment<'a> {
197 fn from_iter<I: IntoIterator<Item = Cow<'a, str>>>(iter: I) -> Self {
200 Self::new(iter.into_iter().collect())
201 }
202}
203
204impl<'a> StrictEq for MultiLineComment<'a> {
205 #[inline]
207 fn strict_eq(&self, other: &Self) -> bool {
208 self == other
209 }
210}