Skip to content

Commit 8167598

Browse files
committed
refactor: [#1309] align our HTTP Announce Events with the aquatic one
- To simplify conversions. - To properly implement BEP 3. The new `empty` option is defined in the protocol and was missing in our implementation.
1 parent 5362a6d commit 8167598

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

packages/http-protocol/src/v1/requests/announce.rs

+27-17
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,21 @@ pub enum ParseAnnounceQueryError {
145145
///
146146
/// Refer to [BEP 03. The `BitTorrent Protocol` Specification](https://www.bittorrent.org/beps/bep_0003.html)
147147
/// for more information.
148-
#[derive(PartialEq, Debug)]
148+
#[derive(PartialEq, Debug, Clone)]
149149
pub enum Event {
150150
/// Event sent when a download first begins.
151151
Started,
152+
152153
/// Event sent when the downloader cease downloading.
153154
Stopped,
155+
154156
/// Event sent when the download is complete.
155-
/// No `completed` is sent if the file was complete when started
157+
/// No `completed` is sent if the file was complete when started.
156158
Completed,
159+
160+
/// It is the same as not being present. If not present, this is one of the
161+
/// announcements done at regular intervals.
162+
Empty,
157163
}
158164

159165
impl FromStr for Event {
@@ -164,6 +170,7 @@ impl FromStr for Event {
164170
"started" => Ok(Self::Started),
165171
"stopped" => Ok(Self::Stopped),
166172
"completed" => Ok(Self::Completed),
173+
"empty" => Ok(Self::Empty),
167174
_ => Err(ParseAnnounceQueryError::InvalidParam {
168175
param_name: EVENT.to_owned(),
169176
param_value: raw_param.to_owned(),
@@ -179,17 +186,29 @@ impl fmt::Display for Event {
179186
Event::Started => write!(f, "started"),
180187
Event::Stopped => write!(f, "stopped"),
181188
Event::Completed => write!(f, "completed"),
189+
Event::Empty => write!(f, "empty"),
182190
}
183191
}
184192
}
185193

186194
impl From<aquatic_udp_protocol::request::AnnounceEvent> for Event {
187-
fn from(value: aquatic_udp_protocol::request::AnnounceEvent) -> Self {
188-
match value {
195+
fn from(event: aquatic_udp_protocol::request::AnnounceEvent) -> Self {
196+
match event {
189197
AnnounceEvent::Started => Self::Started,
190198
AnnounceEvent::Stopped => Self::Stopped,
191199
AnnounceEvent::Completed => Self::Completed,
192-
AnnounceEvent::None => panic!("can't convert announce event from aquatic for None variant"),
200+
AnnounceEvent::None => Self::Empty,
201+
}
202+
}
203+
}
204+
205+
impl From<Event> for aquatic_udp_protocol::request::AnnounceEvent {
206+
fn from(event: Event) -> Self {
207+
match event {
208+
Event::Started => Self::Started,
209+
Event::Stopped => Self::Stopped,
210+
Event::Completed => Self::Completed,
211+
Event::Empty => Self::None,
193212
}
194213
}
195214
}
@@ -399,19 +418,10 @@ pub fn peer_from_request(announce_request: &Announce, peer_ip: &IpAddr) -> peer:
399418
uploaded: announce_request.uploaded.unwrap_or(NumberOfBytes::new(0)),
400419
downloaded: announce_request.downloaded.unwrap_or(NumberOfBytes::new(0)),
401420
left: announce_request.left.unwrap_or(NumberOfBytes::new(0)),
402-
event: convert_to_aquatic_event(&announce_request.event),
403-
}
404-
}
405-
406-
#[must_use]
407-
pub fn convert_to_aquatic_event(event: &Option<Event>) -> aquatic_udp_protocol::request::AnnounceEvent {
408-
match event {
409-
Some(event) => match &event {
410-
Event::Started => AnnounceEvent::Started,
411-
Event::Stopped => AnnounceEvent::Stopped,
412-
Event::Completed => AnnounceEvent::Completed,
421+
event: match &announce_request.event {
422+
Some(event) => event.clone().into(),
423+
None => AnnounceEvent::None,
413424
},
414-
None => AnnounceEvent::None,
415425
}
416426
}
417427

0 commit comments

Comments
 (0)