alpm_types/env.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
use std::{
fmt::{Display, Formatter},
str::FromStr,
string::ToString,
};
use crate::error::Error;
use crate::Architecture;
use crate::Name;
use crate::Version;
/// An option string
///
/// The option string is identified by its name and whether it is on (not prefixed with "!") or off
/// (prefixed with "!").
///
/// This type is used in the context of `makepkg` options, build environment options ([`BuildEnv`]),
/// and package options ([`PackageOption`]).
///
/// ## Examples
/// ```
/// use alpm_types::MakePkgOption;
///
/// let option = MakePkgOption::new("foo").unwrap();
/// assert_eq!(option.on(), true);
/// assert_eq!(option.name(), "foo");
///
/// let not_option = MakePkgOption::new("!foo").unwrap();
/// assert_eq!(not_option.on(), false);
/// assert_eq!(not_option.name(), "foo");
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct MakePkgOption {
name: String,
on: bool,
}
impl MakePkgOption {
/// Create a new MakePkgOption in a Result
pub fn new(option: &str) -> Result<Self, Error> {
Self::from_str(option)
}
/// Get the name of the MakePkgOption
pub fn name(&self) -> &str {
&self.name
}
/// Get whether the MakePkgOption is on
pub fn on(&self) -> bool {
self.on
}
}
impl FromStr for MakePkgOption {
type Err = Error;
/// Create an Option from a string
fn from_str(s: &str) -> Result<MakePkgOption, Self::Err> {
let (name, on) = if let Some(name) = s.strip_prefix('!') {
(name.to_owned(), false)
} else {
(s.to_owned(), true)
};
if let Some(c) = name
.chars()
.find(|c| !(c.is_alphanumeric() || ['-', '.', '_'].contains(c)))
{
return Err(Error::ValueContainsInvalidChars { invalid_char: c });
}
Ok(MakePkgOption { name, on })
}
}
impl Display for MakePkgOption {
fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result {
write!(fmt, "{}{}", if self.on { "" } else { "!" }, self.name)
}
}
/// An option string used in a build environment
///
/// The option string is identified by its name and whether it is on (not prefixed with "!") or off
/// (prefixed with "!"). This type is an alias for [`MakePkgOption`].
///
/// ## Examples
/// ```
/// use alpm_types::BuildEnv;
///
/// let option = BuildEnv::new("foo").unwrap();
/// assert_eq!(option.on(), true);
/// assert_eq!(option.name(), "foo");
///
/// let not_option = BuildEnv::new("!foo").unwrap();
/// assert_eq!(not_option.on(), false);
/// assert_eq!(not_option.name(), "foo");
/// ```
pub type BuildEnv = MakePkgOption;
/// An option string used in packaging
///
/// The option string is identified by its name and whether it is on (not prefixed with "!") or off
/// (prefixed with "!"). This type is an alias for [`MakePkgOption`].
///
/// ## Examples
/// ```
/// use alpm_types::PackageOption;
///
/// let option = PackageOption::new("foo").unwrap();
/// assert_eq!(option.on(), true);
/// assert_eq!(option.name(), "foo");
///
/// let not_option = PackageOption::new("!foo").unwrap();
/// assert_eq!(not_option.on(), false);
/// assert_eq!(not_option.name(), "foo");
/// ```
pub type PackageOption = MakePkgOption;
/// Information on an installed package in an environment
///
/// Tracks a `Name`, `Version` (which is guaranteed to have a `Pkgrel`) and `Architecture` of a
/// package in an environment.
///
/// ## Examples
/// ```
/// use std::str::FromStr;
///
/// use alpm_types::InstalledPackage;
///
/// assert!(InstalledPackage::from_str("foo-bar-1:1.0.0-1-any").is_ok());
/// assert!(InstalledPackage::from_str("foo-bar-1:1.0.0-1").is_err());
/// assert!(InstalledPackage::from_str("foo-bar-1:1.0.0-any").is_err());
/// assert!(InstalledPackage::from_str("1:1.0.0-1-any").is_err());
/// ```
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct InstalledPackage {
name: Name,
version: Version,
architecture: Architecture,
}
impl InstalledPackage {
/// Create a new InstalledPackage
pub fn new(name: Name, version: Version, architecture: Architecture) -> Result<Self, Error> {
Ok(InstalledPackage {
name,
version,
architecture,
})
}
}
impl FromStr for InstalledPackage {
type Err = Error;
/// Create an Installed from a string
fn from_str(s: &str) -> Result<InstalledPackage, Self::Err> {
const DELIMITER: char = '-';
let mut parts = s.rsplitn(4, DELIMITER);
let architecture = parts.next().ok_or(Error::MissingComponent {
component: "architecture",
})?;
let architecture = architecture.parse()?;
let version = {
let Some(pkgrel) = parts.next() else {
return Err(Error::MissingComponent {
component: "pkgrel",
})?;
};
let Some(epoch_pkgver) = parts.next() else {
return Err(Error::MissingComponent {
component: "epoch_pkgver",
})?;
};
epoch_pkgver.to_string() + "-" + pkgrel
};
let name = parts
.next()
.ok_or(Error::MissingComponent { component: "name" })?
.to_string();
Ok(InstalledPackage {
name: Name::new(name)?,
version: Version::with_pkgrel(version.as_str())?,
architecture,
})
}
}
impl Display for InstalledPackage {
fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result {
write!(fmt, "{}-{}-{}", self.name, self.version, self.architecture)
}
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use super::*;
#[rstest]
#[case("something", Ok(MakePkgOption{name: "something".to_string(), on: true}))]
#[case("1cool.build-option", Ok(MakePkgOption{name: "1cool.build-option".to_string(), on: true}))]
#[case("üñıçøĐë", Ok(MakePkgOption{name: "üñıçøĐë".to_string(), on: true}))]
#[case("!üñıçøĐë", Ok(MakePkgOption{name: "üñıçøĐë".to_string(), on: false}))]
#[case("!something", Ok(MakePkgOption{name: "something".to_string(), on: false}))]
#[case("!!something", Err(Error::ValueContainsInvalidChars { invalid_char: '!'}))]
#[case("foo\\", Err(Error::ValueContainsInvalidChars { invalid_char: '\\'}))]
fn makepkgoption(#[case] s: &str, #[case] result: Result<MakePkgOption, Error>) {
assert_eq!(MakePkgOption::from_str(s), result);
}
#[rstest]
#[case(
"foo-bar-1:1.0.0-1-any",
Ok(InstalledPackage{
name: Name::new("foo-bar".to_string()).unwrap(),
version: Version::from_str("1:1.0.0-1").unwrap(),
architecture: Architecture::Any,
}),
)]
#[case("foo-bar-1:1.0.0-1", Err(strum::ParseError::VariantNotFound.into()))]
#[case("foo-bar-1:1.0.0-any", Err(Error::InvalidInteger{ kind: std::num::IntErrorKind::InvalidDigit}))]
#[case("1:1.0.0-1-any", Err(Error::MissingComponent { component: "name" }))]
fn installed_new(#[case] s: &str, #[case] result: Result<InstalledPackage, Error>) {
assert_eq!(InstalledPackage::from_str(s), result);
}
}