pcloud/stream/
mod.rs

1use chrono::{DateTime, Utc};
2
3pub mod audio;
4pub mod file;
5pub mod video;
6
7/// A struct that represents a list of streaming links with metadata such as expiration date and hosts.
8#[derive(Debug, serde::Deserialize)]
9pub struct StreamingLinkList {
10    /// The expiration date and time of the streaming links.
11    #[serde(with = "crate::date")]
12    pub expires: DateTime<Utc>,
13
14    /// The list of available host URLs for streaming.
15    pub hosts: Vec<String>,
16
17    /// The path to the resource to be streamed.
18    pub path: String,
19}
20
21/// A struct representing an individual streaming link, built using a host and a path.
22pub struct StreamingLink<'a> {
23    host: &'a str,
24    path: &'a str,
25}
26
27impl<'a> StreamingLink<'a> {
28    /// Creates a new `StreamingLink` from a host and path.
29    ///
30    /// # Arguments
31    ///
32    /// * `host` - The host URL for streaming.
33    /// * `path` - The path to the resource.
34    ///
35    /// # Returns
36    ///
37    /// Returns a new `StreamingLink`.
38    #[inline(always)]
39    fn new(host: &'a str, path: &'a str) -> Self {
40        Self { host, path }
41    }
42}
43
44impl std::fmt::Display for StreamingLink<'_> {
45    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46        write!(f, "https://{}{}", self.host, self.path)
47    }
48}
49
50impl StreamingLinkList {
51    /// Returns the first streaming link using the first host in the list.
52    ///
53    /// # Returns
54    ///
55    /// An optional `StreamingLink`, which may be `None` if the list of hosts is empty.
56    pub fn first_link(&self) -> Option<StreamingLink<'_>> {
57        self.hosts
58            .first()
59            .map(|host| StreamingLink::new(host.as_str(), self.path.as_str()))
60    }
61
62    /// Returns the last streaming link using the last host in the list.
63    ///
64    /// # Returns
65    ///
66    /// An optional `StreamingLink`, which may be `None` if the list of hosts is empty.
67    pub fn last_link(&self) -> Option<StreamingLink<'_>> {
68        self.hosts
69            .last()
70            .map(|host| StreamingLink::new(host.as_str(), self.path.as_str()))
71    }
72
73    /// Returns an iterator over all available streaming links.
74    ///
75    /// This will create a `StreamingLink` for each host in the list using the common path.
76    ///
77    /// # Returns
78    ///
79    /// An iterator over `StreamingLink` instances.
80    pub fn links(&self) -> impl Iterator<Item = StreamingLink<'_>> {
81        self.hosts
82            .iter()
83            .map(move |host| StreamingLink::new(host.as_str(), self.path.as_str()))
84    }
85}