Skip to content

Commit

Permalink
Add new feature flags
Browse files Browse the repository at this point in the history
  • Loading branch information
AldaronLau committed Oct 26, 2024
1 parent 0270436 commit f42ccc1
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 9 deletions.
18 changes: 16 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,19 @@ categories = [
readme = "README.md"
rust-version = "1.70"

[dependencies]
traitful = "0.3.0"
[package.metadata.docs.rs]
all-features = true

[dependencies.traitful]
version = "0.3.0"

[features]
# Enable the `io` module, MSRV 1.84 for `Pin::as_deref_mut()`
#
# Unstable API has no stability guarantees
unstable-io = []

# Enable usage of the `Error` trait in core, MSRV 1.81
#
# Unstable API has no stability guarantees
unstable-error = []
18 changes: 17 additions & 1 deletion src/error/end.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
use core::num::NonZeroUsize;
use core::{fmt, num::NonZeroUsize};

/// Expected buffer to end, but it didn't
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)]
#[non_exhaustive]
pub struct EndError(Option<NonZeroUsize>);

/// __*`unstable-error`*__: feature required
#[cfg(feature = "unstable-error")]
impl core::error::Error for EndError {}

impl fmt::Display for EndError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("expected buffer to end, but it didn't")?;

if let Some(remaining) = self.0 {
write!(f, ", {remaining} bytes remaining")?;
}

Ok(())
}
}

impl EndError {
/// Create a new [`EndError`].
pub const fn new() -> Self {
Expand Down
17 changes: 17 additions & 0 deletions src/error/flush.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::fmt;

use crate::error::{FullError, LostError};

/// Flush error
Expand All @@ -9,6 +11,21 @@ pub enum FlushError {
Lost(LostError),
}

/// __*`unstable-error`*__: feature required
#[cfg(feature = "unstable-error")]
impl core::error::Error for FlushError {}

impl fmt::Display for FlushError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("flush error: ")?;

match self {
Self::Full(err) => fmt::Display::fmt(err, f),
Self::Lost(err) => fmt::Display::fmt(err, f),
}
}
}

impl From<FullError> for FlushError {
fn from(error: FullError) -> Self {
Self::Full(error)
Expand Down
18 changes: 17 additions & 1 deletion src/error/full.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
use core::num::NonZeroUsize;
use core::{fmt, num::NonZeroUsize};

/// Destination has run out of space
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)]
#[non_exhaustive]
pub struct FullError(Option<NonZeroUsize>);

/// __*`unstable-error`*__: feature required
#[cfg(feature = "unstable-error")]
impl core::error::Error for FullError {}

impl fmt::Display for FullError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("destination has run out of space")?;

if let Some(remaining) = self.0 {
write!(f, ", {remaining} bytes remaining")?;
}

Ok(())
}
}

impl FullError {
/// Create a new [`FullError`].
pub const fn new() -> Self {
Expand Down
18 changes: 17 additions & 1 deletion src/error/len.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
use core::num::NonZeroUsize;
use core::{fmt, num::NonZeroUsize};

/// Source ran over the end of the buffer
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)]
#[non_exhaustive]
pub struct LenError(Option<NonZeroUsize>);

/// __*`unstable-error`*__: feature required
#[cfg(feature = "unstable-error")]
impl core::error::Error for LenError {}

impl fmt::Display for LenError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("source ran over the end of the buffer")?;

if let Some(remaining) = self.0 {
write!(f, ", {remaining} bytes remaining")?;
}

Ok(())
}
}

impl LenError {
/// Create a new [`LenError`].
pub const fn new() -> Self {
Expand Down
19 changes: 19 additions & 0 deletions src/error/lost.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
use core::fmt;

/// Destination lost (from either corruption or disconnection)
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)]
#[non_exhaustive]
pub struct LostError(Option<&'static str>);

/// __*`unstable-error`*__: feature required
#[cfg(feature = "unstable-error")]
impl core::error::Error for LostError {}

impl fmt::Display for LostError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("destination lost")?;

if let Some(message) = self.0 {
f.write_str(", ")?;
f.write_str(message)?;
}

Ok(())
}
}

impl LostError {
/// Create a new [`LostError`].
pub fn new() -> Self {
Expand Down
16 changes: 14 additions & 2 deletions src/error/overflow.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
/// Overflow (variable can't contain parsed value)
use core::fmt;

/// Value overflow (variable can't contain parsed value)
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)]
#[non_exhaustive]
pub struct OverflowError();

/// __*`unstable-error`*__: feature required
#[cfg(feature = "unstable-error")]
impl core::error::Error for OverflowError {}

impl fmt::Display for OverflowError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("value overflow")
}
}

impl OverflowError {
/// Create a new [`OverflowError`].
pub fn new() -> Self {
pub const fn new() -> Self {
Self()
}
}
21 changes: 21 additions & 0 deletions src/error/parsing.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::fmt;

use crate::error::{
EndError, FlushError, FullError, LenError, LostError, OverflowError,
StrError, Uleb128Error, Utf8Error,
Expand All @@ -21,6 +23,25 @@ pub enum Error {
Lost(LostError),
}

/// __*`unstable-error`*__: feature required
#[cfg(feature = "unstable-error")]
impl core::error::Error for Error {}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("parsing error: ")?;

match self {
Self::Len(err) => fmt::Display::fmt(err, f),
Self::End(err) => fmt::Display::fmt(err, f),
Self::Utf8(err) => fmt::Display::fmt(err, f),
Self::Overflow(err) => fmt::Display::fmt(err, f),
Self::Full(err) => fmt::Display::fmt(err, f),
Self::Lost(err) => fmt::Display::fmt(err, f),
}
}
}

impl From<LenError> for Error {
fn from(error: LenError) -> Self {
Self::Len(error)
Expand Down
17 changes: 17 additions & 0 deletions src/error/str.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::fmt;

use crate::error::{LenError, Utf8Error};

/// String parsing error
Expand All @@ -9,6 +11,21 @@ pub enum StrError {
Utf8(Utf8Error),
}

/// __*`unstable-error`*__: feature required
#[cfg(feature = "unstable-error")]
impl core::error::Error for StrError {}

impl fmt::Display for StrError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("string parsing error: ")?;

match self {
Self::Len(err) => fmt::Display::fmt(err, f),
Self::Utf8(err) => fmt::Display::fmt(err, f),
}
}
}

impl From<LenError> for StrError {
fn from(error: LenError) -> Self {
Self::Len(error)
Expand Down
17 changes: 17 additions & 0 deletions src/error/uleb128.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::fmt;

use crate::error::{LenError, OverflowError};

/// ULEB128 parsing error
Expand All @@ -9,6 +11,21 @@ pub enum Uleb128Error {
Overflow(OverflowError),
}

/// __*`unstable-error`*__: feature required
#[cfg(feature = "unstable-error")]
impl core::error::Error for Uleb128Error {}

impl fmt::Display for Uleb128Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("uleb128 parsing error: ")?;

match self {
Self::Len(err) => fmt::Display::fmt(err, f),
Self::Overflow(err) => fmt::Display::fmt(err, f),
}
}
}

impl From<LenError> for Uleb128Error {
fn from(error: LenError) -> Self {
Self::Len(error)
Expand Down
13 changes: 12 additions & 1 deletion src/error/utf8.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
use core::str;
use core::{fmt, str};

/// Invalid UTF-8
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[non_exhaustive]
pub struct Utf8Error(str::Utf8Error);

/// __*`unstable-error`*__: feature required
#[cfg(feature = "unstable-error")]
impl core::error::Error for Utf8Error {}

impl fmt::Display for Utf8Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("invalid utf8: ")?;
fmt::Display::fmt(&self.0, f)
}
}

impl Utf8Error {
/// Return the offset from the given reader's cursor up to which valid UTF-8
/// was verified.
Expand Down
2 changes: 1 addition & 1 deletion src/io/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! I/O primitives
//! __*`unstable-io`*__ feature required; I/O primitives (MSRV 1.84)

mod destination;
mod receiver;
Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@
//! bound reading and writing, there are the [`io::Source`] and
//! [`io::Destination`] traits which work by buffering the bytes to be sent on
//! an [`io::Sender`] or received on an [`io::Receiver`].
//!
//! # Features
//!
//! Some non-default features can enable unstable (no API stability guarantees)
//! functionality.
//!
//! - __*`unstable-io`*__: Bumps MSRV to 1.84, enables the [`io`] module
//! - __*`unstable-error`*__: Bumps MSRV to 1.81, implements [`Error`] for
//! error types
//!
//! [`Error`]: core::error::Error

#![doc(
html_logo_url = "https://ardaku.github.io/mm/logo.svg",
Expand Down Expand Up @@ -45,6 +56,7 @@ pub mod be;
pub mod class;
mod empty;
pub mod error;
#[cfg(feature = "unstable-io")]
pub mod io;
pub mod le;
mod purge;
Expand Down

0 comments on commit f42ccc1

Please sign in to comment.