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
//! Torrent protocol version indicator.
use serde::{Deserialize, Serialize};
/// Indicates which BitTorrent protocol version(s) a torrent supports.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum TorrentVersion {
/// BitTorrent v1 only (BEP 3). SHA-1 piece hashes.
V1Only,
/// BitTorrent v2 only (BEP 52). SHA-256 Merkle per-file trees.
V2Only,
/// Hybrid v1+v2 (BEP 52). Both hash types in a single info dict.
Hybrid,
}
impl TorrentVersion {
/// Whether this version includes v1 (SHA-1) hashes.
pub fn has_v1(&self) -> bool {
matches!(self, Self::V1Only | Self::Hybrid)
}
/// Whether this version includes v2 (SHA-256 Merkle) hashes.
pub fn has_v2(&self) -> bool {
matches!(self, Self::V2Only | Self::Hybrid)
}
/// Whether this is a hybrid torrent with both v1 and v2 hashes.
pub fn is_hybrid(&self) -> bool {
matches!(self, Self::Hybrid)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn v1_only_flags() {
let v = TorrentVersion::V1Only;
assert!(v.has_v1());
assert!(!v.has_v2());
assert!(!v.is_hybrid());
}
#[test]
fn v2_only_flags() {
let v = TorrentVersion::V2Only;
assert!(!v.has_v1());
assert!(v.has_v2());
assert!(!v.is_hybrid());
}
#[test]
fn hybrid_flags() {
let v = TorrentVersion::Hybrid;
assert!(v.has_v1());
assert!(v.has_v2());
assert!(v.is_hybrid());
}
}