Skip to main content

irontide_core/
torrent_version.rs

1//! Torrent protocol version indicator.
2
3use serde::{Deserialize, Serialize};
4
5/// Indicates which `BitTorrent` protocol version(s) a torrent supports.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
7pub enum TorrentVersion {
8    /// `BitTorrent` v1 only (BEP 3). SHA-1 piece hashes.
9    V1Only,
10    /// `BitTorrent` v2 only (BEP 52). SHA-256 Merkle per-file trees.
11    V2Only,
12    /// Hybrid v1+v2 (BEP 52). Both hash types in a single info dict.
13    Hybrid,
14}
15
16impl TorrentVersion {
17    /// Whether this version includes v1 (SHA-1) hashes.
18    #[must_use]
19    pub fn has_v1(&self) -> bool {
20        matches!(self, Self::V1Only | Self::Hybrid)
21    }
22
23    /// Whether this version includes v2 (SHA-256 Merkle) hashes.
24    #[must_use]
25    pub fn has_v2(&self) -> bool {
26        matches!(self, Self::V2Only | Self::Hybrid)
27    }
28
29    /// Whether this is a hybrid torrent with both v1 and v2 hashes.
30    #[must_use]
31    pub fn is_hybrid(&self) -> bool {
32        matches!(self, Self::Hybrid)
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[test]
41    fn v1_only_flags() {
42        let v = TorrentVersion::V1Only;
43        assert!(v.has_v1());
44        assert!(!v.has_v2());
45        assert!(!v.is_hybrid());
46    }
47
48    #[test]
49    fn v2_only_flags() {
50        let v = TorrentVersion::V2Only;
51        assert!(!v.has_v1());
52        assert!(v.has_v2());
53        assert!(!v.is_hybrid());
54    }
55
56    #[test]
57    fn hybrid_flags() {
58        let v = TorrentVersion::Hybrid;
59        assert!(v.has_v1());
60        assert!(v.has_v2());
61        assert!(v.is_hybrid());
62    }
63}