rss/extension/
mod.rs

1use std::collections::HashMap;
2
3/// Types and functions for
4/// [iTunes](https://help.apple.com/itc/podcasts_connect/#/itcb54353390) extensions.
5pub mod itunes;
6
7/// Types and functions for [Dublin Core](http://dublincore.org/documents/dces/) extensions.
8pub mod dublincore;
9
10/// A map of extension namespace prefixes to local names to elements.
11pub type ExtensionMap = HashMap<String, HashMap<String, Vec<Extension>>>;
12
13/// A namespaced extension such as iTunes or Dublin Core.
14#[derive(Debug, Default, Clone, PartialEq)]
15pub struct Extension {
16    /// The qualified name of the extension element.
17    pub name: String,
18    /// The content of the extension element.
19    pub value: Option<String>,
20    /// The attributes for the extension element.
21    pub attrs: HashMap<String, String>,
22    /// The children of the extension element. This is a map of local names to child
23    /// elements.
24    pub children: HashMap<String, Vec<Extension>>,
25}
26
27/// Get a reference to the value for the first extension with the specified key.
28pub fn get_extension_value<'a>(map: &'a HashMap<String, Vec<Extension>>,
29                               key: &str)
30                               -> Option<&'a str> {
31    map.get(key).and_then(|v| v.first()).and_then(|ext| ext.value.as_ref()).map(|s| s.as_str())
32}
33
34/// Remove and return the value for the first extension with the specified key.
35pub fn remove_extension_value(map: &mut HashMap<String, Vec<Extension>>,
36                              key: &str)
37                              -> Option<String> {
38    map.remove(key).map(|mut v| v.remove(0)).and_then(|ext| ext.value)
39}
40
41/// Get a reference to all values for the extensions with the specified key.
42pub fn get_extension_values<'a>(map: &'a HashMap<String, Vec<Extension>>,
43                                key: &str)
44                                -> Option<Vec<&'a str>> {
45    map.get(key).map(|v| {
46        v.iter().filter_map(|ext| ext.value.as_ref().map(|s| s.as_str())).collect::<Vec<_>>()
47    })
48}
49
50/// Remove and return all values for the extensions with the specified key.
51pub fn remove_extension_values(map: &mut HashMap<String, Vec<Extension>>,
52                               key: &str)
53                               -> Option<Vec<String>> {
54    map.remove(key).map(|v| v.into_iter().filter_map(|ext| ext.value).collect::<Vec<_>>())
55}