Skip to content

Commit

Permalink
Fix compile errors with no-std.
Browse files Browse the repository at this point in the history
  • Loading branch information
frozenlib committed Nov 17, 2024
1 parent 143020b commit b8200be
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 60 deletions.
5 changes: 5 additions & 0 deletions parse-display-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ rust-version = "1.81.0"
[lib]
proc-macro = true

[features]
default = []
std = []


[dependencies]
syn = { version = "2.0.72", features = ["visit"] }
quote = "1.0.36"
Expand Down
83 changes: 43 additions & 40 deletions parse-display-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,19 @@ fn derive_from_str_for_struct(input: &DeriveInput, data: &DataStruct) -> Result<
},
));

let body = p.build_from_str_regex_body(crate_path)?;
ts.extend(impl_trait(
input,
&parse_quote!(#crate_path::FromStrRegex),
&wheres,
quote! {
fn from_str_regex() -> String {
#body
}
},
));
if cfg!(feature = "std") {
let body = p.build_from_str_regex_body(crate_path)?;
ts.extend(impl_trait(
input,
&parse_quote!(#crate_path::FromStrRegex),
&wheres,
quote! {
fn from_str_regex() -> String {
#body
}
},
));
}
dump_if(hattrs.dump_from_str, &ts);
Ok(ts)
}
Expand Down Expand Up @@ -266,36 +268,37 @@ fn derive_from_str_for_enum(input: &DeriveInput, data: &DataEnum) -> Result<Toke
}
},
));
let body = if regex_args.is_empty() {
let fmts = regex_fmts
.into_iter()
.map(|s| escape(&s.unwrap()))
.collect::<Vec<_>>();
let s = fmts.join("|");
quote! { #s.into() }
} else {
let fmts = regex_fmts
.into_iter()
.map(|s| match s {
Some(s) => format!("({})", escape_fmt(&escape(&s))),
None => "{}".to_string(),
})
.collect::<Vec<_>>();
let fmt = fmts.join("|");
quote! { format!(#fmt, #(#regex_args,)*) }
};

ts.extend(impl_trait(
input,
&parse_quote!(#crate_path::FromStrRegex),
&wheres,
quote! {
fn from_str_regex() -> String {
#body
}
},
));
if cfg!(feature = "std") {
let body = if regex_args.is_empty() {
let fmts = regex_fmts
.into_iter()
.map(|s| escape(&s.unwrap()))
.collect::<Vec<_>>();
let s = fmts.join("|");
quote! { #s.into() }
} else {
let fmts = regex_fmts
.into_iter()
.map(|s| match s {
Some(s) => format!("({})", escape_fmt(&escape(&s))),
None => "{}".to_string(),
})
.collect::<Vec<_>>();
let fmt = fmts.join("|");
quote! { format!(#fmt, #(#regex_args,)*) }
};

ts.extend(impl_trait(
input,
&parse_quote!(#crate_path::FromStrRegex),
&wheres,
quote! {
fn from_str_regex() -> String {
#body
}
},
));
}
dump_if(hattrs_enum.dump_from_str, &ts);
Ok(ts)
}
Expand Down
2 changes: 1 addition & 1 deletion parse-display/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ rust-version = "1.81.0"

[features]
default = ["std"]
std = ["regex", "regex-syntax"]
std = ["regex", "regex-syntax", "parse-display-derive/std"]
docs = []

[dependencies]
Expand Down
18 changes: 1 addition & 17 deletions parse-display/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::fmt;
#[cfg(feature = "std")]
pub use super::helpers_std::*;

use crate::{DisplayFormat, FromStrFormat, FromStrRegex};
use crate::{DisplayFormat, FromStrFormat};

pub struct Formatted<'a, T: ?Sized, F: DisplayFormat<T>> {
pub value: &'a T,
Expand Down Expand Up @@ -34,19 +34,3 @@ impl<'a, T: ?Sized + fmt::Pointer> fmt::Pointer for FmtPointer<'a, T> {
pub fn fmt_pointer<T: ?Sized + fmt::Pointer>(value: &T) -> impl fmt::Pointer + '_ {
FmtPointer(value)
}

pub struct RegexInfer;
impl<T: fmt::Display> DisplayFormat<T> for RegexInfer {
fn write(&self, f: &mut fmt::Formatter, value: &T) -> fmt::Result {
T::fmt(value, f)
}
}
impl<T: FromStrRegex> FromStrFormat<T> for RegexInfer {
type Err = T::Err;
fn parse(&self, s: &str) -> core::result::Result<T, Self::Err> {
s.parse()
}
fn regex(&self) -> Option<String> {
Some(T::from_str_regex())
}
}
20 changes: 18 additions & 2 deletions parse-display/src/helpers_std.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use core::mem;
use std::borrow::Cow;
use std::collections::HashMap;
use std::{borrow::Cow, fmt};

use regex::Regex;
use regex_syntax::ast::{Ast, Flags, GroupKind};

use crate::FromStrFormat;
use crate::{DisplayFormat, FromStrFormat, FromStrRegex};

pub use regex;

Expand Down Expand Up @@ -164,3 +164,19 @@ fn replace_ast(
}

type ReplaceAstResult<T = ()> = Result<T, String>;

pub struct RegexInfer;
impl<T: fmt::Display> DisplayFormat<T> for RegexInfer {
fn write(&self, f: &mut fmt::Formatter, value: &T) -> fmt::Result {
T::fmt(value, f)
}
}
impl<T: FromStrRegex> FromStrFormat<T> for RegexInfer {
type Err = T::Err;
fn parse(&self, s: &str) -> core::result::Result<T, Self::Err> {
s.parse()
}
fn regex(&self) -> Option<String> {
Some(T::from_str_regex())
}
}
2 changes: 2 additions & 0 deletions parse-display/tests/from_str_regex.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "std")]

use core::f32;
use std::fmt::Display;

Expand Down

0 comments on commit b8200be

Please sign in to comment.