Skip to content

Commit

Permalink
move ReservedBytes from standard library
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Feb 11, 2024
1 parent cd603b7 commit e8f7c2a
Showing 1 changed file with 62 additions and 6 deletions.
68 changes: 62 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,35 @@ pub use prelude::*;

pub const LIB_NAME_RGB: &str = "RGB";

/// Reserved byte.
// TODO: Move to amplify crate

/// Reserved bytes.
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display)]

Check warning on line 69 in src/lib.rs

View check run for this annotation

Codecov / codecov/patch

src/lib.rs#L69

Added line #L69 was not covered by tests
#[display("reserved")]
#[derive(StrictType, StrictEncode)]
#[strict_type(lib = LIB_NAME_RGB)]
pub struct ReservedBytes<const LEN: usize>([u8; LEN]);
pub struct ReservedBytes<const LEN: usize, const VAL: u8 = 0>([u8; LEN]);

impl<const LEN: usize> Default for ReservedBytes<LEN> {
fn default() -> Self { Self([0; LEN]) }
impl<const LEN: usize, const VAL: u8> Default for ReservedBytes<LEN, VAL> {
fn default() -> Self { Self([VAL; LEN]) }
}

impl<const LEN: usize, const VAL: u8> From<[u8; LEN]> for ReservedBytes<LEN, VAL> {
fn from(value: [u8; LEN]) -> Self {
assert_eq!(value, [VAL; LEN]);
Self(value)
}

Check warning on line 83 in src/lib.rs

View check run for this annotation

Codecov / codecov/patch

src/lib.rs#L80-L83

Added lines #L80 - L83 were not covered by tests
}

mod _reserved {
use strict_encoding::{DecodeError, ReadTuple, StrictDecode, TypedRead};

use crate::ReservedBytes;

impl<const LEN: usize> StrictDecode for ReservedBytes<LEN> {
impl<const LEN: usize, const VAL: u8> StrictDecode for ReservedBytes<LEN, VAL> {
fn strict_decode(reader: &mut impl TypedRead) -> Result<Self, DecodeError> {
let reserved = reader.read_tuple(|r| r.read_field().map(Self))?;
if reserved != ReservedBytes::<LEN>::default() {
if reserved != ReservedBytes::<LEN, VAL>::default() {

Check warning on line 94 in src/lib.rs

View check run for this annotation

Codecov / codecov/patch

src/lib.rs#L94

Added line #L94 was not covered by tests
Err(DecodeError::DataIntegrityError(format!(
"unsupported reserved byte value indicating a future RGB version. Please \
update your software, or, if the problem persists, contact your vendor \
Expand All @@ -93,6 +102,53 @@ mod _reserved {
}
}
}

#[cfg(feature = "serde")]
mod _serde {
use std::fmt;

use serde_crate::de::Visitor;
use serde_crate::{de, Deserialize, Deserializer, Serialize, Serializer};

use super::*;

impl<const LEN: usize, const VAL: u8> Serialize for ReservedBytes<LEN, VAL> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer {
// Doing nothing
serializer.serialize_unit()
}

Check warning on line 120 in src/lib.rs

View check run for this annotation

Codecov / codecov/patch

src/lib.rs#L116-L120

Added lines #L116 - L120 were not covered by tests
}

impl<'de, const LEN: usize, const VAL: u8> Deserialize<'de> for ReservedBytes<LEN, VAL> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de> {
#[derive(Default)]
pub struct UntaggedUnitVisitor;

impl<'de> Visitor<'de> for UntaggedUnitVisitor {
type Value = ();

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "reserved unit")
}

fn visit_none<E>(self) -> Result<(), E>
where E: de::Error {
Ok(())
}

fn visit_unit<E>(self) -> Result<(), E>
where E: de::Error {
Ok(())
}
}

deserializer.deserialize_unit(UntaggedUnitVisitor)?;
Ok(default!())
}

Check warning on line 149 in src/lib.rs

View check run for this annotation

Codecov / codecov/patch

src/lib.rs#L124-L149

Added lines #L124 - L149 were not covered by tests
}
}
}

/// Fast-forward version code
Expand Down

0 comments on commit e8f7c2a

Please sign in to comment.