Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: hack-ink/array-bytes
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v9.1.0
Choose a base ref
...
head repository: hack-ink/array-bytes
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
  • 7 commits
  • 6 files changed
  • 2 contributors

Commits on Jan 3, 2025

  1. Release v9.1.1

    aurexav committed Jan 3, 2025

    Verified

    This commit was signed with the committer’s verified signature.
    aurexav Xavier Lau
    Copy the full SHA
    2436a35 View commit details
  2. Release v9.1.2

    aurexav committed Jan 3, 2025

    Verified

    This commit was signed with the committer’s verified signature.
    aurexav Xavier Lau
    Copy the full SHA
    3cc642f View commit details

Commits on Jan 25, 2025

  1. Enable merge queue

    Signed-off-by: Xavier Lau <[email protected]>
    aurexav authored Jan 25, 2025

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    f02b83c View commit details
  2. Bump serde_json from 1.0.134 to 1.0.137 (#182)

    Bumps [serde_json](https://github.com/serde-rs/json) from 1.0.134 to 1.0.137.
    - [Release notes](https://github.com/serde-rs/json/releases)
    - [Commits](serde-rs/json@v1.0.134...v1.0.137)
    
    ---
    updated-dependencies:
    - dependency-name: serde_json
      dependency-type: direct:production
      update-type: version-update:semver-patch
    ...
    
    Signed-off-by: dependabot[bot] <[email protected]>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    Co-authored-by: Xavier Lau <[email protected]>
    dependabot[bot] and aurexav authored Jan 25, 2025

    Partially verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    We cannot verify signatures from co-authors, and some of the co-authors attributed to this commit require their commits to be signed.
    Copy the full SHA
    8631a46 View commit details

Commits on Feb 25, 2025

  1. Bump serde_json from 1.0.137 to 1.0.139 (#186)

    Bumps [serde_json](https://github.com/serde-rs/json) from 1.0.137 to 1.0.139.
    - [Release notes](https://github.com/serde-rs/json/releases)
    - [Commits](serde-rs/json@v1.0.137...v1.0.139)
    
    ---
    updated-dependencies:
    - dependency-name: serde_json
      dependency-type: direct:production
      update-type: version-update:semver-patch
    ...
    
    Signed-off-by: dependabot[bot] <[email protected]>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Feb 25, 2025

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    3844df2 View commit details
  2. Bump serde from 1.0.217 to 1.0.218 (#185)

    Bumps [serde](https://github.com/serde-rs/serde) from 1.0.217 to 1.0.218.
    - [Release notes](https://github.com/serde-rs/serde/releases)
    - [Commits](serde-rs/serde@v1.0.217...v1.0.218)
    
    ---
    updated-dependencies:
    - dependency-name: serde
      dependency-type: direct:production
      update-type: version-update:semver-patch
    ...
    
    Signed-off-by: dependabot[bot] <[email protected]>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Feb 25, 2025

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    838d833 View commit details
  3. Bump smallvec from 1.13.2 to 1.14.0 (#184)

    Bumps [smallvec](https://github.com/servo/rust-smallvec) from 1.13.2 to 1.14.0.
    - [Release notes](https://github.com/servo/rust-smallvec/releases)
    - [Commits](servo/rust-smallvec@v1.13.2...v1.14.0)
    
    ---
    updated-dependencies:
    - dependency-name: smallvec
      dependency-type: direct:production
      update-type: version-update:semver-minor
    ...
    
    Signed-off-by: dependabot[bot] <[email protected]>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Feb 25, 2025

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    aea7327 View commit details
Showing with 98 additions and 16 deletions.
  1. +1 −0 .github/workflows/checks.yml
  2. +6 −0 CHANGELOG
  3. +19 −9 Cargo.lock
  4. +11 −4 Cargo.toml
  5. +2 −0 src/lib.rs
  6. +59 −3 src/serde.rs
1 change: 1 addition & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ on:
pull_request:
branches:
- main
merge_group:

jobs:
cargo-checks:
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### v9.1.2
- Add `ser_bytes_stringify` and `de_bytes_destringify`.

### v9.1.1
- Integrate `serde_bytes`.

### v9.1.0
- Use reference instead of value in `Hexify` trait and related serialize functions.

28 changes: 19 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -3,8 +3,8 @@ authors = ["Xavier Lau <x@acg.box>"]
categories = [
"algorithms",
"encoding",
"parsing",
"no-std",
"parsing",
"wasm",
]
description = "A collection of Array/Bytes/Hex utilities with full No-STD compatibility."
@@ -21,15 +21,22 @@ license = "Apache-2.0/GPL-3.0"
name = "array-bytes"
readme = "README.md"
repository = "https://github.com/hack-ink/array-bytes"
version = "9.1.0"
version = "9.1.2"

[profile.ci-dev]
incremental = false
inherits = "dev"

[features]
serde = [
"dep:serde",
"serde_bytes",
]

[dependencies]
serde = { version = "1.0", optional = true, default-features = false }
smallvec = { version = "1.13" }
serde = { version = "1.0", optional = true, default-features = false }
serde_bytes = { version = "0.11", optional = true, default-features = false, features = ["alloc"] }
smallvec = { version = "1.14" }

[dev-dependencies]
const-hex = { version = "1.14" }
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -60,6 +60,8 @@ mod prelude {
#[cfg(test)] pub use test::*;
}

#[cfg(feature = "serde")] pub use serde_bytes;

#[allow(missing_docs)]
pub type Result<T, E = Error> = core::result::Result<T, E>;

62 changes: 59 additions & 3 deletions src/serde.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
// core
use core::str;
// alloc
use alloc::format;
// crates.io
#[cfg(test)] use serde::Serialize;
use serde::{de::Error as DeError, Deserialize, Deserializer, Serializer};
use serde::{de::Error as _, ser::Error as _, Deserialize, Deserializer, Serializer};
// self
use crate::{prelude::*, Dehexify, Hexify};

/// Serialize bytes to string.
///
/// # Examples
/// ```
/// use serde::Serialize;
///
/// #[derive(Debug, PartialEq, Serialize)]
/// struct Ljf {
/// #[serde(serialize_with = "array_bytes::ser_bytes_stringify")]
/// _0: Vec<u8>,
/// }
///
/// assert_eq!(
/// serde_json::to_string::<Ljf>(&Ljf { _0: b"Love Jane Forever".to_vec() }).unwrap(),
/// r#"{"_0":"Love Jane Forever"}"#
/// );
/// ```
pub fn ser_bytes_stringify<S, T>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: AsRef<[u8]>,
{
serializer.serialize_str(str::from_utf8(value.as_ref()).map_err(S::Error::custom)?)
}

/// Serialize `T` to hex.
///
/// # Examples
@@ -130,6 +157,32 @@ where
serializer.serialize_str(&value.hexify_prefixed())
}

/// Deserialize string to bytes.
///
/// # Examples
/// ```
/// use serde::Deserialize;
///
/// #[derive(Debug, PartialEq, Deserialize)]
/// struct Ljf {
/// #[serde(deserialize_with = "array_bytes::de_bytes_destringify")]
/// _0: Vec<u8>,
/// }
///
/// assert_eq!(
/// serde_json::from_str::<Ljf>(r#"{"_0":"Love Jane Forever"}"#).unwrap(),
/// Ljf { _0: b"Love Jane Forever".to_vec() }
/// );
/// ```
pub fn de_bytes_destringify<'de, D>(str: D) -> Result<Vec<u8>, D::Error>
where
D: Deserializer<'de>,
{
let str = <&str>::deserialize(str)?;

Ok(str.as_bytes().to_vec())
}

/// Deserialize hex to `T`.
///
/// # Examples
@@ -168,7 +221,7 @@ where
{
let hex = <&str>::deserialize(hex)?;

T::dehexify(hex).map_err(|_| D::Error::custom(alloc::format!("invalid hex str `{}`", hex)))
T::dehexify(hex).map_err(|e| D::Error::custom(alloc::format!("{e:?}")))
}

/// Deserialize hex to `T` where `T: From<Vec<u8>>`.
@@ -323,6 +376,8 @@ fn serde_should_work() {
_6: [u8; 17],
#[serde(deserialize_with = "de_dehexify", serialize_with = "ser_hexify_prefixed_upper")]
_7: Vec<u8>,
#[serde(deserialize_with = "de_bytes_destringify", serialize_with = "ser_bytes_stringify")]
_8: Vec<u8>,
}
impl Default for LjfPredefined {
fn default() -> Self {
@@ -335,6 +390,7 @@ fn serde_should_work() {
_5: 5_201_314,
_6: *b"Love Jane Forever",
_7: b"Love Jane Forever".to_vec(),
_8: b"Love Jane Forever".to_vec(),
}
}
}
@@ -346,7 +402,7 @@ fn serde_should_work() {
let json = result.unwrap();
assert_eq!(
json,
r#"{"_0":"34","_1":"208","_2":"0x4f5da2","_3":"0x4f5da2","_4":"4f5da2","_5":"4F5DA2","_6":"0x4c6f7665204a616e6520466f7265766572","_7":"0x4c6f7665204a616e6520466f7265766572"}"#
r#"{"_0":"34","_1":"208","_2":"0x4f5da2","_3":"0x4f5da2","_4":"4f5da2","_5":"4F5DA2","_6":"0x4c6f7665204a616e6520466f7265766572","_7":"0x4c6f7665204a616e6520466f7265766572","_8":"Love Jane Forever"}"#
);

let result = serde_json::from_str::<LjfPredefined>(&json);