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: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ readme = "README.md"
documentation = "https://docs.rs/sap"
include = ["src/**/*.rs", "README.md", "LICENSE"]

[features]
default = ["std"]
std = []

[profile.release]
lto = true
codegen-units = 1
Expand Down
24 changes: 16 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![warn(clippy::pedantic)]
#![warn(clippy::complexity)]
#![cfg_attr(not(feature = "std"), no_std)]
//! # Sap - a Small Argument Parser
//!
//! A minimal, zero-dependency Unix command-line argument parser for Rust.
Expand Down Expand Up @@ -38,13 +39,18 @@
//! }
//! ```

use std::{
borrow::Cow,
env,
error::Error,
fmt::{Debug, Display},
hint::unreachable_unchecked,
mem,
#[cfg(not(feature = "std"))]
extern crate alloc;

use core::{error::Error, fmt::Display, hint::unreachable_unchecked, mem};

#[cfg(feature = "std")]
use std::{borrow::Cow, env, fmt::Debug};

#[cfg(not(feature = "std"))]
use alloc::{
borrow::{Cow, ToOwned},
string::{String, ToString},
};

/// A [`Result`] type alias using [`ParsingError`] as the default error type.
Expand Down Expand Up @@ -226,6 +232,7 @@ enum State {
End,
}

#[cfg(feature = "std")]
impl Parser<env::Args> {
/// Creates a `Parser` using the program's command-line arguments from [`std::env::args`].
///
Expand Down Expand Up @@ -571,7 +578,7 @@ pub enum ParsingError {
}

impl Display for ParsingError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::InvalidOption { reason, offender } => {
write!(f, "reason: {reason}")?;
Expand Down Expand Up @@ -608,6 +615,7 @@ impl Display for ParsingError {

impl Error for ParsingError {}

#[cfg(feature = "std")]
#[cfg(test)]
mod tests {
use crate::{Argument::*, Parser, Result};
Expand Down