microformats-types 0.15.0

A representation of the known objects of Microformats
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! Core types for Microformats2 parsing.
//!
//! This crate provides the fundamental data structures for representing
//! Microformats2 parsed documents, items, properties, and values.
//!
//! # Features
//!
//! - `metaformats` - Enable metaformats backcompat support for Open Graph and Twitter Cards
//! - `debug_flow` - Enable source tracking for debugging parsed values
//! - `per_element_lang` - Enable per-element language tracking for TextValue and UrlValue

pub mod class;
#[cfg(feature = "debug_flow")]
mod debug_types;
pub mod document;
pub mod error;
pub mod item;
pub mod property_value;
/// Link relation types.
pub mod relation;
pub mod temporal;
pub mod traits;

pub use class::{Class, KnownClass};
pub use document::Document;
pub use error::Error;
pub use item::{Item, Items, ValueKind};
pub use property_value::{
    Fragment, Image, NodeList, Properties, PropertyValue, PropertyWithMetadata,
};
pub use relation::{Relation, Relations};
pub use traits::{FindItemById, FindItemByProperty, FindItemByUrl, LanguageFilter};

#[cfg(feature = "debug_flow")]
pub use debug_types::*;

/// Alias for [`Item`], representing a parsed microformat.
pub type Microformat = Item;

pub use url::Url;

#[cfg(test)]
mod test;

/// A text value with optional language information.
///
/// When the `per_element_lang` feature is enabled, this struct can carry
/// a language tag for the text. Without the feature, it serializes
/// transparently as a plain string.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct TextValue {
    value: String,
    #[cfg(feature = "per_element_lang")]
    lang: Option<String>,
}

impl TextValue {
    /// Creates a new TextValue from a string.
    pub fn new(value: String) -> Self {
        Self {
            value,
            #[cfg(feature = "per_element_lang")]
            lang: None,
        }
    }

    /// Creates a new TextValue with a language tag.
    #[cfg(feature = "per_element_lang")]
    pub fn with_lang(value: String, lang: impl Into<String>) -> Self {
        Self {
            value,
            lang: Some(lang.into()),
        }
    }

    /// Returns the language tag if present.
    #[cfg(feature = "per_element_lang")]
    pub fn lang(&self) -> Option<&str> {
        self.lang.as_deref()
    }
}

impl std::ops::Deref for TextValue {
    type Target = String;
    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

impl From<String> for TextValue {
    fn from(value: String) -> Self {
        Self::new(value)
    }
}

impl From<&str> for TextValue {
    fn from(value: &str) -> Self {
        Self::new(value.to_string())
    }
}

impl std::fmt::Display for TextValue {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.value.fmt(f)
    }
}

// Serialize TextValue transparently as a string when per_element_lang is NOT enabled
#[cfg(not(feature = "per_element_lang"))]
impl serde::Serialize for TextValue {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(&self.value)
    }
}

// Serialize TextValue - as plain string when no lang, as object when lang is present
#[cfg(feature = "per_element_lang")]
impl serde::Serialize for TextValue {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        if let Some(ref lang) = self.lang {
            // Has language: serialize as object
            use serde::ser::SerializeStruct;
            let mut s = serializer.serialize_struct("TextValue", 2)?;
            s.serialize_field("value", &self.value)?;
            s.serialize_field("lang", lang)?;
            s.end()
        } else {
            // No language: serialize as plain string for backward compatibility
            serializer.serialize_str(&self.value)
        }
    }
}

// Deserialize TextValue from a string when per_element_lang is NOT enabled
#[cfg(not(feature = "per_element_lang"))]
impl<'de> serde::Deserialize<'de> for TextValue {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        Ok(TextValue::new(s))
    }
}

// Deserialize TextValue from either a string or an object when per_element_lang IS enabled
#[cfg(feature = "per_element_lang")]
impl<'de> serde::Deserialize<'de> for TextValue {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        use serde::de::{self, MapAccess, Visitor};
        use std::fmt;

        struct TextValueVisitor;

        impl<'de> Visitor<'de> for TextValueVisitor {
            type Value = TextValue;

            fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                formatter
                    .write_str("a string or an object with \"value\" and optional \"lang\" fields")
            }

            fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                Ok(TextValue::new(value.to_string()))
            }

            fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                Ok(TextValue::new(value))
            }

            fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
            where
                M: MapAccess<'de>,
            {
                let mut value: Option<String> = None;
                let mut lang: Option<String> = None;

                while let Some(key) = map.next_key()? {
                    match key {
                        "value" => {
                            if value.is_some() {
                                return Err(de::Error::duplicate_field("value"));
                            }
                            value = Some(map.next_value()?);
                        }
                        "lang" => {
                            if lang.is_some() {
                                return Err(de::Error::duplicate_field("lang"));
                            }
                            lang = Some(map.next_value()?);
                        }
                        _ => {
                            return Err(de::Error::unknown_field(key, &["value", "lang"]));
                        }
                    }
                }

                let value = value.ok_or_else(|| de::Error::missing_field("value"))?;
                Ok(TextValue { value, lang })
            }
        }

        deserializer.deserialize_any(TextValueVisitor)
    }
}

/// A URL value with optional language information.
///
/// When the `per_element_lang` feature is enabled, this struct can carry
/// a language tag for the URL. Without the feature, it serializes
/// transparently as a plain URL string.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct UrlValue {
    value: url::Url,
    #[cfg(feature = "per_element_lang")]
    lang: Option<String>,
}

impl UrlValue {
    /// Creates a new UrlValue from a URL.
    pub fn new(value: url::Url) -> Self {
        Self {
            value,
            #[cfg(feature = "per_element_lang")]
            lang: None,
        }
    }

    /// Creates a new UrlValue with a language tag.
    #[cfg(feature = "per_element_lang")]
    pub fn with_lang(value: url::Url, lang: impl Into<String>) -> Self {
        Self {
            value,
            lang: Some(lang.into()),
        }
    }

    /// Returns the language tag if present.
    #[cfg(feature = "per_element_lang")]
    pub fn lang(&self) -> Option<&str> {
        self.lang.as_deref()
    }
}

impl std::ops::Deref for UrlValue {
    type Target = url::Url;
    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

impl From<url::Url> for UrlValue {
    fn from(value: url::Url) -> Self {
        Self::new(value)
    }
}

impl std::fmt::Display for UrlValue {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.value.fmt(f)
    }
}

// Serialize UrlValue transparently as a URL string when per_element_lang is NOT enabled
#[cfg(not(feature = "per_element_lang"))]
impl serde::Serialize for UrlValue {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(self.value.as_str())
    }
}

// Serialize UrlValue - as plain string when no lang, as object when lang is present
#[cfg(feature = "per_element_lang")]
impl serde::Serialize for UrlValue {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        if let Some(ref lang) = self.lang {
            // Has language: serialize as object
            use serde::ser::SerializeStruct;
            let mut s = serializer.serialize_struct("UrlValue", 2)?;
            s.serialize_field("value", self.value.as_str())?;
            s.serialize_field("lang", lang)?;
            s.end()
        } else {
            // No language: serialize as plain string for backward compatibility
            serializer.serialize_str(self.value.as_str())
        }
    }
}

// Deserialize UrlValue from a URL string when per_element_lang is NOT enabled
#[cfg(not(feature = "per_element_lang"))]
impl<'de> serde::Deserialize<'de> for UrlValue {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        let url = url::Url::parse(&s).map_err(serde::de::Error::custom)?;
        Ok(UrlValue::new(url))
    }
}

// Deserialize UrlValue from either a URL string or an object when per_element_lang IS enabled
#[cfg(feature = "per_element_lang")]
impl<'de> serde::Deserialize<'de> for UrlValue {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        use serde::de::{self, MapAccess, Visitor};
        use std::fmt;

        struct UrlValueVisitor;

        impl<'de> Visitor<'de> for UrlValueVisitor {
            type Value = UrlValue;

            fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                formatter.write_str(
                    "a URL string or an object with \"value\" and optional \"lang\" fields",
                )
            }

            fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                let url = url::Url::parse(value).map_err(de::Error::custom)?;
                Ok(UrlValue::new(url))
            }

            fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                let url = url::Url::parse(&value).map_err(de::Error::custom)?;
                Ok(UrlValue::new(url))
            }

            fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
            where
                M: MapAccess<'de>,
            {
                let mut value: Option<String> = None;
                let mut lang: Option<String> = None;

                while let Some(key) = map.next_key()? {
                    match key {
                        "value" => {
                            if value.is_some() {
                                return Err(de::Error::duplicate_field("value"));
                            }
                            value = Some(map.next_value()?);
                        }
                        "lang" => {
                            if lang.is_some() {
                                return Err(de::Error::duplicate_field("lang"));
                            }
                            lang = Some(map.next_value()?);
                        }
                        _ => {
                            return Err(de::Error::unknown_field(key, &["value", "lang"]));
                        }
                    }
                }

                let value_str = value.ok_or_else(|| de::Error::missing_field("value"))?;
                let url = url::Url::parse(&value_str).map_err(de::Error::custom)?;
                Ok(UrlValue { value: url, lang })
            }
        }

        deserializer.deserialize_any(UrlValueVisitor)
    }
}

#[cfg(test)]
mod test_per_element_lang {
    use super::*;

    #[test]
    #[cfg(feature = "per_element_lang")]
    fn text_value_without_lang_serializes_as_string() {
        let tv = TextValue::new("hello".to_string());
        // Without lang, it should serialize as a plain string for backward compatibility
        assert_eq!(serde_json::to_string(&tv).unwrap(), r#""hello""#);
    }

    #[test]
    #[cfg(feature = "per_element_lang")]
    fn text_value_with_lang_serializes_as_object() {
        let tv = TextValue::with_lang("hello".to_string(), "en");
        let json = serde_json::to_string(&tv).unwrap();
        assert!(json.contains(r#""value":"hello""#));
        assert!(json.contains(r#""lang":"en""#));
    }

    #[test]
    #[cfg(feature = "per_element_lang")]
    fn text_value_deserializes_string() {
        let tv: TextValue = serde_json::from_str("\"hello\"").unwrap();
        assert_eq!(&*tv, "hello");
        assert_eq!(tv.lang(), None);
    }

    #[test]
    #[cfg(feature = "per_element_lang")]
    fn text_value_deserializes_object() {
        let tv: TextValue = serde_json::from_str("{\"value\":\"hello\",\"lang\":\"en\"}").unwrap();
        assert_eq!(&*tv, "hello");
        assert_eq!(tv.lang(), Some("en"));
    }

    #[test]
    #[cfg(feature = "per_element_lang")]
    fn url_value_without_lang_serializes_as_string() {
        let uv = UrlValue::new("https://example.com".parse().unwrap());
        // Without lang, it should serialize as a plain string for backward compatibility
        assert_eq!(
            serde_json::to_string(&uv).unwrap(),
            r#""https://example.com/""#
        );
    }

    #[test]
    #[cfg(feature = "per_element_lang")]
    fn url_value_with_lang_serializes_as_object() {
        let uv = UrlValue::with_lang("https://example.com".parse().unwrap(), "en");
        let json = serde_json::to_string(&uv).unwrap();
        assert!(json.contains(r#""value":"https://example.com/""#));
        assert!(json.contains(r#""lang":"en""#));
    }

    #[test]
    #[cfg(feature = "per_element_lang")]
    fn text_value_deref_works() {
        let tv = TextValue::new("hello".to_string());
        assert_eq!(tv.len(), 5); // Deref to String
        assert_eq!(&tv[..], "hello");
    }

    #[test]
    #[cfg(feature = "per_element_lang")]
    fn url_value_deref_works() {
        let uv = UrlValue::new("https://example.com/path".parse().unwrap());
        assert_eq!(uv.path(), "/path"); // Deref to Url
    }
}