Skip to content

Commit 753ae91

Browse files
committed
fix: move code in src/protocol/info_hash.rs
1 parent 981509f commit 753ae91

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

src/protocol/info_hash.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,35 @@ impl<'de> serde::de::Deserialize<'de> for InfoHash {
6464
}
6565
}
6666

67+
struct InfoHashVisitor;
68+
69+
impl<'v> serde::de::Visitor<'v> for InfoHashVisitor {
70+
type Value = InfoHash;
71+
72+
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
73+
write!(formatter, "a 40 character long hash")
74+
}
75+
76+
fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<Self::Value, E> {
77+
if v.len() != 40 {
78+
return Err(serde::de::Error::invalid_value(
79+
serde::de::Unexpected::Str(v),
80+
&"expected a 40 character long string",
81+
));
82+
}
83+
84+
let mut res = InfoHash([0u8; 20]);
85+
86+
if binascii::hex2bin(v.as_bytes(), &mut res.0).is_err() {
87+
return Err(serde::de::Error::invalid_value(
88+
serde::de::Unexpected::Str(v),
89+
&"expected a hexadecimal string",
90+
));
91+
};
92+
Ok(res)
93+
}
94+
}
95+
6796
#[cfg(test)]
6897
mod tests {
6998

@@ -159,32 +188,3 @@ mod tests {
159188
);
160189
}
161190
}
162-
163-
struct InfoHashVisitor;
164-
165-
impl<'v> serde::de::Visitor<'v> for InfoHashVisitor {
166-
type Value = InfoHash;
167-
168-
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
169-
write!(formatter, "a 40 character long hash")
170-
}
171-
172-
fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<Self::Value, E> {
173-
if v.len() != 40 {
174-
return Err(serde::de::Error::invalid_value(
175-
serde::de::Unexpected::Str(v),
176-
&"expected a 40 character long string",
177-
));
178-
}
179-
180-
let mut res = InfoHash([0u8; 20]);
181-
182-
if binascii::hex2bin(v.as_bytes(), &mut res.0).is_err() {
183-
return Err(serde::de::Error::invalid_value(
184-
serde::de::Unexpected::Str(v),
185-
&"expected a hexadecimal string",
186-
));
187-
};
188-
Ok(res)
189-
}
190-
}

0 commit comments

Comments
 (0)