|
1 | 1 | #![warn(clippy::pedantic)] |
2 | 2 | #![warn(clippy::complexity)] |
| 3 | +#![cfg_attr(not(feature = "std"), no_std)] |
3 | 4 | //! # Sap - a Small Argument Parser |
4 | 5 | //! |
5 | 6 | //! A minimal, zero-dependency Unix command-line argument parser for Rust. |
|
38 | 39 | //! } |
39 | 40 | //! ``` |
40 | 41 |
|
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}, |
48 | 54 | }; |
49 | 55 |
|
50 | 56 | /// A [`Result`] type alias using [`ParsingError`] as the default error type. |
@@ -239,6 +245,7 @@ enum State { |
239 | 245 | End, |
240 | 246 | } |
241 | 247 |
|
| 248 | +#[cfg(feature = "std")] |
242 | 249 | impl Parser<env::Args> { |
243 | 250 | /// Creates a `Parser` using the program's command-line arguments from [`std::env::args`]. |
244 | 251 | /// |
@@ -584,7 +591,7 @@ pub enum ParsingError { |
584 | 591 | } |
585 | 592 |
|
586 | 593 | 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 { |
588 | 595 | match self { |
589 | 596 | Self::InvalidOption { reason, offender } => { |
590 | 597 | write!(f, "reason: {reason}")?; |
@@ -621,6 +628,7 @@ impl Display for ParsingError { |
621 | 628 |
|
622 | 629 | impl Error for ParsingError {} |
623 | 630 |
|
| 631 | +#[cfg(feature = "std")] |
624 | 632 | #[cfg(test)] |
625 | 633 | mod tests { |
626 | 634 | use crate::{Argument::*, Parser, Result}; |
|
0 commit comments