diff options
| author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-07-10 15:50:16 +0200 |
|---|---|---|
| committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-07-10 15:50:16 +0200 |
| commit | 66d56d7a2b64c5ed13860d809d9bf35d86292df2 (patch) | |
| tree | 2d79d616024e1dd326afd8b7d2b8b05efb165386 /crates/bytes/src/lib.rs | |
| parent | build(.cargo/cargo.toml): Remove pointless `PYO3_PATH` (diff) | |
| download | yt-66d56d7a2b64c5ed13860d809d9bf35d86292df2.zip | |
refactor(crates/bytes): Move into yt
`yt_dlp` no longer depends on it.
Diffstat (limited to '')
| -rw-r--r-- | crates/yt/src/shared/bytes/mod.rs (renamed from crates/bytes/src/lib.rs) | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/crates/bytes/src/lib.rs b/crates/yt/src/shared/bytes/mod.rs index 2a9248d..31e782e 100644 --- a/crates/bytes/src/lib.rs +++ b/crates/yt/src/shared/bytes/mod.rs @@ -16,6 +16,7 @@ )] use std::{fmt::Display, str::FromStr}; +use ::serde::{Deserialize, Serialize}; use error::BytesError; const B: u64 = 1; @@ -31,10 +32,11 @@ const MB: u64 = 1000 * KB; const GB: u64 = 1000 * MB; const TB: u64 = 1000 * GB; -pub mod error; -pub mod serde; +pub(crate) mod error; -#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Ord, Eq)] +#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Ord, Eq, Deserialize, Serialize)] +#[serde(try_from = "String")] +#[serde(into = "String")] pub struct Bytes(u64); impl Bytes { @@ -131,6 +133,20 @@ impl Display for Bytes { } } +impl From<Bytes> for String { + fn from(value: Bytes) -> Self { + value.to_string() + } +} + +impl TryFrom<String> for Bytes { + type Error = BytesError; + + fn try_from(value: String) -> Result<Self, Self::Error> { + value.as_str().parse() + } +} + // taken from this stack overflow question: https://stackoverflow.com/a/76572321 /// Round to significant digits (rather than digits after the decimal). /// @@ -149,7 +165,7 @@ impl Display for Bytes { ///# } /// ``` #[must_use] -pub fn precision_f64(x: f64, decimals: u32) -> f64 { +pub(crate) fn precision_f64(x: f64, decimals: u32) -> f64 { if x == 0. || decimals == 0 { 0. } else { |
