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
//! Types for representing discovered releases.
use debversion::Version;
use std::cmp::Ordering;
/// A discovered release from an upstream source
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Release {
/// The version string of the release (after uversionmangle)
pub version: String,
/// The URL to download the release tarball (after downloadurlmangle)
pub url: String,
/// Optional URL to the PGP signature file
pub pgpsigurl: Option<String>,
/// Optional target filename for the downloaded tarball (from filenamemangle)
pub target_filename: Option<String>,
/// Optional Debian package version (from oversionmangle, e.g., "1.0+dfsg")
pub package_version: Option<String>,
}
impl Release {
/// Create a new Release
///
/// # Examples
///
/// ```
/// use debian_watch::Release;
///
/// let release = Release::new("1.0.0", "https://example.com/project-1.0.0.tar.gz", None);
/// assert_eq!(release.version, "1.0.0");
/// assert_eq!(release.url, "https://example.com/project-1.0.0.tar.gz");
/// ```
pub fn new(
version: impl Into<String>,
url: impl Into<String>,
pgpsigurl: Option<String>,
) -> Self {
Self {
version: version.into(),
url: url.into(),
pgpsigurl,
target_filename: None,
package_version: None,
}
}
/// Create a new Release with all fields
///
/// # Examples
///
/// ```
/// use debian_watch::Release;
///
/// let release = Release::new_full(
/// "1.0.0",
/// "https://example.com/project-1.0.0.tar.gz",
/// Some("https://example.com/project-1.0.0.tar.gz.asc".to_string()),
/// Some("myproject_1.0.0.orig.tar.gz".to_string()),
/// Some("1.0.0+dfsg".to_string()),
/// );
/// assert_eq!(release.version, "1.0.0");
/// assert_eq!(release.target_filename, Some("myproject_1.0.0.orig.tar.gz".to_string()));
/// ```
pub fn new_full(
version: impl Into<String>,
url: impl Into<String>,
pgpsigurl: Option<String>,
target_filename: Option<String>,
package_version: Option<String>,
) -> Self {
Self {
version: version.into(),
url: url.into(),
pgpsigurl,
target_filename,
package_version,
}
}
/// Download the release tarball (async version)
///
/// Downloads the tarball from the release URL.
/// Requires the 'discover' feature.
///
/// # Examples
///
/// ```ignore
/// use debian_watch::Release;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let release = Release::new("1.0.0", "https://example.com/project-1.0.tar.gz", None);
/// let data = release.download().await?;
/// println!("Downloaded {} bytes", data.len());
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "discover")]
pub async fn download(&self) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
let client = reqwest::Client::new();
let response = client.get(&self.url).send().await?;
let bytes = response.bytes().await?;
Ok(bytes.to_vec())
}
/// Download the release tarball (blocking version)
///
/// Downloads the tarball from the release URL.
/// Requires both 'discover' and 'blocking' features.
///
/// # Examples
///
/// ```ignore
/// use debian_watch::Release;
///
/// let release = Release::new("1.0.0", "https://example.com/project-1.0.tar.gz", None);
/// let data = release.download_blocking()?;
/// println!("Downloaded {} bytes", data.len());
/// ```
#[cfg(all(feature = "discover", feature = "blocking"))]
pub fn download_blocking(&self) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
let client = reqwest::blocking::Client::new();
let response = client.get(&self.url).send()?;
let bytes = response.bytes()?;
Ok(bytes.to_vec())
}
}
impl PartialOrd for Release {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Release {
fn cmp(&self, other: &Self) -> Ordering {
// Parse versions and compare them
match (
self.version.parse::<Version>(),
other.version.parse::<Version>(),
) {
(Ok(v1), Ok(v2)) => v1.cmp(&v2),
// If parsing fails, fall back to string comparison
_ => self.version.cmp(&other.version),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_release_new() {
let release = Release::new("1.0.0", "https://example.com/foo.tar.gz", None);
assert_eq!(release.version, "1.0.0");
assert_eq!(release.url, "https://example.com/foo.tar.gz");
assert_eq!(release.pgpsigurl, None);
let release = Release::new(
"2.0.0",
"https://example.com/foo-2.0.0.tar.gz",
Some("https://example.com/foo-2.0.0.tar.gz.asc".to_string()),
);
assert_eq!(release.version, "2.0.0");
assert_eq!(
release.pgpsigurl,
Some("https://example.com/foo-2.0.0.tar.gz.asc".to_string())
);
}
#[test]
fn test_release_ordering() {
let r1 = Release::new("1.0.0", "https://example.com/foo-1.0.0.tar.gz", None);
let r2 = Release::new("2.0.0", "https://example.com/foo-2.0.0.tar.gz", None);
let r3 = Release::new("1.5.0", "https://example.com/foo-1.5.0.tar.gz", None);
assert!(r1 < r2);
assert!(r2 > r1);
assert!(r1 < r3);
assert!(r3 < r2);
}
#[test]
fn test_release_ordering_debian_versions() {
// Test with Debian version strings
let r1 = Release::new("1.0", "https://example.com/foo-1.0.tar.gz", None);
let r2 = Release::new("1.0+dfsg", "https://example.com/foo-1.0+dfsg.tar.gz", None);
let r3 = Release::new("1.0~rc1", "https://example.com/foo-1.0~rc1.tar.gz", None);
// 1.0~rc1 < 1.0 < 1.0+dfsg in Debian version ordering
assert!(r3 < r1);
assert!(r1 < r2);
}
#[test]
fn test_release_max() {
let releases = vec![
Release::new("1.0.0", "https://example.com/foo-1.0.0.tar.gz", None),
Release::new("2.0.0", "https://example.com/foo-2.0.0.tar.gz", None),
Release::new("1.5.0", "https://example.com/foo-1.5.0.tar.gz", None),
];
let max = releases.iter().max().unwrap();
assert_eq!(max.version, "2.0.0");
}
}