Skip to content

Commit

Permalink
Add no_std crate support
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverUv authored and jcreekmore committed Feb 26, 2023
1 parent a2c12dd commit 66a6bdb
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ jobs:
- run: cargo clippy --all-features -- --deny warnings
- run: cargo test --all-features
- run: cargo bench --all-features
# For no_std we use check instead of test because
# std is required for the test suite to run.
- run: cargo clippy --no-default-features -- --deny warnings
- run: cargo check --no-default-features
27 changes: 24 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,33 @@ readme = "README.md"
repository = "https://github.com/jcreekmore/pem-rs.git"
version = "1.1.2-alpha.0"
categories = [ "cryptography" ]
keywords = [
"no-std",
"no_std",
"pem",
]
edition = "2021"
rust-version = "1.67.0"

[dependencies]
base64 = "0.21.0"
serde = { version = "1", optional = true, features = ["serde_derive"] }
[features]
default = ["std"]
std = [
"base64/std",
# enable serde's std feature iff the serde and std features are both activated
"serde?/std",
]
serde = ["dep:serde"]

[dependencies.base64]
version = "0.21.0"
default-features = false
features = ["alloc"]

[dependencies.serde]
version = "1"
optional = true
default-features = false
features = ["serde_derive"]

[dev-dependencies]
criterion = "0.3.0"
Expand Down
12 changes: 9 additions & 3 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
// Licensed under the MIT license <LICENSE.md or
// http://opensource.org/licenses/MIT>. This file may not be
// copied, modified, or distributed except according to those terms.
use core::fmt;

#[cfg(any(feature = "std", test))]
use std::error::Error;
use std::fmt;

#[cfg(not(any(feature = "std", test)))]
use alloc::string::String;

/// The `pem` error type.
#[derive(Debug, Eq, PartialEq)]
Expand All @@ -16,7 +21,7 @@ pub enum PemError {
MissingEndTag,
MissingData,
InvalidData(::base64::DecodeError),
NotUtf8(::std::str::Utf8Error),
NotUtf8(::core::str::Utf8Error),
}

impl fmt::Display for PemError {
Expand All @@ -35,6 +40,7 @@ impl fmt::Display for PemError {
}
}

#[cfg(any(feature = "std", test))]
impl Error for PemError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Expand All @@ -48,4 +54,4 @@ impl Error for PemError {
}

/// The `pem` result type.
pub type Result<T> = ::std::result::Result<T, PemError>;
pub type Result<T> = ::core::result::Result<T, PemError>;
24 changes: 22 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@
//! assert_eq!(pems[0].tag(), "INTERMEDIATE CERT");
//! assert_eq!(pems[1].tag(), "CERTIFICATE");
//! ```
//!
//! # Features
//!
//! This crate supports two features: `std` and `serde`.
//!
//! The `std` feature is enabled by default. If you specify
//! `default-features = false` to disable `std`, be aware that
//! this crate still needs an allocator.
//!
//! The `serde` feature implements `serde::{Deserialize, Serialize}`
//! for this crate's `Pem` struct.

#![recursion_limit = "1024"]
#![deny(
Expand All @@ -105,15 +116,24 @@
unused_import_braces,
unused_qualifications
)]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(not(any(feature = "std", test)))]
extern crate alloc;
#[cfg(not(any(feature = "std", test)))]
use alloc::{
format,
string::{String, ToString},
vec::Vec,
};

mod errors;
mod parser;
use parser::{parse_captures, parse_captures_iter, Captures};

pub use crate::errors::{PemError, Result};
use base64::Engine as _;
use std::fmt;
use std::str;
use core::{fmt, str};

/// The line length for PEM encoding
const LINE_WRAP: usize = 64;
Expand Down

0 comments on commit 66a6bdb

Please sign in to comment.