|
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. |
@@ -226,6 +232,7 @@ enum State { |
226 | 232 | End, |
227 | 233 | } |
228 | 234 |
|
| 235 | +#[cfg(feature = "std")] |
229 | 236 | impl Parser<env::Args> { |
230 | 237 | /// Creates a `Parser` using the program's command-line arguments from [`std::env::args`]. |
231 | 238 | /// |
@@ -571,7 +578,7 @@ pub enum ParsingError { |
571 | 578 | } |
572 | 579 |
|
573 | 580 | 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 { |
575 | 582 | match self { |
576 | 583 | Self::InvalidOption { reason, offender } => { |
577 | 584 | write!(f, "reason: {reason}")?; |
@@ -608,6 +615,7 @@ impl Display for ParsingError { |
608 | 615 |
|
609 | 616 | impl Error for ParsingError {} |
610 | 617 |
|
| 618 | +#[cfg(feature = "std")] |
611 | 619 | #[cfg(test)] |
612 | 620 | mod tests { |
613 | 621 | use crate::{Argument::*, Parser, Result}; |
|
0 commit comments