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
// This file is part of rss.
//
// Copyright © 2015-2017 The rust-syndication Developers
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the MIT License and/or Apache 2.0 License.

use std::collections::HashMap;
use std::fmt;
use std::io::Write;
use std::str::FromStr;

use quick_xml::Error as XmlError;
use quick_xml::Writer;

use extension::Extension;
use toxml::WriterExt;

/// The Syndication XML namespace.
pub const NAMESPACE: &str = "http://purl.org/rss/1.0/modules/syndication/";

/// The unit of time between updates/refreshes
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub enum UpdatePeriod {
    /// refresh hourly
    HOURLY,
    /// refresh daily
    DAILY,
    /// refresh weekly
    WEEKLY,
    /// refresh monthly
    MONTHLY,
    /// refresh yearly
    YEARLY,
}

impl FromStr for UpdatePeriod {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "hourly" => Ok(UpdatePeriod::HOURLY),
            "daily" => Ok(UpdatePeriod::DAILY),
            "weekly" => Ok(UpdatePeriod::WEEKLY),
            "monthly" => Ok(UpdatePeriod::MONTHLY),
            "yearly" => Ok(UpdatePeriod::YEARLY),
            _ => Err(())
        }
    }
}

impl fmt::Display for UpdatePeriod {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            UpdatePeriod::HOURLY => write!(f, "hourly"),
            UpdatePeriod::DAILY => write!(f, "daily"),
            UpdatePeriod::WEEKLY => write!(f, "weekly"),
            UpdatePeriod::MONTHLY => write!(f, "monthly"),
            UpdatePeriod::YEARLY => write!(f, "yearly"),
        }
    }
}

/// An RSS syndication element extension.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Builder)]
#[builder(setter(into), default)]
pub struct SyndicationExtension {
    /// The refresh period for this channel
    period: UpdatePeriod,
    /// Number of periods between refreshes
    frequency: u32,
    /// Timestamp from which the refresh periods are calculated
    base: String,
}

impl SyndicationExtension {
    /// Retrieve the base timestamp from which the refresh periods are calculated
    pub fn base(&self) -> &str {
        &self.base
    }

    /// Set the base from which the refresh periods are calculated
    pub fn set_base(&mut self, base: &str) {
        self.base = base.to_owned();
    }

    /// Retrieve the number of periods between refreshes
    pub fn frequency(&self) -> u32 {
        self.frequency
    }

    /// Set the number of periods between refreshes
    pub fn set_frequency(&mut self, frequency: u32) {
        self.frequency = frequency;
    }

    /// Retrieve the refresh period for this channel
    pub fn period(&self) -> &UpdatePeriod {
        &self.period
    }

    /// Set the refresh period for this channel
    pub fn set_period(&mut self, period: UpdatePeriod) {
        self.period = period;
    }

    /// Serialises this extension to the nominated writer
    pub fn to_xml<W: Write>(&self, namespaces: &HashMap<String, String>, writer: &mut Writer<W>) -> Result<(), XmlError> {
        for (prefix, namespace) in namespaces {
            if NAMESPACE == namespace {
                writer.write_text_element(format!("{}:updatePeriod", prefix), &self.period.to_string())?;
                writer.write_text_element(format!("{}:updateFrequency", prefix), &format!("{}", self.frequency))?;
                writer.write_text_element(format!("{}:updateBase", prefix), &self.base)?;
            }
        }
        Ok(())
    }
}

impl Default for SyndicationExtension {
    fn default() -> Self {
        SyndicationExtension { period: UpdatePeriod::DAILY, frequency: 1, base: String::from("1970-01-01T00:00+00:00") }
    }
}

/// Retrieves the extensions for the nominated field and runs the callback if there is at least 1 extension value
fn with_first_ext_value<'a, F>(map: &'a HashMap<String, Vec<Extension>>, field: &str, f: F)
    where F: FnOnce(&'a str) {
    if let Some(extensions) = map.get(field) {
        if !extensions.is_empty() {
            if let Some(v) = extensions[0].value.as_ref() {
                f(v);
            }
        }
    }
}

impl SyndicationExtension {
    /// Creates a `SyndicationExtension` using the specified `HashMap`.
    pub fn from_map(map: HashMap<String, Vec<Extension>>) -> Self {
        let mut syn = SyndicationExtension::default();

        with_first_ext_value(&map, "updatePeriod", |value| syn.period = value.parse().unwrap());
        with_first_ext_value(&map, "updateFrequency", |value| syn.frequency = value.parse().unwrap());
        with_first_ext_value(&map, "updateBase", |value| syn.base = value.to_owned());

        syn
    }
}