Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ name = "bench"
harness = false

[features]
# depcrecated, will be removed in a future release
array_chunks = []
default = ["std"]
std = []
unsafe = []
13 changes: 8 additions & 5 deletions src/decode.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
use crate::alphabet::{self, SIZE, SIZE_SIZE};
use std::error::Error;
use std::fmt::{Display, Formatter};

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use core::fmt::{Display, Formatter};
/// The error type returned when the input is not a valid base45 string
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
pub struct DecodeError;

impl Display for DecodeError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.write_str("Invalid base45 string")
}
}

impl Error for DecodeError {}
impl core::error::Error for DecodeError {}

/// Decode a string from base45
///
/// Takes a base45 encoded string and returns a UTF-8 string on success
///
/// ```rust,no_run
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # fn main() -> Result<(), Box<dyn core::error::Error>> {
/// let decoded = String::from_utf8(base45::decode("%69 VD92EX0")?)?;
/// assert_eq!(decoded, "Hello!!");
/// # Ok(())
Expand Down
4 changes: 3 additions & 1 deletion src/encode.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::alphabet::{self, SIZE, SIZE_SIZE};

#[inline(always)]
#[cfg(not(feature = "std"))]
use alloc::{string::String, vec::Vec};

fn divmod<const N: u32>(x: u32) -> (u32, u32) {
(x / N, x % N)
}
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(not(feature = "std"), no_std)]
//! Encoder/decoder for base45 that is fully compatible with
//! [`draft-faltstrom-base45-07.txt`](https://www.ietf.org/archive/id/draft-faltstrom-base45-07.txt)
//!
Expand All @@ -14,6 +15,9 @@
//! [`core::slice::ChunksExact`](https://doc.rust-lang.org/core/slice/struct.ChunksExact.html).
//! Ideally, there is no performance penalty using this means.

#[cfg(not(feature = "std"))]
extern crate alloc;

pub mod alphabet;
mod decode;
mod encode;
Expand Down
2 changes: 2 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::*;
#[cfg(not(feature = "std"))]
use alloc::{string::String, vec};

#[test]
fn encode_ab() {
Expand Down
Loading