mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-02 10:45:30 +00:00
aquatic_http: deserialize AnnounceRequest.compact to bool
This commit is contained in:
parent
6fc4b693cd
commit
ff4fcc163d
3 changed files with 40 additions and 3 deletions
2
TODO.md
2
TODO.md
|
|
@ -16,7 +16,7 @@
|
||||||
* move stuff to common crate with ws: what about Request/InMessage etc?
|
* move stuff to common crate with ws: what about Request/InMessage etc?
|
||||||
* info hashes, peer ids: check that whole deserialization and url decoding
|
* info hashes, peer ids: check that whole deserialization and url decoding
|
||||||
works as it should. There are suspicously many `\u{fffd}`
|
works as it should. There are suspicously many `\u{fffd}`
|
||||||
* AnnounceRequest.compact: parse int to bool
|
* AnnounceRequest.compact: handle that it is optional
|
||||||
|
|
||||||
## aquatic_ws
|
## aquatic_ws
|
||||||
* tests
|
* tests
|
||||||
|
|
|
||||||
|
|
@ -74,8 +74,11 @@ pub struct AnnounceRequest {
|
||||||
pub bytes_left: usize,
|
pub bytes_left: usize,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub event: AnnounceEvent,
|
pub event: AnnounceEvent,
|
||||||
/// FIXME: number: 0 or 1 to bool
|
/// FIXME: is optional, so should default to something, maybe `true`
|
||||||
pub compact: u8,
|
#[serde(
|
||||||
|
deserialize_with = "deserialize_bool_from_number"
|
||||||
|
)]
|
||||||
|
pub compact: bool,
|
||||||
/// Requested number of peers to return
|
/// Requested number of peers to return
|
||||||
pub numwant: usize,
|
pub numwant: usize,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,40 @@ use serde::{Serializer, Deserializer, de::{Visitor, SeqAccess}};
|
||||||
use super::{InfoHash, ResponsePeer};
|
use super::{InfoHash, ResponsePeer};
|
||||||
|
|
||||||
|
|
||||||
|
struct BoolFromNumberVisitor;
|
||||||
|
|
||||||
|
impl<'de> Visitor<'de> for BoolFromNumberVisitor {
|
||||||
|
type Value = bool;
|
||||||
|
|
||||||
|
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
|
formatter.write_str("1 for true, 0 for false")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
|
||||||
|
where E: ::serde::de::Error,
|
||||||
|
{
|
||||||
|
if value == "0" {
|
||||||
|
Ok(false)
|
||||||
|
} else if value == "1" {
|
||||||
|
Ok(true)
|
||||||
|
} else {
|
||||||
|
Err(E::custom(format!("not 0 or 1: {}", value)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn deserialize_bool_from_number<'de, D>(
|
||||||
|
deserializer: D
|
||||||
|
) -> Result<bool, D::Error>
|
||||||
|
where D: Deserializer<'de>
|
||||||
|
{
|
||||||
|
deserializer.deserialize_any(BoolFromNumberVisitor)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
struct TwentyCharStringVisitor;
|
struct TwentyCharStringVisitor;
|
||||||
|
|
||||||
impl<'de> Visitor<'de> for TwentyCharStringVisitor {
|
impl<'de> Visitor<'de> for TwentyCharStringVisitor {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue