Skip to content

Commit 0946e1c

Browse files
committed
Add no_std support
1 parent 9a595c6 commit 0946e1c

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ readme = "README.md"
1111
documentation = "https://docs.rs/sap"
1212
include = ["src/**/*.rs", "README.md", "LICENSE"]
1313

14+
[features]
15+
default = ["std"]
16+
std = []
17+
1418
[profile.release]
1519
lto = true
1620
codegen-units = 1

src/lib.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![warn(clippy::pedantic)]
22
#![warn(clippy::complexity)]
3+
#![cfg_attr(not(feature = "std"), no_std)]
34
//! # Sap - a Small Argument Parser
45
//!
56
//! A minimal, zero-dependency Unix command-line argument parser for Rust.
@@ -38,13 +39,18 @@
3839
//! }
3940
//! ```
4041
41-
use std::{
42-
borrow::Cow,
43-
env,
44-
error::Error,
45-
fmt::{Debug, Display},
46-
hint::unreachable_unchecked,
47-
mem,
42+
#[cfg(not(feature = "std"))]
43+
extern crate alloc;
44+
45+
use core::{error::Error, fmt::Display, hint::unreachable_unchecked, mem};
46+
47+
#[cfg(feature = "std")]
48+
use std::{borrow::Cow, env, fmt::Debug};
49+
50+
#[cfg(not(feature = "std"))]
51+
use alloc::{
52+
borrow::{Cow, ToOwned},
53+
string::{String, ToString},
4854
};
4955

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

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

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

609616
impl Error for ParsingError {}
610617

618+
#[cfg(feature = "std")]
611619
#[cfg(test)]
612620
mod tests {
613621
use crate::{Argument::*, Parser, Result};

0 commit comments

Comments
 (0)