rustible/src/modules/package/list.rs

80 lines
1.9 KiB
Rust
Raw Normal View History

2023-04-19 21:49:30 +02:00
use serde::Serialize;
#[derive(Clone, Debug, Serialize)]
pub struct PackageList {
pub list: Vec<String>,
2023-04-19 21:49:30 +02:00
}
impl PackageList {
pub fn list(&self) -> & [ String ] {
&self.list
}
2023-04-19 21:49:30 +02:00
pub fn add<T: IntoPackageList>(self, add: T) -> PackageList {
let Self { mut list } = self;
2023-04-19 21:49:30 +02:00
let extend_with = add.into_package_list();
list.extend(extend_with.list);
2023-04-19 21:49:30 +02:00
PackageList {
list
}
2023-04-19 21:49:30 +02:00
}
}
/// Turn a stringy value or list of values into an actual package list.
/// Only implemented for &str and &str/String slices because apparently
/// there may be a future implementation that turns a slice of stringy values
/// into a stringy value, resulting in conflicting implementations.
/// https://stackoverflow.com/questions/63136970/how-do-i-work-around-the-upstream-crates-may-add-a-new-impl-of-trait-error
pub trait IntoPackageList {
fn into_package_list(self) -> PackageList;
}
2023-04-19 21:49:30 +02:00
impl IntoPackageList for &str {
fn into_package_list(self) -> PackageList {
PackageList {
list: vec!(self.to_string()),
2023-04-19 21:49:30 +02:00
}
}
}
impl IntoPackageList for String {
fn into_package_list(self) -> PackageList {
PackageList {
list: vec!(self),
2023-04-19 21:49:30 +02:00
}
}
}
2023-04-19 21:49:30 +02:00
impl<const C: usize> IntoPackageList for & [ &str; C ] {
fn into_package_list(self) -> PackageList {
PackageList {
list: self.iter().map(|x| x.to_string()).collect(),
2023-04-19 21:49:30 +02:00
}
}
}
impl<const C: usize> IntoPackageList for & [ String; C ] {
fn into_package_list(self) -> PackageList {
PackageList {
list: self.to_vec(),
}
}
2023-04-19 21:49:30 +02:00
}
impl IntoPackageList for Vec<&str> {
fn into_package_list(self) -> PackageList {
PackageList {
list: self.iter().map(|x| x.to_string()).collect(),
}
2023-04-19 21:49:30 +02:00
}
}
impl IntoPackageList for Vec<String> {
fn into_package_list(self) -> PackageList {
PackageList {
list: self,
}
}
2023-04-19 21:49:30 +02:00
}