vortex_protocol/tlv/
mod.rs1pub mod cache_piece_content;
18pub mod close;
19pub mod download_cache_piece;
20pub mod download_persistent_cache_piece;
21pub mod download_piece;
22pub mod error;
23pub mod persistent_cache_piece_content;
24pub mod piece_content;
25
26#[repr(u8)]
28#[derive(Debug, Clone, Copy, PartialEq, Eq)]
29pub enum Tag {
30 DownloadPiece = 0,
33
34 PieceContent = 1,
36
37 DownloadPersistentCachePiece = 2,
40
41 PersistentCachePieceContent = 3,
43
44 DownloadCachePiece = 4,
47
48 CachePieceContent = 5,
50
51 Reserved(u8),
53
54 Close = 254,
56
57 Error = 255,
59}
60
61impl From<u8> for Tag {
63 fn from(value: u8) -> Self {
65 match value {
66 0 => Tag::DownloadPiece,
67 1 => Tag::PieceContent,
68 2 => Tag::DownloadPersistentCachePiece,
69 3 => Tag::PersistentCachePieceContent,
70 4 => Tag::DownloadCachePiece,
71 5 => Tag::CachePieceContent,
72 6..=253 => Tag::Reserved(value),
73 254 => Tag::Close,
74 255 => Tag::Error,
75 }
76 }
77}
78
79impl From<Tag> for u8 {
81 fn from(tag: Tag) -> Self {
83 match tag {
84 Tag::DownloadPiece => 0,
85 Tag::PieceContent => 1,
86 Tag::DownloadPersistentCachePiece => 2,
87 Tag::PersistentCachePieceContent => 3,
88 Tag::DownloadCachePiece => 4,
89 Tag::CachePieceContent => 5,
90 Tag::Reserved(value) => value,
91 Tag::Close => 254,
92 Tag::Error => 255,
93 }
94 }
95}
96
97#[cfg(test)]
98mod tests {
99 use super::*;
100
101 #[test]
102 fn test_try_from_u8() {
103 assert_eq!(Tag::from(0), Tag::DownloadPiece);
104 assert_eq!(Tag::from(1), Tag::PieceContent);
105 assert_eq!(Tag::from(2), Tag::DownloadPersistentCachePiece);
106 assert_eq!(Tag::from(3), Tag::PersistentCachePieceContent);
107 assert_eq!(Tag::from(4), Tag::DownloadCachePiece);
108 assert_eq!(Tag::from(5), Tag::CachePieceContent);
109 assert_eq!(Tag::from(6), Tag::Reserved(6));
110 assert_eq!(Tag::from(253), Tag::Reserved(253));
111 assert_eq!(Tag::from(254), Tag::Close);
112 assert_eq!(Tag::from(255), Tag::Error);
113 }
114
115 #[test]
116 fn test_from_tag() {
117 assert_eq!(u8::from(Tag::DownloadPiece), 0);
118 assert_eq!(u8::from(Tag::PieceContent), 1);
119 assert_eq!(u8::from(Tag::DownloadPersistentCachePiece), 2);
120 assert_eq!(u8::from(Tag::PersistentCachePieceContent), 3);
121 assert_eq!(u8::from(Tag::DownloadCachePiece), 4);
122 assert_eq!(u8::from(Tag::CachePieceContent), 5);
123 assert_eq!(u8::from(Tag::Reserved(6)), 6);
124 assert_eq!(u8::from(Tag::Reserved(253)), 253);
125 assert_eq!(u8::from(Tag::Close), 254);
126 assert_eq!(u8::from(Tag::Error), 255);
127 }
128}