mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-02 10:45:30 +00:00
Run rustfmt, clean up aquatic_http_protocol/Cargo.toml
This commit is contained in:
parent
0cc312a78d
commit
d0e716f80b
65 changed files with 1754 additions and 2590 deletions
|
|
@ -1,20 +1,20 @@
|
|||
use serde::{Serializer, Deserializer, de::Visitor};
|
||||
use serde::{de::Visitor, Deserializer, Serializer};
|
||||
|
||||
use super::{AnnounceAction, ScrapeAction};
|
||||
|
||||
|
||||
pub struct AnnounceActionVisitor;
|
||||
|
||||
|
||||
impl<'de> Visitor<'de> for AnnounceActionVisitor {
|
||||
type Value = AnnounceAction;
|
||||
|
||||
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
formatter.write_str("string with value 'announce'")
|
||||
}
|
||||
|
||||
|
||||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
||||
where E: ::serde::de::Error, {
|
||||
where
|
||||
E: ::serde::de::Error,
|
||||
{
|
||||
if v == "announce" {
|
||||
Ok(AnnounceAction)
|
||||
} else {
|
||||
|
|
@ -23,19 +23,19 @@ impl<'de> Visitor<'de> for AnnounceActionVisitor {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
pub struct ScrapeActionVisitor;
|
||||
|
||||
|
||||
impl<'de> Visitor<'de> for ScrapeActionVisitor {
|
||||
type Value = ScrapeAction;
|
||||
|
||||
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
formatter.write_str("string with value 'scrape'")
|
||||
}
|
||||
|
||||
|
||||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
||||
where E: ::serde::de::Error, {
|
||||
where
|
||||
E: ::serde::de::Error,
|
||||
{
|
||||
if v == "scrape" {
|
||||
Ok(ScrapeAction)
|
||||
} else {
|
||||
|
|
@ -44,17 +44,15 @@ impl<'de> Visitor<'de> for ScrapeActionVisitor {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn serialize_20_bytes<S>(
|
||||
data: &[u8; 20],
|
||||
serializer: S
|
||||
) -> Result<S::Ok, S::Error> where S: Serializer {
|
||||
pub fn serialize_20_bytes<S>(data: &[u8; 20], serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let text: String = data.iter().map(|byte| char::from(*byte)).collect();
|
||||
|
||||
serializer.serialize_str(&text)
|
||||
}
|
||||
|
||||
|
||||
struct TwentyByteVisitor;
|
||||
|
||||
impl<'de> Visitor<'de> for TwentyByteVisitor {
|
||||
|
|
@ -66,7 +64,8 @@ impl<'de> Visitor<'de> for TwentyByteVisitor {
|
|||
|
||||
#[inline]
|
||||
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
|
||||
where E: ::serde::de::Error,
|
||||
where
|
||||
E: ::serde::de::Error,
|
||||
{
|
||||
// Value is encoded in nodejs reference client something as follows:
|
||||
// ```
|
||||
|
|
@ -83,8 +82,8 @@ impl<'de> Visitor<'de> for TwentyByteVisitor {
|
|||
let mut arr = [0u8; 20];
|
||||
let mut char_iter = value.chars();
|
||||
|
||||
for a in arr.iter_mut(){
|
||||
if let Some(c) = char_iter.next(){
|
||||
for a in arr.iter_mut() {
|
||||
if let Some(c) = char_iter.next() {
|
||||
if c as u32 > 255 {
|
||||
return Err(E::custom(format!(
|
||||
"character not in single byte range: {:#?}",
|
||||
|
|
@ -102,17 +101,14 @@ impl<'de> Visitor<'de> for TwentyByteVisitor {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
pub fn deserialize_20_bytes<'de, D>(
|
||||
deserializer: D
|
||||
) -> Result<[u8; 20], D::Error>
|
||||
where D: Deserializer<'de>
|
||||
pub fn deserialize_20_bytes<'de, D>(deserializer: D) -> Result<[u8; 20], D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_any(TwentyByteVisitor)
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use quickcheck_macros::quickcheck;
|
||||
|
|
@ -130,7 +126,7 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_20_bytes(){
|
||||
fn test_deserialize_20_bytes() {
|
||||
let mut input = r#""aaaabbbbccccddddeeee""#.to_string();
|
||||
|
||||
let expected = info_hash_from_bytes(b"aaaabbbbccccddddeeee");
|
||||
|
|
@ -150,7 +146,7 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_serde_20_bytes(){
|
||||
fn test_serde_20_bytes() {
|
||||
let info_hash = info_hash_from_bytes(b"aaaabbbbccccddddeeee");
|
||||
|
||||
let mut out = ::simd_json::serde::to_string(&info_hash).unwrap();
|
||||
|
|
@ -166,5 +162,4 @@ mod tests {
|
|||
|
||||
info_hash == info_hash_2
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue