diff --git a/.github/workflows/ci-version.yml b/.github/workflows/ci-version.yml index b5706c2..d713ff4 100644 --- a/.github/workflows/ci-version.yml +++ b/.github/workflows/ci-version.yml @@ -24,16 +24,20 @@ jobs: - - --features u128 - --features serde + - --features rocket - --features bit - --features bit --features serde + - --features bit --features rocket - --features u128 --features serde - --features u128 --features bit - --features u128 --features bit --features serde - --no-default-features - --no-default-features --features byte - --no-default-features --features byte --features serde + - --no-default-features --features byte --features rocket - --no-default-features --features bit - --no-default-features --features bit --features serde + - --no-default-features --features bit --features rocket - --no-default-features --features byte --features bit - --no-default-features --features u128 - --no-default-features --features u128 --features byte @@ -60,21 +64,25 @@ jobs: - macos-latest - windows-latest toolchain: - - "1.60" + - 1.69 features: - - --features u128 - --features serde + - --features rocket - --features bit - --features bit --features serde + - --features bit --features rocket - --features u128 --features serde - --features u128 --features bit - --features u128 --features bit --features serde - --no-default-features - --no-default-features --features byte - --no-default-features --features byte --features serde + - --no-default-features --features byte --features rocket - --no-default-features --features bit - --no-default-features --features bit --features serde + - --no-default-features --features bit --features rocket - --no-default-features --features byte --features bit - --no-default-features --features u128 - --no-default-features --features u128 --features byte diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b36a48a..b15e712 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -40,16 +40,20 @@ jobs: - - --features u128 - --features serde + - --features rocket - --features bit - --features bit --features serde + - --features bit --features rocket - --features u128 --features serde - --features u128 --features bit - --features u128 --features bit --features serde - --no-default-features - --no-default-features --features byte - --no-default-features --features byte --features serde + - --no-default-features --features byte --features rocket - --no-default-features --features bit - --no-default-features --features bit --features serde + - --no-default-features --features bit --features rocket - --no-default-features --features byte --features bit - --no-default-features --features u128 - --no-default-features --features u128 --features byte @@ -76,21 +80,25 @@ jobs: - macos-latest - windows-latest toolchain: - - "1.60" + - 1.69 features: - - --features u128 - --features serde + - --features rocket - --features bit - --features bit --features serde + - --features bit --features rocket - --features u128 --features serde - --features u128 --features bit - --features u128 --features bit --features serde - --no-default-features - --no-default-features --features byte - --no-default-features --features byte --features serde + - --no-default-features --features byte --features rocket - --no-default-features --features bit - --no-default-features --features bit --features serde + - --no-default-features --features bit --features rocket - --no-default-features --features byte --features bit - --no-default-features --features u128 - --no-default-features --features u128 --features byte diff --git a/Cargo.toml b/Cargo.toml index 2c61d9a..4d3e122 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,9 @@ [package] name = "byte-unit" -version = "5.0.4" +version = "5.1.0" authors = ["Magic Len "] edition = "2021" -rust-version = "1.60" +rust-version = "1.69" repository = "https://github.com/magiclen/byte-unit" homepage = "https://magiclen.org/byte-unit" keywords = ["byte", "unit", "kb", "mb", "gb"] @@ -16,6 +16,7 @@ include = ["src/**/*", "Cargo.toml", "README.md", "LICENSE"] utf8-width = "0.1" serde = { version = "1", default-features = false, features = ["alloc"], optional = true } +rocket = { version = "0.5", optional = true } rust_decimal = { version = "1", default-features = false, optional = true } [dev-dependencies] @@ -25,9 +26,10 @@ serde_json = "1" default = ["std", "byte"] serde = ["dep:serde"] +rocket = ["dep:rocket", "std"] rust_decimal = ["dep:rust_decimal"] -std = ["serde?/std"] +std = ["serde?/std", "rust_decimal?/std"] u128 = [] byte = ["rust_decimal"] bit = ["rust_decimal"] diff --git a/src/bit/adjusted/mod.rs b/src/bit/adjusted/mod.rs index 1494bf5..96ab36e 100644 --- a/src/bit/adjusted/mod.rs +++ b/src/bit/adjusted/mod.rs @@ -1,6 +1,8 @@ mod built_in_traits; +#[cfg(feature = "rocket")] +mod rocket_traits; #[cfg(feature = "serde")] -mod serde; +mod serde_traits; use core::{ cmp::Ordering, diff --git a/src/bit/adjusted/rocket_traits.rs b/src/bit/adjusted/rocket_traits.rs new file mode 100644 index 0000000..f946092 --- /dev/null +++ b/src/bit/adjusted/rocket_traits.rs @@ -0,0 +1,25 @@ +use std::str::FromStr; + +use rocket::{ + form::{self, FromFormField, ValueField}, + request::FromParam, +}; + +use super::AdjustedBit; +use crate::ParseError; + +impl<'r> FromParam<'r> for AdjustedBit { + type Error = ParseError; + + #[inline] + fn from_param(v: &'r str) -> Result { + Self::from_str(v) + } +} + +impl<'r> FromFormField<'r> for AdjustedBit { + #[inline] + fn from_value(v: ValueField<'r>) -> form::Result<'r, Self> { + Ok(Self::from_str(v.value).map_err(form::Error::custom)?) + } +} diff --git a/src/bit/adjusted/serde.rs b/src/bit/adjusted/serde_traits.rs similarity index 100% rename from src/bit/adjusted/serde.rs rename to src/bit/adjusted/serde_traits.rs diff --git a/src/bit/mod.rs b/src/bit/mod.rs index fb9c12f..0339e31 100644 --- a/src/bit/mod.rs +++ b/src/bit/mod.rs @@ -3,8 +3,10 @@ mod built_in_traits; mod constants; mod decimal; mod parse; +#[cfg(feature = "rocket")] +mod rocket_traits; #[cfg(feature = "serde")] -mod serde; +mod serde_traits; use core::fmt::{self, Alignment, Display, Formatter, Write}; diff --git a/src/bit/rocket_traits.rs b/src/bit/rocket_traits.rs new file mode 100644 index 0000000..d118d35 --- /dev/null +++ b/src/bit/rocket_traits.rs @@ -0,0 +1,25 @@ +use std::str::FromStr; + +use rocket::{ + form::{self, FromFormField, ValueField}, + request::FromParam, +}; + +use super::Bit; +use crate::ParseError; + +impl<'r> FromParam<'r> for Bit { + type Error = ParseError; + + #[inline] + fn from_param(v: &'r str) -> Result { + Self::from_str(v) + } +} + +impl<'r> FromFormField<'r> for Bit { + #[inline] + fn from_value(v: ValueField<'r>) -> form::Result<'r, Self> { + Ok(Self::from_str(v.value).map_err(form::Error::custom)?) + } +} diff --git a/src/bit/serde.rs b/src/bit/serde_traits.rs similarity index 100% rename from src/bit/serde.rs rename to src/bit/serde_traits.rs diff --git a/src/byte/adjusted/mod.rs b/src/byte/adjusted/mod.rs index 9a1a7ad..9b70487 100644 --- a/src/byte/adjusted/mod.rs +++ b/src/byte/adjusted/mod.rs @@ -1,6 +1,8 @@ mod built_in_traits; +#[cfg(feature = "rocket")] +mod rocket_traits; #[cfg(feature = "serde")] -mod serde; +mod serde_traits; use core::{ cmp::Ordering, diff --git a/src/byte/adjusted/rocket_traits.rs b/src/byte/adjusted/rocket_traits.rs new file mode 100644 index 0000000..6d14d3f --- /dev/null +++ b/src/byte/adjusted/rocket_traits.rs @@ -0,0 +1,25 @@ +use std::str::FromStr; + +use rocket::{ + form::{self, FromFormField, ValueField}, + request::FromParam, +}; + +use super::AdjustedByte; +use crate::ParseError; + +impl<'r> FromParam<'r> for AdjustedByte { + type Error = ParseError; + + #[inline] + fn from_param(v: &'r str) -> Result { + Self::from_str(v) + } +} + +impl<'r> FromFormField<'r> for AdjustedByte { + #[inline] + fn from_value(v: ValueField<'r>) -> form::Result<'r, Self> { + Ok(Self::from_str(v.value).map_err(form::Error::custom)?) + } +} diff --git a/src/byte/adjusted/serde.rs b/src/byte/adjusted/serde_traits.rs similarity index 100% rename from src/byte/adjusted/serde.rs rename to src/byte/adjusted/serde_traits.rs diff --git a/src/byte/mod.rs b/src/byte/mod.rs index b11369d..fb36ed7 100644 --- a/src/byte/mod.rs +++ b/src/byte/mod.rs @@ -3,8 +3,10 @@ mod built_in_traits; mod constants; mod decimal; mod parse; +#[cfg(feature = "rocket")] +mod rocket_traits; #[cfg(feature = "serde")] -mod serde; +mod serde_traits; use core::fmt::{self, Alignment, Display, Formatter, Write}; diff --git a/src/byte/rocket_traits.rs b/src/byte/rocket_traits.rs new file mode 100644 index 0000000..5f3c265 --- /dev/null +++ b/src/byte/rocket_traits.rs @@ -0,0 +1,25 @@ +use std::str::FromStr; + +use rocket::{ + form::{self, FromFormField, ValueField}, + request::FromParam, +}; + +use super::Byte; +use crate::ParseError; + +impl<'r> FromParam<'r> for Byte { + type Error = ParseError; + + #[inline] + fn from_param(v: &'r str) -> Result { + Self::from_str(v) + } +} + +impl<'r> FromFormField<'r> for Byte { + #[inline] + fn from_value(v: ValueField<'r>) -> form::Result<'r, Self> { + Ok(Self::from_str(v.value).map_err(form::Error::custom)?) + } +} diff --git a/src/byte/serde.rs b/src/byte/serde_traits.rs similarity index 100% rename from src/byte/serde.rs rename to src/byte/serde_traits.rs diff --git a/src/lib.rs b/src/lib.rs index 56e90cf..5892bbf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -197,7 +197,6 @@ features = ["serde"] #[cfg(feature = "serde")] #[macro_use] extern crate alloc; -extern crate core; #[cfg(feature = "rust_decimal")] pub extern crate rust_decimal; diff --git a/src/unit/mod.rs b/src/unit/mod.rs index f6947ac..e6a605b 100644 --- a/src/unit/mod.rs +++ b/src/unit/mod.rs @@ -1,7 +1,9 @@ mod built_in_trait; pub(crate) mod parse; +#[cfg(feature = "rocket")] +mod rocket_traits; #[cfg(feature = "serde")] -mod serde; +mod serde_traits; #[cfg(any(feature = "byte", feature = "bit"))] mod unit_type; diff --git a/src/unit/rocket_traits.rs b/src/unit/rocket_traits.rs new file mode 100644 index 0000000..be2bbc9 --- /dev/null +++ b/src/unit/rocket_traits.rs @@ -0,0 +1,25 @@ +use std::str::FromStr; + +use rocket::{ + form::{self, FromFormField, ValueField}, + request::FromParam, +}; + +use super::Unit; +use crate::UnitParseError; + +impl<'r> FromParam<'r> for Unit { + type Error = UnitParseError; + + #[inline] + fn from_param(v: &'r str) -> Result { + Self::from_str(v) + } +} + +impl<'r> FromFormField<'r> for Unit { + #[inline] + fn from_value(v: ValueField<'r>) -> form::Result<'r, Self> { + Ok(Self::from_str(v.value).map_err(form::Error::custom)?) + } +} diff --git a/src/unit/serde.rs b/src/unit/serde_traits.rs similarity index 100% rename from src/unit/serde.rs rename to src/unit/serde_traits.rs