1use super::generics::{FromAttributes, ParseFrom};
2use crate::util::date::parse_date;
3use compact_str::CompactString;
4use jiff::Timestamp;
5use serde_json::Value;
6use std::collections::HashMap;
7use std::ops::Deref;
8use std::sync::Arc;
9
10pub type SmallString = CompactString;
28
29#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize)]
47#[serde(transparent)]
48pub struct Url(String);
49
50impl Url {
51 #[inline]
63 pub fn new(s: impl Into<String>) -> Self {
64 Self(s.into())
65 }
66
67 #[inline]
78 pub fn as_str(&self) -> &str {
79 &self.0
80 }
81
82 #[inline]
94 pub fn into_inner(self) -> String {
95 self.0
96 }
97}
98
99impl Deref for Url {
100 type Target = str;
101
102 #[inline]
103 fn deref(&self) -> &str {
104 &self.0
105 }
106}
107
108impl From<String> for Url {
109 #[inline]
110 fn from(s: String) -> Self {
111 Self(s)
112 }
113}
114
115impl From<&str> for Url {
116 #[inline]
117 fn from(s: &str) -> Self {
118 Self(s.to_string())
119 }
120}
121
122impl AsRef<str> for Url {
123 #[inline]
124 fn as_ref(&self) -> &str {
125 &self.0
126 }
127}
128
129impl std::fmt::Display for Url {
130 #[inline]
131 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
132 self.0.fmt(f)
133 }
134}
135
136impl PartialEq<str> for Url {
137 fn eq(&self, other: &str) -> bool {
138 self.0 == other
139 }
140}
141
142impl PartialEq<&str> for Url {
143 fn eq(&self, other: &&str) -> bool {
144 self.0 == *other
145 }
146}
147
148impl PartialEq<String> for Url {
149 fn eq(&self, other: &String) -> bool {
150 &self.0 == other
151 }
152}
153
154#[derive(Debug, Clone, PartialEq, Eq, Hash)]
172pub struct MimeType(Arc<str>);
173
174impl serde::Serialize for MimeType {
176 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
177 where
178 S: serde::Serializer,
179 {
180 serializer.serialize_str(&self.0)
181 }
182}
183
184impl<'de> serde::Deserialize<'de> for MimeType {
185 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
186 where
187 D: serde::Deserializer<'de>,
188 {
189 let s = <String as serde::Deserialize>::deserialize(deserializer)?;
190 Ok(Self::new(s))
191 }
192}
193
194impl MimeType {
195 #[inline]
206 pub fn new(s: impl AsRef<str>) -> Self {
207 Self(Arc::from(s.as_ref()))
208 }
209
210 #[inline]
221 pub fn as_str(&self) -> &str {
222 &self.0
223 }
224
225 pub const TEXT_HTML: &'static str = "text/html";
236
237 pub const TEXT_PLAIN: &'static str = "text/plain";
239
240 pub const APPLICATION_XML: &'static str = "application/xml";
242
243 pub const APPLICATION_JSON: &'static str = "application/json";
245}
246
247impl Default for MimeType {
248 #[inline]
249 fn default() -> Self {
250 Self(Arc::from(""))
251 }
252}
253
254impl Deref for MimeType {
255 type Target = str;
256
257 #[inline]
258 fn deref(&self) -> &str {
259 &self.0
260 }
261}
262
263impl From<String> for MimeType {
264 #[inline]
265 fn from(s: String) -> Self {
266 Self(Arc::from(s.as_str()))
267 }
268}
269
270impl From<&str> for MimeType {
271 #[inline]
272 fn from(s: &str) -> Self {
273 Self(Arc::from(s))
274 }
275}
276
277impl AsRef<str> for MimeType {
278 #[inline]
279 fn as_ref(&self) -> &str {
280 &self.0
281 }
282}
283
284impl std::fmt::Display for MimeType {
285 #[inline]
286 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
287 self.0.fmt(f)
288 }
289}
290
291impl PartialEq<str> for MimeType {
292 fn eq(&self, other: &str) -> bool {
293 &*self.0 == other
294 }
295}
296
297impl PartialEq<&str> for MimeType {
298 fn eq(&self, other: &&str) -> bool {
299 &*self.0 == *other
300 }
301}
302
303impl PartialEq<String> for MimeType {
304 fn eq(&self, other: &String) -> bool {
305 &*self.0 == other
306 }
307}
308
309#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, serde::Serialize, serde::Deserialize)]
323#[serde(transparent)]
324pub struct Email(String);
325
326impl Email {
327 #[inline]
338 pub fn new(s: impl Into<String>) -> Self {
339 Self(s.into())
340 }
341
342 #[inline]
353 pub fn as_str(&self) -> &str {
354 &self.0
355 }
356
357 #[inline]
369 pub fn into_inner(self) -> String {
370 self.0
371 }
372}
373
374impl Deref for Email {
375 type Target = str;
376
377 #[inline]
378 fn deref(&self) -> &str {
379 &self.0
380 }
381}
382
383impl From<String> for Email {
384 #[inline]
385 fn from(s: String) -> Self {
386 Self(s)
387 }
388}
389
390impl From<&str> for Email {
391 #[inline]
392 fn from(s: &str) -> Self {
393 Self(s.to_string())
394 }
395}
396
397impl AsRef<str> for Email {
398 #[inline]
399 fn as_ref(&self) -> &str {
400 &self.0
401 }
402}
403
404impl std::fmt::Display for Email {
405 #[inline]
406 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
407 self.0.fmt(f)
408 }
409}
410
411impl PartialEq<str> for Email {
412 fn eq(&self, other: &str) -> bool {
413 self.0 == other
414 }
415}
416
417impl PartialEq<&str> for Email {
418 fn eq(&self, other: &&str) -> bool {
419 self.0 == *other
420 }
421}
422
423impl PartialEq<String> for Email {
424 fn eq(&self, other: &String) -> bool {
425 &self.0 == other
426 }
427}
428
429#[derive(Debug, Clone, Default)]
431#[non_exhaustive]
432pub struct Link {
433 pub href: Url,
435 pub rel: Option<SmallString>,
438 pub link_type: Option<MimeType>,
440 pub title: Option<String>,
442 pub length: Option<String>,
444 pub hreflang: Option<SmallString>,
446 pub thr_count: Option<u32>,
448 pub thr_updated: Option<Timestamp>,
450 pub json_extensions: HashMap<String, Value>,
455}
456
457impl Link {
458 #[inline]
460 pub fn new(href: impl Into<Url>, rel: impl AsRef<str>) -> Self {
461 Self {
462 href: href.into(),
463 rel: Some(rel.as_ref().into()),
464 link_type: None,
465 title: None,
466 length: None,
467 hreflang: None,
468 thr_count: None,
469 thr_updated: None,
470 ..Self::default()
471 }
472 }
473
474 #[inline]
476 pub fn alternate(href: impl Into<Url>) -> Self {
477 Self::new(href, "alternate")
478 }
479
480 #[inline]
482 pub fn self_link(href: impl Into<Url>, mime_type: impl Into<MimeType>) -> Self {
483 Self {
484 href: href.into(),
485 rel: Some("self".into()),
486 link_type: Some(mime_type.into()),
487 title: None,
488 length: None,
489 hreflang: None,
490 thr_count: None,
491 thr_updated: None,
492 ..Self::default()
493 }
494 }
495
496 #[inline]
498 pub fn enclosure(href: impl Into<Url>, mime_type: Option<MimeType>) -> Self {
499 Self {
500 href: href.into(),
501 rel: Some("enclosure".into()),
502 link_type: mime_type,
503 title: None,
504 length: None,
505 hreflang: None,
506 thr_count: None,
507 thr_updated: None,
508 ..Self::default()
509 }
510 }
511
512 #[inline]
514 pub fn related(href: impl Into<Url>) -> Self {
515 Self::new(href, "related")
516 }
517
518 #[inline]
524 pub fn banner(href: impl Into<Url>) -> Self {
525 Self::new(href, "banner")
526 }
527
528 #[inline]
530 pub fn hub(href: impl Into<Url>) -> Self {
531 Self::new(href, "hub")
532 }
533
534 #[inline]
536 #[must_use]
537 pub fn with_type(mut self, mime_type: impl Into<MimeType>) -> Self {
538 self.link_type = Some(mime_type.into());
539 self
540 }
541}
542
543#[derive(Debug, Clone, Default)]
545#[non_exhaustive]
546pub struct Person {
547 pub name: Option<SmallString>,
549 pub email: Option<Email>,
551 pub uri: Option<String>,
553 pub avatar: Option<String>,
555 pub json_extensions: HashMap<String, Value>,
560}
561
562impl Person {
563 #[inline]
576 pub fn from_name(name: impl AsRef<str>) -> Self {
577 Self {
578 name: Some(name.as_ref().into()),
579 email: None,
580 uri: None,
581 avatar: None,
582 ..Self::default()
583 }
584 }
585
586 #[must_use]
590 pub fn flat_string(&self) -> Option<SmallString> {
591 match (&self.name, &self.email) {
592 (Some(name), Some(email)) => Some(format!("{name} ({email})").into()),
593 (Some(name), None) => Some(name.clone()),
594 (None, Some(email)) => Some(email.as_str().into()),
595 (None, None) => None,
596 }
597 }
598}
599
600#[derive(Debug, Clone)]
602#[non_exhaustive]
603pub struct Tag {
604 pub term: SmallString,
606 pub scheme: Option<SmallString>,
608 pub label: Option<SmallString>,
610}
611
612impl Tag {
613 #[inline]
615 pub fn new(term: impl AsRef<str>) -> Self {
616 Self {
617 term: term.as_ref().into(),
618 scheme: None,
619 label: None,
620 }
621 }
622}
623
624#[derive(Debug, Default, Clone, PartialEq, Eq)]
626#[non_exhaustive]
627pub struct Cloud {
628 pub domain: Option<String>,
630 pub port: Option<String>,
632 pub path: Option<String>,
634 pub register_procedure: Option<String>,
636 pub protocol: Option<String>,
638}
639
640#[derive(Debug, Default, Clone, PartialEq, Eq)]
642#[non_exhaustive]
643pub struct TextInput {
644 pub title: Option<String>,
646 pub description: Option<String>,
648 pub name: Option<String>,
650 pub link: Option<String>,
652}
653
654#[derive(Debug, Clone)]
656#[non_exhaustive]
657pub struct Image {
658 pub url: Url,
660 pub title: Option<String>,
662 pub link: Option<String>,
664 pub width: Option<u32>,
666 pub height: Option<u32>,
668 pub description: Option<String>,
670}
671
672impl Image {
673 #[must_use]
684 pub const fn new(url: Url) -> Self {
685 Self {
686 url,
687 title: None,
688 link: None,
689 width: None,
690 height: None,
691 description: None,
692 }
693 }
694}
695
696#[derive(Debug, Clone, Default)]
698#[non_exhaustive]
699pub struct Enclosure {
700 pub url: Url,
702 pub length: Option<String>,
704 pub enclosure_type: Option<MimeType>,
706 pub title: Option<String>,
708 pub duration: Option<String>,
710 pub json_extensions: HashMap<String, Value>,
715}
716
717impl Enclosure {
718 #[must_use]
729 pub fn new(url: Url) -> Self {
730 Self {
731 url,
732 ..Self::default()
733 }
734 }
735}
736
737#[derive(Debug, Clone)]
739#[non_exhaustive]
740pub struct Content {
741 pub value: String,
743 pub content_type: Option<MimeType>,
745 pub language: Option<SmallString>,
747 pub base: Option<String>,
749 pub src: Option<String>,
751}
752
753impl Content {
754 #[inline]
756 pub fn html(value: impl Into<String>) -> Self {
757 Self {
758 value: value.into(),
759 content_type: Some(MimeType::new(MimeType::TEXT_HTML)),
760 language: None,
761 base: None,
762 src: None,
763 }
764 }
765
766 #[inline]
768 pub fn plain(value: impl Into<String>) -> Self {
769 Self {
770 value: value.into(),
771 content_type: Some(MimeType::new(MimeType::TEXT_PLAIN)),
772 language: None,
773 base: None,
774 src: None,
775 }
776 }
777}
778
779#[derive(Debug, Clone, Copy, PartialEq, Eq)]
781#[expect(
782 clippy::exhaustive_enums,
783 reason = "the text types are fixed by the Atom RFC 4287 and matched exhaustively by callers"
784)]
785pub enum TextType {
786 Text,
788 Html,
790 Xhtml,
792}
793
794impl TextType {
795 #[must_use]
816 pub fn from_type_attr(raw: &str) -> Self {
817 match raw.trim().to_ascii_lowercase().as_str() {
818 "text" | "text/plain" => Self::Text,
819 "xhtml" | "application/xhtml+xml" => Self::Xhtml,
820 _ => Self::Html,
821 }
822 }
823}
824
825#[derive(Debug, Clone)]
827#[non_exhaustive]
828pub struct TextConstruct {
829 pub value: String,
831 pub content_type: TextType,
833 pub language: Option<SmallString>,
835 pub base: Option<String>,
837}
838
839impl TextConstruct {
840 #[inline]
842 pub fn text(value: impl Into<String>) -> Self {
843 Self {
844 value: value.into(),
845 content_type: TextType::Text,
846 language: None,
847 base: None,
848 }
849 }
850
851 #[inline]
853 pub fn html(value: impl Into<String>) -> Self {
854 Self {
855 value: value.into(),
856 content_type: TextType::Html,
857 language: None,
858 base: None,
859 }
860 }
861
862 #[inline]
864 #[must_use]
865 pub fn with_language(mut self, language: impl AsRef<str>) -> Self {
866 self.language = Some(language.as_ref().into());
867 self
868 }
869}
870
871#[derive(Debug, Clone)]
873#[non_exhaustive]
874pub struct Generator {
875 pub name: String,
877 pub href: Option<String>,
879 pub version: Option<SmallString>,
881}
882
883impl Generator {
884 #[must_use]
895 pub fn new(name: impl Into<String>) -> Self {
896 Self {
897 name: name.into(),
898 href: None,
899 version: None,
900 }
901 }
902}
903
904#[derive(Debug, Clone, Default)]
906#[non_exhaustive]
907pub struct Source {
908 pub title: Option<String>,
910 pub href: Option<String>,
912 pub link: Option<String>,
914 pub author: Option<String>,
916 pub id: Option<String>,
918 pub links: Vec<Link>,
920 pub updated: Option<Timestamp>,
922 pub updated_str: Option<String>,
924 pub rights: Option<String>,
926 pub guidislink: Option<bool>,
932}
933
934#[derive(Debug, Clone)]
936#[non_exhaustive]
937pub struct MediaThumbnail {
938 pub url: Url,
945 pub width: Option<String>,
947 pub height: Option<String>,
949 pub time: Option<String>,
953}
954
955impl MediaThumbnail {
956 #[must_use]
967 pub const fn new(url: Url) -> Self {
968 Self {
969 url,
970 width: None,
971 height: None,
972 time: None,
973 }
974 }
975}
976
977#[derive(Debug, Clone)]
979#[non_exhaustive]
980pub struct MediaContent {
981 pub url: Url,
988 pub content_type: Option<MimeType>,
990 pub medium: Option<String>,
992 pub filesize: Option<String>,
994 pub width: Option<String>,
996 pub height: Option<String>,
998 pub duration: Option<String>,
1000 pub bitrate: Option<String>,
1002 pub lang: Option<String>,
1004 pub channels: Option<String>,
1006 pub codec: Option<String>,
1008 pub expression: Option<String>,
1010 pub isdefault: Option<String>,
1012 pub samplingrate: Option<String>,
1014 pub framerate: Option<String>,
1016}
1017
1018impl MediaContent {
1019 #[must_use]
1030 pub const fn new(url: Url) -> Self {
1031 Self {
1032 url,
1033 content_type: None,
1034 medium: None,
1035 filesize: None,
1036 width: None,
1037 height: None,
1038 duration: None,
1039 bitrate: None,
1040 lang: None,
1041 channels: None,
1042 codec: None,
1043 expression: None,
1044 isdefault: None,
1045 samplingrate: None,
1046 framerate: None,
1047 }
1048 }
1049}
1050
1051#[derive(Debug, Clone, Default, PartialEq, Eq)]
1056#[non_exhaustive]
1057pub struct MediaRating {
1058 pub scheme: Option<String>,
1060 pub content: String,
1062}
1063
1064impl FromAttributes for Link {
1065 fn from_attributes<'a, I>(attrs: I, max_attr_length: usize) -> Option<Self>
1066 where
1067 I: Iterator<Item = quick_xml::events::attributes::Attribute<'a>>,
1068 {
1069 let mut href = None;
1070 let mut rel = None;
1071 let mut link_type = None;
1072 let mut title = None;
1073 let mut hreflang = None;
1074 let mut length = None;
1075 let mut thr_count = None;
1076 let mut thr_updated = None;
1077
1078 for attr in attrs {
1079 if attr.value.len() > max_attr_length {
1080 continue;
1081 }
1082 match attr.key.as_ref() {
1083 "href" => href = Some(attr.value.to_string()),
1084 "rel" => rel = Some(attr.value.to_string()),
1085 "type" => link_type = Some(attr.value.to_string()),
1086 "title" => title = Some(attr.value.to_string()),
1087 "hreflang" => hreflang = Some(attr.value.to_string()),
1088 "length" => length = Some(attr.value.to_string()),
1089 "thr:count" => {
1090 thr_count = attr.value.to_string().trim().parse::<u32>().ok();
1091 }
1092 "thr:updated" => {
1093 thr_updated = parse_date(attr.value.to_string().trim());
1094 }
1095 _ => {}
1096 }
1097 }
1098
1099 let rel_str: Option<SmallString> = rel
1100 .map(std::convert::Into::into)
1101 .or_else(|| Some("alternate".into()));
1102 let resolved_type = link_type
1103 .map(MimeType::new)
1104 .or_else(|| match rel_str.as_deref() {
1105 Some("self") => Some(MimeType::new("application/atom+xml")),
1106 _ => Some(MimeType::new("text/html")),
1107 });
1108
1109 href.map(|href| Self {
1110 href: Url::new(href),
1111 rel: rel_str,
1112 link_type: resolved_type,
1113 title,
1114 length,
1115 hreflang: hreflang.map(std::convert::Into::into),
1116 thr_count,
1117 thr_updated,
1118 ..Self::default()
1119 })
1120 }
1121}
1122
1123impl FromAttributes for Tag {
1124 fn from_attributes<'a, I>(attrs: I, max_attr_length: usize) -> Option<Self>
1125 where
1126 I: Iterator<Item = quick_xml::events::attributes::Attribute<'a>>,
1127 {
1128 let mut term = None;
1129 let mut scheme = None;
1130 let mut label = None;
1131
1132 for attr in attrs {
1133 if attr.value.len() > max_attr_length {
1134 continue;
1135 }
1136
1137 match attr.key.as_ref() {
1138 "term" => term = Some(attr.value.to_string()),
1139 "scheme" | "domain" => scheme = Some(attr.value.to_string()),
1140 "label" => label = Some(attr.value.to_string()),
1141 _ => {}
1142 }
1143 }
1144
1145 term.map(|term| Self {
1146 term: term.into(),
1147 scheme: scheme.map(std::convert::Into::into),
1148 label: label.map(std::convert::Into::into),
1149 })
1150 }
1151}
1152
1153impl FromAttributes for Enclosure {
1154 fn from_attributes<'a, I>(attrs: I, max_attr_length: usize) -> Option<Self>
1155 where
1156 I: Iterator<Item = quick_xml::events::attributes::Attribute<'a>>,
1157 {
1158 let mut url = None;
1159 let mut length = None;
1160 let mut enclosure_type = None;
1161
1162 for attr in attrs {
1163 if attr.value.len() > max_attr_length {
1164 continue;
1165 }
1166
1167 match attr.key.as_ref() {
1168 "url" => url = Some(attr.value.to_string()),
1169 "length" => length = Some(attr.value.to_string()),
1170 "type" => enclosure_type = Some(attr.value.to_string()),
1171 _ => {}
1172 }
1173 }
1174
1175 url.map(|url| Self {
1176 url: Url::new(url),
1177 length,
1178 enclosure_type: enclosure_type.map(MimeType::new),
1179 title: None,
1180 duration: None,
1181 ..Self::default()
1182 })
1183 }
1184}
1185
1186impl FromAttributes for MediaThumbnail {
1187 fn from_attributes<'a, I>(attrs: I, max_attr_length: usize) -> Option<Self>
1188 where
1189 I: Iterator<Item = quick_xml::events::attributes::Attribute<'a>>,
1190 {
1191 let mut url = None;
1192 let mut width = None;
1193 let mut height = None;
1194
1195 let mut time = None;
1196
1197 for attr in attrs {
1198 if attr.value.len() > max_attr_length {
1199 continue;
1200 }
1201
1202 match attr.key.as_ref() {
1203 "url" => url = Some(attr.value.to_string()),
1204 "width" => width = Some(attr.value.to_string()),
1205 "height" => height = Some(attr.value.to_string()),
1206 "time" => time = Some(attr.value.to_string()),
1207 _ => {}
1208 }
1209 }
1210
1211 url.map(|url| Self {
1212 url: Url::new(url),
1213 width,
1214 height,
1215 time,
1216 })
1217 }
1218}
1219
1220impl FromAttributes for MediaContent {
1221 fn from_attributes<'a, I>(attrs: I, max_attr_length: usize) -> Option<Self>
1222 where
1223 I: Iterator<Item = quick_xml::events::attributes::Attribute<'a>>,
1224 {
1225 let mut url = None;
1226 let mut content_type = None;
1227 let mut medium = None;
1228 let mut filesize = None;
1229 let mut width = None;
1230 let mut height = None;
1231 let mut duration = None;
1232 let mut bitrate = None;
1233 let mut lang = None;
1234 let mut channels = None;
1235 let mut codec = None;
1236 let mut expression = None;
1237 let mut isdefault = None;
1238 let mut samplingrate = None;
1239 let mut framerate = None;
1240
1241 for attr in attrs {
1242 if attr.value.len() > max_attr_length {
1243 continue;
1244 }
1245
1246 match attr.key.as_ref() {
1247 "url" => url = Some(attr.value.to_string()),
1248 "type" => content_type = Some(attr.value.to_string()),
1249 "medium" => medium = Some(attr.value.to_string()),
1250 "fileSize" => filesize = Some(attr.value.to_string()),
1251 "width" => width = Some(attr.value.to_string()),
1252 "height" => height = Some(attr.value.to_string()),
1253 "duration" => duration = Some(attr.value.to_string()),
1254 "bitrate" => bitrate = Some(attr.value.to_string()),
1255 "lang" => lang = Some(attr.value.to_string()),
1256 "channels" => channels = Some(attr.value.to_string()),
1257 "codec" => codec = Some(attr.value.to_string()),
1258 "expression" => expression = Some(attr.value.to_string()),
1259 "isDefault" => isdefault = Some(attr.value.to_string()),
1260 "samplingrate" => samplingrate = Some(attr.value.to_string()),
1261 "framerate" => framerate = Some(attr.value.to_string()),
1262 _ => {}
1263 }
1264 }
1265
1266 url.map(|url| Self {
1267 url: Url::new(url),
1268 content_type: content_type.map(MimeType::new),
1269 medium,
1270 filesize,
1271 width,
1272 height,
1273 duration,
1274 bitrate,
1275 lang,
1276 channels,
1277 codec,
1278 expression,
1279 isdefault,
1280 samplingrate,
1281 framerate,
1282 })
1283 }
1284}
1285
1286impl ParseFrom<&Value> for Person {
1289 fn parse_from(json: &Value) -> Option<Self> {
1293 json.as_object().map(|obj| Self {
1294 name: obj
1295 .get("name")
1296 .and_then(Value::as_str)
1297 .map(std::convert::Into::into),
1298 email: None, uri: obj.get("url").and_then(Value::as_str).map(String::from),
1300 avatar: obj.get("avatar").and_then(Value::as_str).map(String::from),
1301 ..Self::default()
1302 })
1303 }
1304}
1305
1306impl ParseFrom<&Value> for Enclosure {
1307 fn parse_from(json: &Value) -> Option<Self> {
1311 let obj = json.as_object()?;
1312 let url = obj.get("url").and_then(Value::as_str)?;
1313 Some(Self {
1314 url: Url::new(url),
1315 length: obj
1316 .get("size_in_bytes")
1317 .and_then(Value::as_u64)
1318 .map(|v| v.to_string()),
1319 enclosure_type: obj
1320 .get("mime_type")
1321 .and_then(Value::as_str)
1322 .map(MimeType::new),
1323 title: obj.get("title").and_then(Value::as_str).map(String::from),
1324 duration: obj
1325 .get("duration_in_seconds")
1326 .and_then(Value::as_u64)
1327 .map(|v| v.to_string()),
1328 ..Self::default()
1329 })
1330 }
1331}
1332
1333#[derive(Debug, Clone, Default)]
1335#[non_exhaustive]
1336pub struct MediaCredit {
1337 pub role: Option<String>,
1339 pub scheme: Option<String>,
1341 pub content: String,
1343}
1344
1345#[derive(Debug, Clone, Default)]
1347#[non_exhaustive]
1348pub struct MediaCopyright {
1349 pub url: Option<String>,
1351}
1352
1353#[cfg(test)]
1354mod tests {
1355 use super::*;
1356 use serde_json::json;
1357
1358 #[test]
1359 fn test_link_default() {
1360 let link = Link::default();
1361 assert!(link.href.is_empty());
1362 assert!(link.rel.is_none());
1363 }
1364
1365 #[test]
1366 fn test_link_builders() {
1367 let link = Link::alternate("https://example.com");
1368 assert_eq!(link.href, "https://example.com");
1369 assert_eq!(link.rel.as_deref(), Some("alternate"));
1370
1371 let link = Link::self_link("https://example.com/feed", "application/feed+json");
1372 assert_eq!(link.rel.as_deref(), Some("self"));
1373 assert_eq!(link.link_type.as_deref(), Some("application/feed+json"));
1374
1375 let link = Link::enclosure("https://example.com/audio.mp3", Some("audio/mpeg".into()));
1376 assert_eq!(link.rel.as_deref(), Some("enclosure"));
1377 assert_eq!(link.link_type.as_deref(), Some("audio/mpeg"));
1378
1379 let link = Link::related("https://other.com");
1380 assert_eq!(link.rel.as_deref(), Some("related"));
1381 }
1382
1383 #[test]
1384 fn test_tag_builder() {
1385 let tag = Tag::new("rust");
1386 assert_eq!(tag.term, "rust");
1387 assert!(tag.scheme.is_none());
1388 }
1389
1390 #[test]
1391 fn test_text_construct_builders() {
1392 let text = TextConstruct::text("Hello");
1393 assert_eq!(text.value, "Hello");
1394 assert_eq!(text.content_type, TextType::Text);
1395
1396 let html = TextConstruct::html("<p>Hello</p>");
1397 assert_eq!(html.content_type, TextType::Html);
1398
1399 let with_lang = TextConstruct::text("Hello").with_language("en");
1400 assert_eq!(with_lang.language.as_deref(), Some("en"));
1401 }
1402
1403 #[test]
1404 fn test_content_builders() {
1405 let html = Content::html("<p>Content</p>");
1406 assert_eq!(html.content_type.as_deref(), Some("text/html"));
1407
1408 let plain = Content::plain("Content");
1409 assert_eq!(plain.content_type.as_deref(), Some("text/plain"));
1410 }
1411
1412 #[test]
1413 fn test_person_default() {
1414 let person = Person::default();
1415 assert!(person.name.is_none());
1416 assert!(person.email.is_none());
1417 assert!(person.uri.is_none());
1418 }
1419
1420 #[test]
1421 fn test_person_parse_from_json() {
1422 let json = json!({"name": "John Doe", "url": "https://example.com"});
1423 let person = Person::parse_from(&json).unwrap();
1424 assert_eq!(person.name.as_deref(), Some("John Doe"));
1425 assert_eq!(person.uri.as_deref(), Some("https://example.com"));
1426 assert!(person.email.is_none());
1427 }
1428
1429 #[test]
1430 fn test_person_parse_from_empty_json() {
1431 let json = json!({});
1432 let person = Person::parse_from(&json).unwrap();
1433 assert!(person.name.is_none());
1434 }
1435
1436 #[test]
1437 fn test_enclosure_parse_from_json() {
1438 let json = json!({
1439 "url": "https://example.com/file.mp3",
1440 "mime_type": "audio/mpeg",
1441 "size_in_bytes": 12345
1442 });
1443 let enclosure = Enclosure::parse_from(&json).unwrap();
1444 assert_eq!(enclosure.url, "https://example.com/file.mp3");
1445 assert_eq!(enclosure.enclosure_type.as_deref(), Some("audio/mpeg"));
1446 assert_eq!(enclosure.length.as_deref(), Some("12345"));
1447 }
1448
1449 #[test]
1450 fn test_enclosure_parse_from_json_missing_url() {
1451 let json = json!({"mime_type": "audio/mpeg"});
1452 assert!(Enclosure::parse_from(&json).is_none());
1453 }
1454
1455 #[test]
1456 fn test_text_type_equality() {
1457 assert_eq!(TextType::Text, TextType::Text);
1458 assert_ne!(TextType::Text, TextType::Html);
1459 }
1460
1461 #[test]
1464 fn test_url_new() {
1465 let url = Url::new("https://example.com");
1466 assert_eq!(url.as_str(), "https://example.com");
1467 }
1468
1469 #[test]
1470 fn test_url_from_string() {
1471 let url: Url = String::from("https://example.com").into();
1472 assert_eq!(url.as_str(), "https://example.com");
1473 }
1474
1475 #[test]
1476 fn test_url_from_str() {
1477 let url: Url = "https://example.com".into();
1478 assert_eq!(url.as_str(), "https://example.com");
1479 }
1480
1481 #[test]
1482 fn test_url_deref() {
1483 let url = Url::new("https://example.com");
1484 assert_eq!(url.len(), 19);
1486 assert!(url.starts_with("https://"));
1487 }
1488
1489 #[test]
1490 fn test_url_into_inner() {
1491 let url = Url::new("https://example.com");
1492 let inner = url.into_inner();
1493 assert_eq!(inner, "https://example.com");
1494 }
1495
1496 #[test]
1497 fn test_url_default() {
1498 let url = Url::default();
1499 assert_eq!(url.as_str(), "");
1500 }
1501
1502 #[test]
1503 fn test_url_clone() {
1504 let url1 = Url::new("https://example.com");
1505 let url2 = url1.clone();
1506 assert_eq!(url1, url2);
1507 }
1508
1509 #[test]
1510 fn test_mime_type_new() {
1511 let mime = MimeType::new("text/html");
1512 assert_eq!(mime.as_str(), "text/html");
1513 }
1514
1515 #[test]
1516 fn test_mime_type_from_string() {
1517 let mime: MimeType = String::from("application/json").into();
1518 assert_eq!(mime.as_str(), "application/json");
1519 }
1520
1521 #[test]
1522 fn test_mime_type_from_str() {
1523 let mime: MimeType = "text/plain".into();
1524 assert_eq!(mime.as_str(), "text/plain");
1525 }
1526
1527 #[test]
1528 fn test_mime_type_deref() {
1529 let mime = MimeType::new("text/html");
1530 assert_eq!(mime.len(), 9);
1531 assert!(mime.starts_with("text/"));
1532 }
1533
1534 #[test]
1535 fn test_mime_type_default() {
1536 let mime = MimeType::default();
1537 assert_eq!(mime.as_str(), "");
1538 }
1539
1540 #[test]
1541 fn test_mime_type_clone() {
1542 let mime1 = MimeType::new("application/xml");
1543 let mime2 = mime1.clone();
1544 assert_eq!(mime1, mime2);
1545 }
1547
1548 #[test]
1549 fn test_mime_type_constants() {
1550 assert_eq!(MimeType::TEXT_HTML, "text/html");
1551 assert_eq!(MimeType::TEXT_PLAIN, "text/plain");
1552 assert_eq!(MimeType::APPLICATION_XML, "application/xml");
1553 assert_eq!(MimeType::APPLICATION_JSON, "application/json");
1554 }
1555
1556 #[test]
1557 fn test_email_new() {
1558 let email = Email::new("user@example.com");
1559 assert_eq!(email.as_str(), "user@example.com");
1560 }
1561
1562 #[test]
1563 fn test_email_from_string() {
1564 let email: Email = String::from("user@example.com").into();
1565 assert_eq!(email.as_str(), "user@example.com");
1566 }
1567
1568 #[test]
1569 fn test_email_from_str() {
1570 let email: Email = "user@example.com".into();
1571 assert_eq!(email.as_str(), "user@example.com");
1572 }
1573
1574 #[test]
1575 fn test_email_deref() {
1576 let email = Email::new("user@example.com");
1577 assert_eq!(email.len(), 16);
1578 assert!(email.contains('@'));
1579 }
1580
1581 #[test]
1582 fn test_email_into_inner() {
1583 let email = Email::new("user@example.com");
1584 let inner = email.into_inner();
1585 assert_eq!(inner, "user@example.com");
1586 }
1587
1588 #[test]
1589 fn test_email_default() {
1590 let email = Email::default();
1591 assert_eq!(email.as_str(), "");
1592 }
1593
1594 #[test]
1595 fn test_email_clone() {
1596 let email1 = Email::new("user@example.com");
1597 let email2 = email1.clone();
1598 assert_eq!(email1, email2);
1599 }
1600}