mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-01 02:05:30 +00:00
71 lines
No EOL
1.4 KiB
Rust
71 lines
No EOL
1.4 KiB
Rust
use std::str::FromStr;
|
|
|
|
use serde::Serialize;
|
|
|
|
use super::utils::*;
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Serialize)]
|
|
#[serde(transparent)]
|
|
pub struct PeerId(
|
|
#[serde(
|
|
serialize_with = "serialize_20_bytes",
|
|
)]
|
|
pub [u8; 20]
|
|
);
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize)]
|
|
#[serde(transparent)]
|
|
pub struct InfoHash(
|
|
#[serde(
|
|
serialize_with = "serialize_20_bytes",
|
|
)]
|
|
pub [u8; 20]
|
|
);
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub enum AnnounceEvent {
|
|
Started,
|
|
Stopped,
|
|
Completed,
|
|
Empty
|
|
}
|
|
|
|
|
|
impl Default for AnnounceEvent {
|
|
fn default() -> Self {
|
|
Self::Empty
|
|
}
|
|
}
|
|
|
|
|
|
impl FromStr for AnnounceEvent {
|
|
type Err = String;
|
|
|
|
fn from_str(value: &str) -> std::result::Result<Self, String> {
|
|
match value {
|
|
"started" => Ok(Self::Started),
|
|
"stopped" => Ok(Self::Stopped),
|
|
"completed" => Ok(Self::Completed),
|
|
"empty" => Ok(Self::Empty),
|
|
value => Err(format!("Unknown value: {}", value))
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
#[cfg(test)]
|
|
impl quickcheck::Arbitrary for InfoHash {
|
|
fn arbitrary<G: quickcheck::Gen>(g: &mut G) -> Self {
|
|
let mut arr = [b'x'; 20];
|
|
|
|
arr[0] = u8::arbitrary(g);
|
|
arr[1] = u8::arbitrary(g);
|
|
arr[18] = u8::arbitrary(g);
|
|
arr[19] = u8::arbitrary(g);
|
|
|
|
Self(arr)
|
|
}
|
|
} |