about summary refs log tree commit diff stats
path: root/crates/bytes/src/lib.rs
diff options
context:
space:
mode:
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 {