refactor: Swtich to using bon for builder pattern

This commit is contained in:
Gerald Pinder 2024-09-21 13:15:45 -04:00
parent aed7e275f2
commit 0c52cf6a54
36 changed files with 326 additions and 314 deletions

View file

@ -26,7 +26,7 @@ miette.workspace = true
serde.workspace = true
serde_json.workspace = true
serde_yaml.workspace = true
typed-builder.workspace = true
bon.workspace = true
[build-dependencies]
syntect = "5"

View file

@ -3,10 +3,10 @@ use std::{
sync::{LazyLock, Mutex},
};
use bon::Builder;
use clap::Args;
use docker_credential::DockerCredential;
use log::trace;
use typed_builder::TypedBuilder;
use crate::{
constants::{
@ -94,7 +94,7 @@ static ENV_CREDENTIALS: LazyLock<Option<Credentials>> = LazyLock::new(|| {
});
/// The credentials for logging into image registries.
#[derive(Debug, Default, Clone, TypedBuilder)]
#[derive(Debug, Default, Clone, Builder)]
pub struct Credentials {
pub registry: String,
pub username: String,
@ -132,22 +132,20 @@ impl Credentials {
}
}
#[derive(Debug, Default, Clone, TypedBuilder, Args)]
#[derive(Debug, Default, Clone, Builder, Args)]
#[builder(on(String, into))]
pub struct CredentialsArgs {
/// The registry's domain name.
#[arg(long, env = BB_REGISTRY)]
#[builder(default, setter(into, strip_option))]
pub registry: Option<String>,
/// The username to login to the
/// container registry.
#[arg(short = 'U', long, env = BB_USERNAME, hide_env_values = true)]
#[builder(default, setter(into, strip_option))]
pub username: Option<String>,
/// The password to login to the
/// container registry.
#[arg(short = 'P', long, env = BB_PASSWORD, hide_env_values = true)]
#[builder(default, setter(into, strip_option))]
pub password: Option<String>,
}

View file

@ -5,6 +5,7 @@ mod macros;
pub mod syntax_highlighting;
#[cfg(feature = "test")]
pub mod test_utils;
pub mod traits;
use std::{
os::unix::ffi::OsStrExt,

View file

@ -27,23 +27,23 @@ macro_rules! cmd {
(@ $command:ident $(,)?) => { };
(@ $command:ident, for $for_expr:expr $(, $($tail:tt)*)?) => {
{
for arg in $for_expr.iter() {
for arg in $for_expr {
$crate::cmd!($command, arg);
}
$($crate::cmd!(@ $command, $($tail)*);)*
}
};
(@ $command:ident, for $iter:ident in $for_expr:expr => [ $($arg:expr),* $(,)? ] $(, $($tail:tt)*)?) => {
(@ $command:ident, for $iter:pat in $for_expr:expr => [ $($arg:expr),* $(,)? ] $(, $($tail:tt)*)?) => {
{
for $iter in $for_expr.iter() {
for $iter in $for_expr {
$($crate::cmd!(@ $command, $arg);)*
}
$($crate::cmd!(@ $command, $($tail)*);)*
}
};
(@ $command:ident, for $iter:ident in $for_expr:expr => $arg:expr $(, $($tail:tt)*)?) => {
(@ $command:ident, for $iter:pat in $for_expr:expr => $arg:expr $(, $($tail:tt)*)?) => {
{
for $iter in $for_expr.iter() {
for $iter in $for_expr {
$crate::cmd!(@ $command, $arg);
}
$($crate::cmd!(@ $command, $($tail)*);)*

62
utils/src/traits.rs Normal file
View file

@ -0,0 +1,62 @@
use std::{
borrow::Cow,
ffi::{OsStr, OsString},
path::{Path, PathBuf},
};
trait PrivateTrait<T: ToOwned + ?Sized> {}
macro_rules! impl_private_trait {
($lt:lifetime, $type:ty) => {
impl<$lt, T> PrivateTrait<$type> for T where T: AsRef<[&$lt $type]> {}
};
($type:ty) => {
impl<T> PrivateTrait<$type> for T where T: AsRef<[$type]> {}
};
}
impl_private_trait!(String);
impl_private_trait!('a, str);
impl_private_trait!(PathBuf);
impl_private_trait!('a, Path);
impl_private_trait!(OsString);
impl_private_trait!('a, OsStr);
#[allow(private_bounds)]
pub trait CowCollecter<'a, IN, OUT>: PrivateTrait<IN>
where
IN: ToOwned + ?Sized,
OUT: ToOwned + ?Sized,
{
fn to_cow_vec(&'a self) -> Vec<Cow<'a, OUT>>;
}
macro_rules! impl_cow_collector {
($type:ty) => {
impl<'a, T> CowCollecter<'a, $type, $type> for T
where
T: AsRef<[&'a $type]>,
{
fn to_cow_vec(&'a self) -> Vec<Cow<'a, $type>> {
self.as_ref().iter().map(|v| Cow::Borrowed(*v)).collect()
}
}
};
($in:ty, $out:ty) => {
impl<'a, T> CowCollecter<'a, $in, $out> for T
where
T: AsRef<[$in]>,
{
fn to_cow_vec(&'a self) -> Vec<Cow<'a, $out>> {
self.as_ref().iter().map(Cow::from).collect()
}
}
};
}
impl_cow_collector!(String, str);
impl_cow_collector!(str);
impl_cow_collector!(PathBuf, Path);
impl_cow_collector!(Path);
impl_cow_collector!(OsString, OsStr);
impl_cow_collector!(OsStr);