cabin/html/elements/
source.rs

1use std::borrow::Cow;
2
3use super::global::Global;
4use super::img::{Sizes, SrcSet};
5use super::input::{Height, Width};
6use super::link::Type;
7use super::meta::Media;
8use super::script::Src;
9use crate::html::attributes::{Attributes, WithAttribute};
10use crate::html::Html;
11
12/// The `source` element allows authors to specify multiple alternative source sets for [super::img]
13/// elements or multiple alternative media resources for media elements. It does not represent
14/// anything on its own.
15pub fn source() -> Html<marker::Source, (), ()> {
16    Html::new("source", (), ())
17}
18
19pub mod marker {
20    pub struct Source;
21}
22
23impl<A: Attributes, V: 'static> Source for Html<marker::Source, A, V> {}
24impl<A: Attributes, V: 'static> Global for Html<marker::Source, A, V> {}
25
26/// An `source` element represents an image.
27pub trait Source: WithAttribute {
28    /// Type of embedded resource.
29    fn r#type(self, r#type: impl Into<Cow<'static, str>>) -> Self::Output<Type> {
30        self.with_attribute(Type(r#type.into()))
31    }
32
33    /// Applicable media.
34    fn media(self, media: impl Into<Cow<'static, str>>) -> Self::Output<Media> {
35        self.with_attribute(Media(media.into()))
36    }
37
38    /// Address of the resource.
39    fn src(self, src: impl Into<Cow<'static, str>>) -> Self::Output<Src> {
40        self.with_attribute(Src(src.into()))
41    }
42
43    /// Images to use in different situations, e.g., high-resolution displays, small monitors, etc.
44    fn src_set(self, src_set: impl Into<Cow<'static, str>>) -> Self::Output<SrcSet> {
45        self.with_attribute(SrcSet(src_set.into()))
46    }
47
48    /// Image sizes for different page layouts.
49    fn sizes(self, sizes: impl Into<Cow<'static, str>>) -> Self::Output<Sizes> {
50        self.with_attribute(Sizes(sizes.into()))
51    }
52
53    /// Vertical dimension.
54    fn height(self, height: u32) -> Self::Output<Height> {
55        self.with_attribute(Height(height))
56    }
57
58    /// Horizontal dimension.
59    fn width(self, width: u32) -> Self::Output<Width> {
60        self.with_attribute(Width(width))
61    }
62}