Skip to content

Commit cfd5aec

Browse files
LPGhatguycwfitzgerald
andauthoredDec 6, 2024··
Implement encoding for more types (#14)
Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com>
1 parent ca7d5cd commit cfd5aec

File tree

3 files changed

+212
-111
lines changed

3 files changed

+212
-111
lines changed
 

‎src/enums.rs

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1-
use core::{fmt, num::NonZeroU32};
1+
use core::{
2+
fmt,
3+
num::{NonZeroU32, NonZeroU8},
4+
};
25

36
macro_rules! pseudo_enum {
4-
($(#[$attr:meta])* $name:ident { $($case:ident = $value:literal,)* }) => {
7+
($(#[$attr:meta])* $container:ident($prim:ident) $name:ident { $($case:ident = $value:literal,)* }) => {
58
$(#[$attr])*
69
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
7-
pub struct $name(pub NonZeroU32);
10+
pub struct $name($container);
11+
812
#[allow(non_upper_case_globals)]
913
impl $name {
10-
pub fn new(x: u32) -> Option<Self> {
11-
Some(Self(NonZeroU32::new(x)?))
14+
pub fn new(x: $prim) -> Option<Self> {
15+
Some(Self($container::new(x)?))
16+
}
17+
18+
pub fn value(&self) -> $prim {
19+
self.0.get()
1220
}
1321

1422
$(
15-
pub const $case: Self = Self(unsafe { NonZeroU32::new_unchecked($value) });
23+
pub const $case: Self = Self(unsafe { $container::new_unchecked($value) });
1624
)*
1725
}
1826

@@ -33,7 +41,7 @@ macro_rules! pseudo_enum {
3341

3442
pseudo_enum! {
3543
/// Known texture formats
36-
Format {
44+
NonZeroU32(u32) Format {
3745
R4G4_UNORM_PACK8 = 1,
3846
R4G4B4A4_UNORM_PACK16 = 2,
3947
B4G4R4A4_UNORM_PACK16 = 3,
@@ -206,15 +214,15 @@ pseudo_enum! {
206214

207215
pseudo_enum! {
208216
/// Known supercompression schemes
209-
SupercompressionScheme {
217+
NonZeroU32(u32) SupercompressionScheme {
210218
BasisLZ = 1,
211219
Zstandard = 2,
212220
ZLIB = 3,
213221
}
214222
}
215223

216224
pseudo_enum! {
217-
ColorModel {
225+
NonZeroU8(u8) ColorModel {
218226
RGBSDA = 1,
219227
YUVSDA = 2,
220228
YIQSDA = 3,
@@ -248,7 +256,7 @@ pseudo_enum! {
248256
}
249257

250258
pseudo_enum! {
251-
ColorPrimaries {
259+
NonZeroU8(u8) ColorPrimaries {
252260
BT709 = 1,
253261
BT601EBU = 2,
254262
BT601SMPTE = 3,
@@ -264,7 +272,7 @@ pseudo_enum! {
264272
}
265273

266274
pseudo_enum! {
267-
TransferFunction {
275+
NonZeroU8(u8) TransferFunction {
268276
Linear = 1,
269277
SRGB = 2,
270278
ITU = 3,

‎src/error.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use core::fmt;
22
#[cfg(feature = "std")]
33
use std::error::Error;
44

5-
/// Error, that happend when data doesn't satisfy expected parameters.
5+
/// Error, that happened when data doesn't satisfy expected parameters.
66
#[derive(Debug)]
77
#[non_exhaustive]
88
pub enum ParseError {
@@ -12,6 +12,8 @@ pub enum ParseError {
1212
ZeroWidth,
1313
/// Zero face count
1414
ZeroFaceCount,
15+
/// Data Format Descriptor had an invalid sample bit length.
16+
InvalidSampleBitLength,
1517
/// Unexpected end of buffer
1618
UnexpectedEnd,
1719
}
@@ -25,6 +27,7 @@ impl fmt::Display for ParseError {
2527
ParseError::BadMagic => f.pad("unexpected magic numbers"),
2628
ParseError::ZeroWidth => f.pad("zero pixel width"),
2729
ParseError::ZeroFaceCount => f.pad("zero face count"),
30+
ParseError::InvalidSampleBitLength => f.pad("invalid sample bit length"),
2831
ParseError::UnexpectedEnd => f.pad("unexpected end of buffer"),
2932
}
3033
}

0 commit comments

Comments
 (0)