Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP/oliver/no_std #39

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ language: rust
rust:
- beta
- stable
- 1.42.0
- 1.60.0
before_script:
- |
pip install 'travis-cargo<0.2' --user &&
Expand All @@ -12,6 +12,8 @@ script:
- |
travis-cargo build &&
travis-cargo test &&
travis-cargo check -- --no-default-features &&
travis-cargo test -- --no-default-features &&
travis-cargo bench &&
travis-cargo --only stable doc
addons:
Expand Down
30 changes: 25 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,32 @@ readme = "README.md"
repository = "https://github.com/jcreekmore/pem-rs.git"
version = "1.1.2-alpha.0"
categories = [ "cryptography" ]
edition = "2018"
rust-version = "1.34.2"
keywords = [
"pem",
"no_std",
]
edition = "2021"
rust-version = "1.60.0" # 1.60.0 required for the "serde?/std" dependency feature

[dependencies]
base64 = "0.13.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.13.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
27 changes: 24 additions & 3 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
// 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;

/// The `pem` error type.
#[derive(Debug, Eq, PartialEq)]
Expand All @@ -16,7 +17,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 +36,7 @@ impl fmt::Display for PemError {
}
}

#[cfg(feature = "std")]
impl Error for PemError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Expand All @@ -47,5 +49,24 @@ impl Error for PemError {
}
}

// FIXME(oliveruv): this could be better. Wanted to just do
// #[cfg(any(feature = "std", test))]
// but then it seems base64::DecodeError doesn't get the Error impl even
// though it's got the same cfg attribute. Guess the test attribute is
// only valid for the current crate?
#[cfg(not(feature = "std"))]
impl PemError {
#[allow(missing_docs)]
pub fn source(&self) -> Option<&(dyn core::fmt::Display + 'static)> {
match self {
// Errors originating from other libraries.
PemError::InvalidData(e) => Some(e),
PemError::NotUtf8(e) => Some(e),
// Errors directly originating from `pem-rs`.
_ => None,
}
}
}

/// The `pem` result type.
pub type Result<T> = ::std::result::Result<T, PemError>;
pub type Result<T> = ::core::result::Result<T, PemError>;
19 changes: 18 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@
//! 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, so `alloc` is still
//! required.
//!
//! The `serde` feature implements `serde::{Deserialize, Serialize}`
//! for this crate's `Pem` struct.

#![recursion_limit = "1024"]
#![deny(
Expand All @@ -106,12 +118,17 @@
unused_qualifications
)]

#[cfg(not(any(feature = "std", test)))]
extern crate alloc;
#[cfg(not(any(feature = "std", test)))]
use alloc::vec::Vec;

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

pub use crate::errors::{PemError, Result};
use std::str;
use core::str;

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