alpm_types/relation.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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
use std::{
fmt::{Display, Formatter},
str::FromStr,
};
use strum::IntoEnumIterator;
use crate::{Error, Name, VersionComparison, VersionRequirement};
/// A package relation
///
/// Describes a relation to a component.
/// Package relations may either consist of only a [`Name`] *or* of a [`Name`] and a
/// [`VersionRequirement`].
///
/// ## Note
///
/// A [`PackageRelation`] covers all **alpm-package-relations** *except* optional
/// dependencies, as those behave differently.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PackageRelation {
pub name: Name,
pub version_requirement: Option<VersionRequirement>,
}
impl PackageRelation {
/// Creates a new [`PackageRelation`]
///
/// # Examples
///
/// ```
/// use alpm_types::{PackageRelation, VersionComparison, VersionRequirement};
///
/// # fn main() -> Result<(), alpm_types::Error> {
/// PackageRelation::new(
/// "example".parse()?,
/// Some(VersionRequirement {
/// comparison: VersionComparison::Less,
/// version: "1.0.0".parse()?,
/// }),
/// );
///
/// PackageRelation::new("example".parse()?, None);
/// # Ok(())
/// # }
/// ```
pub fn new(name: Name, version_requirement: Option<VersionRequirement>) -> Self {
Self {
name,
version_requirement,
}
}
}
impl Display for PackageRelation {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if let Some(version_requirement) = self.version_requirement.as_ref() {
write!(f, "{}{}", self.name, version_requirement)
} else {
write!(f, "{}", self.name)
}
}
}
impl FromStr for PackageRelation {
type Err = Error;
/// Parses a [`PackageRelation`] from a string slice.
///
/// # Errors
///
/// Returns an error if a [`PackageRelation`] can not be parsed from input.
///
/// # Examples
///
/// ```
/// use std::str::FromStr;
///
/// use alpm_types::{PackageRelation, VersionComparison, VersionRequirement};
///
/// # fn main() -> Result<(), alpm_types::Error> {
/// assert_eq!(
/// PackageRelation::from_str("example<1.0.0")?,
/// PackageRelation::new(
/// "example".parse()?,
/// Some(VersionRequirement {
/// comparison: VersionComparison::Less,
/// version: "1.0.0".parse()?
/// })
/// ),
/// );
///
/// assert_eq!(
/// PackageRelation::from_str("example<=1.0.0")?,
/// PackageRelation::new(
/// "example".parse()?,
/// Some(VersionRequirement {
/// comparison: VersionComparison::LessOrEqual,
/// version: "1.0.0".parse()?
/// })
/// ),
/// );
///
/// assert_eq!(
/// PackageRelation::from_str("example=1.0.0")?,
/// PackageRelation::new(
/// "example".parse()?,
/// Some(VersionRequirement {
/// comparison: VersionComparison::Equal,
/// version: "1.0.0".parse()?
/// })
/// ),
/// );
///
/// assert_eq!(
/// PackageRelation::from_str("example>1.0.0")?,
/// PackageRelation::new(
/// "example".parse()?,
/// Some(VersionRequirement {
/// comparison: VersionComparison::Greater,
/// version: "1.0.0".parse()?
/// })
/// ),
/// );
///
/// assert_eq!(
/// PackageRelation::from_str("example>=1.0.0")?,
/// PackageRelation::new(
/// "example".parse()?,
/// Some(VersionRequirement {
/// comparison: VersionComparison::GreaterOrEqual,
/// version: "1.0.0".parse()?
/// })
/// ),
/// );
///
/// assert_eq!(
/// PackageRelation::from_str("example")?,
/// PackageRelation::new("example".parse()?, None),
/// );
///
/// assert!(PackageRelation::from_str("example<").is_err());
/// # Ok(())
/// # }
/// ```
fn from_str(s: &str) -> Result<Self, Self::Err> {
// NOTE: The string splitting relies on the specific ordering of the VersionComparison
// variants (which orders two-letter comparators over one-letter ones)!
for comparison in VersionComparison::iter() {
if let Some((name, version)) = s.split_once(comparison.as_ref()) {
return Ok(Self {
name: Name::new(name.to_string())?,
version_requirement: Some(VersionRequirement {
comparison,
version: version.parse()?,
}),
});
}
}
Ok(Self {
name: Name::new(s.to_string())?,
version_requirement: None,
})
}
}
/// An optional dependency for a package.
///
/// This type is used for representing dependencies that are not essential for base functionality
/// of a package, but may be necessary to make use of certain features of a package.
///
/// An [`OptDepend`] consists of a name and an optional description separated by a colon (`:`).
///
/// - The name component must be a valid [`Name`].
/// - If a description is provided it must be at least one character long.
///
/// ## Note
///
/// It's currently not possible to specify a version in an optional dependency due to
/// the limitations of the current file format.
///
/// ## Examples
///
/// ```
/// use std::str::FromStr;
///
/// use alpm_types::{Name, OptDepend};
///
/// # fn main() -> Result<(), alpm_types::Error> {
/// // Create OptDepend from &str
/// let opt_depend = OptDepend::from_str("example: this is an example dependency")?;
///
/// // Get the name
/// assert_eq!("example", opt_depend.name().as_ref());
///
/// // Get the description
/// assert_eq!(
/// Some("this is an example dependency"),
/// opt_depend.description().as_deref()
/// );
///
/// // Format as String
/// assert_eq!(
/// "example: this is an example dependency",
/// format!("{opt_depend}")
/// );
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct OptDepend {
name: Name,
description: Option<String>,
}
impl OptDepend {
/// Create a new OptDepend in a Result
pub fn new(name: Name, description: Option<String>) -> OptDepend {
OptDepend { name, description }
}
/// Return the name of the optional dependency
pub fn name(&self) -> &Name {
&self.name
}
/// Return the description for the optional dependency, if it exists
pub fn description(&self) -> &Option<String> {
&self.description
}
}
impl FromStr for OptDepend {
type Err = Error;
/// Create an OptDepend from a string slice
fn from_str(s: &str) -> Result<OptDepend, Self::Err> {
if let Some((name, description)) = s.split_once(":") {
let description = description.trim_start();
Ok(Self::new(
Name::new(name.to_string())?,
(!description.is_empty()).then_some(description.to_string()),
))
} else {
Ok(Self::new(Name::new(s.to_string())?, None))
}
}
}
impl Display for OptDepend {
fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result {
match self.description {
Some(ref description) => write!(fmt, "{}: {}", self.name(), description),
None => write!(fmt, "{}", self.name()),
}
}
}
/// Group of a package
///
/// Represents an arbitrary collection of packages that share a common
/// characteristic or functionality.
///
/// While group names can be any valid UTF-8 string, it is recommended to follow
/// the format of [`Name`] (`[a-z\d\-._@+]` but must not start with `[-.]`)
/// to ensure consistency and ease of use.
///
/// This is a type alias for [`String`].
///
/// ## Examples
/// ```
/// use alpm_types::Group;
///
/// // Create a Group
/// let group: Group = "package-group".to_string();
/// ```
pub type Group = String;
#[cfg(test)]
mod tests {
use proptest::{prop_assert_eq, proptest, test_runner::Config as ProptestConfig};
use rstest::rstest;
use super::*;
const COMPARATOR_REGEX: &str = r"(<|<=|=|>=|>)";
/// NOTE: [`Epoch`][alpm_types::Epoch] is implicitly constrained by [`std::usize::MAX`].
/// However, it's unrealistic to ever reach that many forced downgrades for a package, hence
/// we don't test that fully
const EPOCH_REGEX: &str = r"[1-9]{1}[0-9]{0,10}";
const NAME_REGEX: &str = r"[a-z0-9_@+]+[a-z0-9\-._@+]*";
const PKGREL_REGEX: &str = r"[1-9]+[0-9]*(|[.]{1}[1-9]{1}[0-9]*)";
const PKGVER_REGEX: &str = r"([[:alnum:]][[:alnum:]_+.]*)";
const DESCRIPTION_REGEX: &str = r"[[:alnum:]][[:alnum:] _+.,-]*";
proptest! {
#![proptest_config(ProptestConfig::with_cases(1000))]
#[test]
fn valid_package_relation_from_str(s in format!("{NAME_REGEX}(|{COMPARATOR_REGEX}(|{EPOCH_REGEX}:){PKGVER_REGEX}(|-{PKGREL_REGEX}))").as_str()) {
println!("s: {s}");
let name = PackageRelation::from_str(&s).unwrap();
prop_assert_eq!(s, format!("{}", name));
}
}
proptest! {
#[test]
fn opt_depend_from_str(s in format!("{NAME_REGEX}(: {DESCRIPTION_REGEX})?").as_str()) {
println!("s: {s}");
let opt_depend = OptDepend::from_str(&s).unwrap();
let formatted = format!("{}", opt_depend);
prop_assert_eq!(s.trim_end(), formatted.trim_end(), "Formatted output doesn't match input");
}
}
#[rstest]
#[case(
"example: this is an example dependency",
Ok(OptDepend {
name: Name::new("example".to_string()).unwrap(),
description: Some("this is an example dependency".to_string()),
}),
)]
#[case(
"dep_name",
Ok(OptDepend {
name: Name::new("dep_name".to_string()).unwrap(),
description: None,
}),
)]
#[case(
"dep_name: ",
Ok(OptDepend {
name: Name::new("dep_name".to_string()).unwrap(),
description: None,
}),
)]
#[case(
"dep_name_with_special_chars-123: description with !@#$%^&*",
Ok(OptDepend {
name: Name::new("dep_name_with_special_chars-123".to_string()).unwrap(),
description: Some("description with !@#$%^&*".to_string()),
}),
)]
#[case(
"#invalid-name: this is an example dependency",
Err(Error::RegexDoesNotMatch {
value: "#invalid-name".to_string(),
regex_type: "pkgname".to_string(),
regex: crate::name::NAME_REGEX.to_string(),
}),
)]
#[case(
": no_name_colon",
Err(Error::RegexDoesNotMatch {
value: "".to_string(),
regex_type: "pkgname".to_string(),
regex: crate::name::NAME_REGEX.to_string(),
}),
)]
fn opt_depend_from_string(
#[case] input: &str,
#[case] expected_result: Result<OptDepend, Error>,
) {
let opt_depend_result = OptDepend::from_str(input);
assert_eq!(expected_result, opt_depend_result);
}
}