use getset::{CopyGetters, WithSetters};
use crate::{
generate_struct_arr,
os_cmd::{
MiniStr, fmt_compact,
presets::{StrVec, cargo_build::ArgConverter},
},
};
#[derive(Debug, Clone, WithSetters, CopyGetters)]
#[getset(set_with = "pub", get_copy = "pub with_prefix")]
pub struct BuildStd {
build_default: bool,
std: bool,
core: bool,
alloc: bool,
panic_abort: bool,
panic_unwind: bool,
test: bool,
proc_macro: bool,
}
impl Default for BuildStd {
fn default() -> Self {
Self {
build_default: false,
std: false,
core: false,
alloc: false,
panic_abort: false,
panic_unwind: false,
test: false,
proc_macro: false,
}
}
}
impl ArgConverter for BuildStd {
type ArgsIter = core::iter::Flatten<core::option::IntoIter<[MiniStr; 2]>>;
fn to_args(&self) -> Self::ArgsIter {
let components = generate_struct_arr! [ self =>
std,
core,
alloc,
panic_abort,
panic_unwind,
test,
proc_macro
];
match components
.into_iter()
.filter_map(|(name, enabled)| enabled.then_some(name))
.collect::<StrVec<8>>()
{
v if !v.is_empty() => [
"-Z".into(),
fmt_compact!("build-std={}", v.join(",")), ]
.into()
,
_ => self
.build_default
.then(|| ["-Z", "build-std"].map(Into::into))
}
.into_iter()
.flatten()
}
}
#[cfg(test)]
mod tests {
use tap::Pipe;
use super::*;
#[ignore]
#[test]
fn show_default_build_std() {
BuildStd::default().pipe(|x| dbg!(x));
}
#[ignore]
#[test]
fn test_build_std_to_args() {
let build_std_with_core_and_alloc = BuildStd::default()
.with_std(false)
.with_core(true)
.with_alloc(true)
.with_build_default(true);
assert_eq!(
build_std_with_core_and_alloc
.to_args()
.next(),
Some("-Z".into())
);
assert_eq!(
build_std_with_core_and_alloc
.to_args()
.last(),
Some("build-std=core,alloc".into())
);
let build_std_with_default_only = BuildStd::default().with_build_default(true);
assert_eq!(
build_std_with_default_only
.to_args()
.last(),
Some("build-std".into())
);
let build_std_none_enabled = BuildStd::default();
assert_eq!(
build_std_none_enabled
.to_args()
.next(),
None
);
}
}