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
#![deny(missing_docs)]
#![deny(clippy::all)]
#![deny(unsafe_code)]
//! Please, see the [`rust-releases`] for additional documentation on how this crate can be used.
//!
//! [`rust-releases`]: https://docs.rs/rust-releases
use rust_releases_core::{semver, Channel, FetchResources, Release, ReleaseIndex, Source};
use rust_releases_io::Document;
#[cfg(test)]
#[macro_use]
extern crate rust_releases_io;

pub(crate) mod errors;
pub(crate) mod fetch;

use crate::fetch::fetch;

pub use errors::{RustChangelogError, RustChangelogResult};
use std::str::FromStr;

/// A [`Source`] which obtains release data from the official Rust changelog.
///
/// [`Source`]: rust_releases_core::Source
pub struct RustChangelog {
    source: Document,

    /// Used to compare against the date of an unreleased version which does already exist in the
    /// changelog. If this date is at least as late as the time found in a release registration, we
    /// will say that such a version is released (i.e. published).
    today: ReleaseDate,
}

impl RustChangelog {
    pub(crate) fn from_document(source: Document) -> Self {
        Self {
            source,
            today: ReleaseDate::today(),
        }
    }

    #[cfg(test)]
    pub(crate) fn from_document_with_date(source: Document, date: ReleaseDate) -> Self {
        Self {
            source,
            today: date,
        }
    }
}

impl Source for RustChangelog {
    type Error = RustChangelogError;

    fn build_index(&self) -> Result<ReleaseIndex, Self::Error> {
        let contents = self.source.load()?;
        let content = String::from_utf8(contents).map_err(RustChangelogError::UnrecognizedText)?;

        let releases = content
            .lines()
            .filter(|s| s.starts_with("Version"))
            .filter_map(|line| {
                let parsed = parse_release(line.split_ascii_whitespace());

                match parsed {
                    // If the version and date can be parsed, and the version has been released
                    Ok((version, date)) if date.is_available(&self.today) => {
                        Some(Ok(Release::new_stable(version)))
                    }
                    // If the version and date can be parsed, but the version is not yet released
                    Ok(_) => None,
                    // We skip versions 0.10, 0.9, etc. which require more lenient semver parsing
                    // Unfortunately we can't access the error kind, so we have to match the string instead
                    Err(RustChangelogError::SemverError(err, _))
                        if err.to_string().as_str()
                            == "unexpected end of input while parsing minor version number" =>
                    {
                        None
                    }
                    // In any ony other error case, we forward the error
                    Err(err) => Some(Err(err)),
                }
            })
            .collect::<Result<ReleaseIndex, Self::Error>>()?;

        Ok(releases)
    }
}

impl FetchResources for RustChangelog {
    type Error = RustChangelogError;

    fn fetch_channel(channel: Channel) -> Result<Self, Self::Error> {
        if let Channel::Stable = channel {
            let document = fetch()?;
            Ok(Self::from_document(document))
        } else {
            Err(RustChangelogError::ChannelNotAvailable(channel))
        }
    }
}

fn parse_release<'line>(
    mut parts: impl Iterator<Item = &'line str>,
) -> Result<(semver::Version, ReleaseDate), RustChangelogError> {
    let version_number = parts
        .nth(1)
        .ok_or(RustChangelogError::NoVersionInChangelogItem)?;
    let release_date = parts
        .next()
        .ok_or(RustChangelogError::NoDateInChangelogItem)?;

    let version = semver::Version::parse(version_number)
        .map_err(|err| RustChangelogError::SemverError(err, version_number.to_string()))?;

    let date = ReleaseDate::parse(&release_date[1..release_date.len() - 1])?;

    Ok((version, date))
}

#[derive(Debug)]
struct ReleaseDate(chrono::NaiveDate);

impl ReleaseDate {
    fn today() -> Self {
        Self(chrono::Utc::today().naive_utc())
    }

    fn parse(from: &str) -> Result<Self, RustChangelogError> {
        from.parse::<ReleaseDate>()
    }

    fn is_available(&self, today: &Self) -> bool {
        today.0 >= self.0
    }
}

impl FromStr for ReleaseDate {
    type Err = crate::RustChangelogError;

    fn from_str(item: &str) -> Result<Self, Self::Err> {
        let result = chrono::NaiveDate::parse_from_str(item, "%Y-%m-%d")
            .map_err(|_| RustChangelogError::ChronoParseError(item.to_string()))?;

        Ok(Self(result))
    }
}

#[cfg(test)]
mod tests {
    use super::ReleaseDate;
    use crate::RustChangelog;
    use rust_releases_core::{semver, Channel, FetchResources, Release, ReleaseIndex};
    use rust_releases_io::Document;
    use yare::parameterized;

    #[test]
    fn source_dist_index() {
        let path = [
            env!("CARGO_MANIFEST_DIR"),
            "/../../resources/rust_changelog/RELEASES.md",
        ]
        .join("");
        let strategy = RustChangelog::from_document(Document::LocalPath(path.into()));
        let index = ReleaseIndex::from_source(strategy).unwrap();

        assert!(index.releases().len() > 50);
        assert_eq!(
            index.releases()[0],
            Release::new_stable(semver::Version::new(1, 50, 0))
        );
    }

    #[test]
    fn parse_date() {
        let date = ReleaseDate::parse("2021-09-01").unwrap();
        assert_eq!(date.0, chrono::NaiveDate::from_ymd(2021, 9, 1));
    }

    #[test]
    fn with_unreleased_version() {
        let path = [
            env!("CARGO_MANIFEST_DIR"),
            "/../../resources/rust_changelog/RELEASES_with_unreleased.md",
        ]
        .join("");

        let date = ReleaseDate::parse("2021-09-01").unwrap();
        let strategy =
            RustChangelog::from_document_with_date(Document::LocalPath(path.into()), date);
        let index = ReleaseIndex::from_source(strategy).unwrap();

        let mut releases = index.releases().iter();

        assert_eq!(
            releases.next().unwrap().version(),
            &semver::Version::new(1, 54, 0)
        );
    }

    #[parameterized(
        beta = { Channel::Beta },
        nightly = { Channel::Nightly },
    )]
    fn fetch_unsupported_channel(channel: Channel) {
        __internal_dl_test!({
            let file = RustChangelog::fetch_channel(channel);
            assert!(file.is_err());
        })
    }

    #[test]
    fn fetch_supported_channel() {
        __internal_dl_test!({
            let file = RustChangelog::fetch_channel(Channel::Stable);
            assert!(file.is_ok());
        })
    }
}