use crate::Parser;
macro_rules! define_parse_methods {
(
$((
$(#[$attr:meta])*
fn $fn_name:ident,
fn $fn_name_bytes:ident,
$parsing:ty,
$err:ident
$(,)?
))*
) => (
$(
define_parse_methods_inner!{
concat!(
"Parses a `", stringify!($parsing), "` from a `&str`.\n\n",
"This returns an `Err` if the string would not successfully `.parse()` into a `",
stringify!($parsing),
"`.\n\n",
"To parse a `",
stringify!($parsing),
"` from only part of a string, you can use [`Parser::parse_",
stringify!($parsing),
"`](../parsing/struct.Parser.html#method.parse_",
stringify!($parsing),
")",
".\n\n",
),
concat!(
"Like [`", stringify!($fn_name), "`](./fn.", stringify!($fn_name),".html)",
"but takes a `&[u8]` argument."
),
$(#[$attr])*,
$fn_name,
$fn_name_bytes,
$parsing,
$err,
}
)*
);
}
macro_rules! define_parse_methods_inner{
(
$s_docs:expr,
$b_docs:expr,
$(#[$attr:meta])*,
$fn_name:ident,
$fn_name_bytes:ident,
$parsing:ty,
$err:ident,
) => {
#[doc = $s_docs]
$(#[$attr])*
#[inline]
#[cfg(feature = "parsing_no_proc")]
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "parsing_no_proc")))]
pub const fn $fn_name(s: &str) -> Result<$parsing, $err> {
$fn_name_bytes(s.as_bytes())
}
#[doc = $b_docs]
#[cfg(feature = "parsing_no_proc")]
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "parsing_no_proc")))]
pub const fn $fn_name_bytes(bytes: &[u8]) -> Result<$parsing, $err> {
match Parser::from_bytes(bytes).$fn_name() {
Ok((num, parser)) if parser.is_empty() => Ok(num),
_ => Err($err {
_priv: (),
}),
}
}
}
}
define_parse_methods! {
(
fn parse_u128, fn parse_u128_b, u128, ParseIntError
)
(
fn parse_i128, fn parse_i128_b, i128, ParseIntError
)
(
fn parse_u64, fn parse_u64_b, u64, ParseIntError
)
(
fn parse_i64, fn parse_i64_b, i64, ParseIntError
)
(
fn parse_u32, fn parse_u32_b, u32, ParseIntError
)
(
fn parse_i32, fn parse_i32_b, i32, ParseIntError
)
(
fn parse_u16, fn parse_u16_b, u16, ParseIntError
)
(
fn parse_i16, fn parse_i16_b, i16, ParseIntError
)
(
fn parse_u8, fn parse_u8_b, u8, ParseIntError
)
(
fn parse_i8, fn parse_i8_b, i8, ParseIntError
)
(
fn parse_usize, fn parse_usize_b, usize, ParseIntError
)
(
fn parse_isize, fn parse_isize_b, isize, ParseIntError
)
(
fn parse_bool, fn parse_bool_b, bool, ParseBoolError
)
}
#[cfg(feature = "parsing_no_proc")]
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "parsing_no_proc")))]
pub type ParseIntResult<T> = Result<T, ParseIntError>;
#[cfg(feature = "parsing_no_proc")]
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "parsing_no_proc")))]
pub type ParseBoolResult = Result<bool, ParseBoolError>;
use core::fmt::{self, Display};
#[cfg(feature = "parsing_no_proc")]
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "parsing_no_proc")))]
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub struct ParseIntError {
_priv: (),
}
impl Display for ParseIntError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("could not parse an integer")
}
}
impl ParseIntError {
pub const fn panic(&self) -> ! {
let x = self.something();
[][x]
}
const fn something(&self) -> usize {
0
}
}
#[cfg(feature = "parsing_no_proc")]
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "parsing_no_proc")))]
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub struct ParseBoolError {
_priv: (),
}
impl Display for ParseBoolError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("could not parse a bool")
}
}
impl ParseBoolError {
pub const fn panic(&self) -> ! {
let x = self.something();
[][x]
}
const fn something(&self) -> usize {
0
}
}