Skip to content

Commit 780a8a4

Browse files
authored
Merge pull request #8 from tailwags/no_std
Add no_std support
2 parents 210d9a1 + 0946e1c commit 780a8a4

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.
@@ -239,6 +245,7 @@ enum State {
239245
End,
240246
}
241247

248+
#[cfg(feature = "std")]
242249
impl Parser<env::Args> {
243250
/// Creates a `Parser` using the program's command-line arguments from [`std::env::args`].
244251
///
@@ -584,7 +591,7 @@ pub enum ParsingError {
584591
}
585592

586593
impl Display for ParsingError {
587-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
594+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
588595
match self {
589596
Self::InvalidOption { reason, offender } => {
590597
write!(f, "reason: {reason}")?;
@@ -621,6 +628,7 @@ impl Display for ParsingError {
621628

622629
impl Error for ParsingError {}
623630

631+
#[cfg(feature = "std")]
624632
#[cfg(test)]
625633
mod tests {
626634
use crate::{Argument::*, Parser, Result};

0 commit comments

Comments
 (0)