rust_releases_rust_dist_with_cli/lib.rs
1#![deny(missing_docs)]
2#![deny(clippy::all)]
3#![deny(unsafe_code)]
4#![allow(clippy::upper_case_acronyms)]
5//! Please, see the [`rust-releases`] for additional documentation on how this crate can be used.
6//!
7//! [`rust-releases`]: https://docs.rs/rust-releases
8
9use rust_releases_core::{semver, Release, ReleaseIndex, Source};
10use rust_releases_io::Document;
11use std::collections::BTreeSet;
12use std::fs;
13use std::iter::FromIterator;
14use std::path::{Path, PathBuf};
15
16pub(crate) mod errors;
17
18pub use crate::errors::{RustDistWithCLIError, RustDistWithCLIResult};
19
20/// A [`Source`] which parses Rust release data from the AWS S3 index.
21///
22/// The data files required as input must be obtained separately (i.e. [`FetchResources`] is not
23/// implemented for [`RustDistWithCLI`]). You can download the input data files by using the [`aws`]
24/// cli utility and running: `aws --no-sign-request s3 ls static-rust-lang-org/dist/ > rust_dist_with_cli.txt`
25///
26/// You may then load the source by creating the [`RustDistWithCLI`] and calling the `build_index` method
27/// from the `Source` trait.
28///
29/// ```rust,no_run
30/// use rust_releases_core::Source;
31/// use rust_releases_rust_dist_with_cli::RustDistWithCLI;
32///
33/// let source = RustDistWithCLI::from_path("rust_dist_with_cli.txt");
34/// let index = source.build_index().expect("Unable to build a release index");
35/// ```
36///
37/// Alternatively you can look at [`RustDist`] which also uses the AWS S3 index, but obtains the
38/// input data differently. The [`RustDist`] source does include a [`FetchResources`] implementation.
39///
40/// [`Source`]: rust_releases_core::Source
41/// [`FetchResources`]: rust_releases_core::FetchResources
42/// [`RustDistWithCLI`]: crate::RustDistWithCLI
43/// [`RustDist`]: https://docs.rs/rust-releases-rust-dist/0.15.0/rust_releases_rust_dist/struct.RustDist.html
44/// [`aws`]: https://aws.amazon.com/cli/
45
46pub struct RustDistWithCLI {
47 path: PathBuf,
48}
49
50impl RustDistWithCLI {
51 /// Creates a `RustDistWithCLI` from a path.
52 pub fn from_path<P: AsRef<Path>>(path: P) -> Self {
53 Self {
54 path: path.as_ref().to_path_buf(),
55 }
56 }
57}
58
59impl Source for RustDistWithCLI {
60 type Error = RustDistWithCLIError;
61
62 fn build_index(&self) -> Result<ReleaseIndex, Self::Error> {
63 let file = fs::read(&self.path)?;
64 let document = Document::new(file);
65 let content = std::str::from_utf8(document.buffer())?;
66
67 // NB: poor man's parsing for stable releases only
68 let versions = content
69 .lines()
70 .filter(|s| !s.trim().starts_with("PRE"))
71 .filter_map(|line| {
72 line.split_ascii_whitespace()
73 .nth(3)
74 .filter(|s| s.starts_with("rust-1"))
75 })
76 .filter_map(|s| s.split('-').nth(1))
77 .flat_map(|s| semver::Version::parse(s).map(Release::new_stable))
78 .collect::<BTreeSet<_>>();
79
80 Ok(ReleaseIndex::from_iter(versions))
81 }
82}
83
84#[cfg(test)]
85mod tests {
86 use crate::{ReleaseIndex, RustDistWithCLI};
87 use rust_releases_core::semver;
88
89 #[test]
90 fn strategy_dist_index() {
91 let expected_version = semver::Version::parse("1.50.0").unwrap();
92
93 let path = [
94 env!("CARGO_MANIFEST_DIR"),
95 "/../../resources/rust_dist_with_cli/dist.txt",
96 ]
97 .join("");
98
99 let strategy = RustDistWithCLI::from_path(path);
100 let index = ReleaseIndex::from_source(strategy).unwrap();
101
102 assert!(index.releases().len() > 50);
103 assert_eq!(index.releases()[0].version(), &expected_version);
104 }
105}