use-metadata 0.0.1

Page metadata and social preview primitives for RustUse presence utilities
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
#![forbid(unsafe_code)]
#![doc = include_str!("../README.md")]

use core::{fmt, str::FromStr};
use std::error::Error;

fn non_empty(value: impl AsRef<str>, field: &'static str) -> Result<String, MetadataValueError> {
    let trimmed = value.as_ref().trim();
    if trimmed.is_empty() {
        Err(MetadataValueError::Empty { field })
    } else {
        Ok(trimmed.to_string())
    }
}

fn is_http_url(value: &str) -> bool {
    let lower = value.to_ascii_lowercase();
    lower.starts_with("https://") || lower.starts_with("http://")
}

/// Error returned by metadata primitive constructors.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum MetadataValueError {
    /// The supplied value was empty after trimming whitespace.
    Empty { field: &'static str },
    /// The URL did not look like an HTTP or HTTPS URL.
    InvalidUrl,
    /// Image dimensions must be non-zero.
    InvalidImageDimensions,
}

impl fmt::Display for MetadataValueError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Empty { field } => write!(formatter, "{field} cannot be empty"),
            Self::InvalidUrl => {
                formatter.write_str("metadata URL must start with http:// or https://")
            },
            Self::InvalidImageDimensions => {
                formatter.write_str("image dimensions must be non-zero")
            },
        }
    }
}

impl Error for MetadataValueError {}

/// Page metadata title.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct MetadataTitle(String);

impl MetadataTitle {
    /// Creates a metadata title.
    ///
    /// # Errors
    ///
    /// Returns [`MetadataValueError::Empty`] when the title is empty.
    pub fn new(value: impl AsRef<str>) -> Result<Self, MetadataValueError> {
        non_empty(value, "metadata title").map(Self)
    }

    /// Returns the title text.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl AsRef<str> for MetadataTitle {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl fmt::Display for MetadataTitle {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.as_str())
    }
}

impl FromStr for MetadataTitle {
    type Err = MetadataValueError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        Self::new(value)
    }
}

/// Page metadata description.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct MetadataDescription(String);

impl MetadataDescription {
    /// Creates a metadata description.
    ///
    /// # Errors
    ///
    /// Returns [`MetadataValueError::Empty`] when the description is empty.
    pub fn new(value: impl AsRef<str>) -> Result<Self, MetadataValueError> {
        non_empty(value, "metadata description").map(Self)
    }

    /// Returns the description text.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl AsRef<str> for MetadataDescription {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl fmt::Display for MetadataDescription {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.as_str())
    }
}

/// Open Graph type label.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum OpenGraphType {
    /// Generic website.
    Website,
    /// Article surface.
    Article,
    /// Product surface.
    Product,
    /// Profile surface.
    Profile,
    /// Local business surface.
    LocalBusiness,
}

impl OpenGraphType {
    /// Returns the Open Graph type label.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Website => "website",
            Self::Article => "article",
            Self::Product => "product",
            Self::Profile => "profile",
            Self::LocalBusiness => "business.business",
        }
    }
}

/// Twitter card kind label.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum TwitterCardKind {
    /// Summary card.
    Summary,
    /// Summary card with a large image.
    SummaryLargeImage,
    /// App card.
    App,
    /// Player card.
    Player,
}

impl TwitterCardKind {
    /// Returns the card kind label.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Summary => "summary",
            Self::SummaryLargeImage => "summary_large_image",
            Self::App => "app",
            Self::Player => "player",
        }
    }
}

/// Open Graph image metadata.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct OpenGraphImage {
    url: String,
    alt: Option<String>,
    width: Option<u32>,
    height: Option<u32>,
}

impl OpenGraphImage {
    /// Creates an Open Graph image from a URL-like string.
    ///
    /// # Errors
    ///
    /// Returns [`MetadataValueError::InvalidUrl`] when the URL shape is unsupported.
    pub fn new(value: impl AsRef<str>) -> Result<Self, MetadataValueError> {
        let url = non_empty(value, "Open Graph image URL")?;
        if !is_http_url(&url) {
            return Err(MetadataValueError::InvalidUrl);
        }

        Ok(Self {
            url,
            alt: None,
            width: None,
            height: None,
        })
    }

    /// Sets alternative text.
    ///
    /// # Errors
    ///
    /// Returns [`MetadataValueError::Empty`] when the alt text is empty.
    pub fn with_alt(mut self, alt: impl AsRef<str>) -> Result<Self, MetadataValueError> {
        self.alt = Some(non_empty(alt, "Open Graph image alt text")?);
        Ok(self)
    }

    /// Sets image dimensions.
    ///
    /// # Errors
    ///
    /// Returns [`MetadataValueError::InvalidImageDimensions`] when either dimension is zero.
    pub fn with_dimensions(mut self, width: u32, height: u32) -> Result<Self, MetadataValueError> {
        if width == 0 || height == 0 {
            return Err(MetadataValueError::InvalidImageDimensions);
        }
        self.width = Some(width);
        self.height = Some(height);
        Ok(self)
    }

    /// Returns the image URL.
    #[must_use]
    pub fn url(&self) -> &str {
        &self.url
    }
}

/// Social preview metadata.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SocialPreview {
    title: MetadataTitle,
    description: MetadataDescription,
    image: Option<OpenGraphImage>,
    open_graph_type: OpenGraphType,
    twitter_card: TwitterCardKind,
}

impl SocialPreview {
    /// Creates a social preview with default website/summary labels.
    #[must_use]
    pub const fn new(title: MetadataTitle, description: MetadataDescription) -> Self {
        Self {
            title,
            description,
            image: None,
            open_graph_type: OpenGraphType::Website,
            twitter_card: TwitterCardKind::Summary,
        }
    }

    /// Sets the preview image.
    #[must_use]
    pub fn with_image(mut self, image: OpenGraphImage) -> Self {
        self.image = Some(image);
        self
    }

    /// Sets the Open Graph type.
    #[must_use]
    pub const fn with_open_graph_type(mut self, value: OpenGraphType) -> Self {
        self.open_graph_type = value;
        self
    }

    /// Sets the Twitter card kind.
    #[must_use]
    pub const fn with_twitter_card(mut self, value: TwitterCardKind) -> Self {
        self.twitter_card = value;
        self
    }

    /// Returns the Open Graph type.
    #[must_use]
    pub const fn open_graph_type(&self) -> OpenGraphType {
        self.open_graph_type
    }
}

/// Page metadata for external surfaces.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PageMetadata {
    title: MetadataTitle,
    description: MetadataDescription,
    canonical_url: Option<String>,
    robots: Option<String>,
    social_preview: Option<SocialPreview>,
}

impl PageMetadata {
    /// Creates page metadata from a title and description.
    #[must_use]
    pub const fn new(title: MetadataTitle, description: MetadataDescription) -> Self {
        Self {
            title,
            description,
            canonical_url: None,
            robots: None,
            social_preview: None,
        }
    }

    /// Sets a canonical URL label.
    #[must_use]
    pub fn with_canonical_url(mut self, url: impl Into<String>) -> Self {
        self.canonical_url = Some(url.into());
        self
    }

    /// Sets robots meta content.
    #[must_use]
    pub fn with_robots(mut self, content: impl Into<String>) -> Self {
        self.robots = Some(content.into());
        self
    }

    /// Sets social preview metadata.
    #[must_use]
    pub fn with_social_preview(mut self, preview: SocialPreview) -> Self {
        self.social_preview = Some(preview);
        self
    }

    /// Returns the title.
    #[must_use]
    pub const fn title(&self) -> &MetadataTitle {
        &self.title
    }

    /// Returns the description.
    #[must_use]
    pub const fn description(&self) -> &MetadataDescription {
        &self.description
    }

    /// Returns the canonical URL label.
    #[must_use]
    pub fn canonical_url(&self) -> Option<&str> {
        self.canonical_url.as_deref()
    }

    /// Returns robots meta content.
    #[must_use]
    pub fn robots(&self) -> Option<&str> {
        self.robots.as_deref()
    }

    /// Returns social preview metadata.
    #[must_use]
    pub const fn social_preview(&self) -> Option<&SocialPreview> {
        self.social_preview.as_ref()
    }
}

#[cfg(test)]
mod tests {
    use super::{
        MetadataDescription, MetadataTitle, OpenGraphImage, OpenGraphType, PageMetadata,
        SocialPreview, TwitterCardKind,
    };

    #[test]
    fn validates_metadata_strings() {
        assert!(MetadataTitle::new("Example").is_ok());
        assert!(MetadataDescription::new(" ").is_err());
    }

    #[test]
    fn builds_open_graph_images() {
        let image = OpenGraphImage::new("https://example.com/image.png")
            .unwrap()
            .with_alt("Preview image")
            .unwrap()
            .with_dimensions(1200, 630)
            .unwrap();

        assert_eq!(image.url(), "https://example.com/image.png");
        assert!(OpenGraphImage::new("/image.png").is_err());
    }

    #[test]
    fn composes_page_metadata() {
        let preview = SocialPreview::new(
            MetadataTitle::new("Example").unwrap(),
            MetadataDescription::new("Example description").unwrap(),
        )
        .with_open_graph_type(OpenGraphType::Article)
        .with_twitter_card(TwitterCardKind::SummaryLargeImage);
        let metadata = PageMetadata::new(
            MetadataTitle::new("Example").unwrap(),
            MetadataDescription::new("Example description").unwrap(),
        )
        .with_canonical_url("https://example.com/")
        .with_robots("index,follow")
        .with_social_preview(preview);

        assert_eq!(metadata.robots(), Some("index,follow"));
        assert_eq!(
            metadata.social_preview().unwrap().open_graph_type(),
            OpenGraphType::Article
        );
    }
}