2024-07-12 23:09:37 +01:00
|
|
|
use std::{fmt::Display, str::FromStr};
|
|
|
|
|
|
|
|
use serde_with::{DeserializeFromStr, SerializeDisplay};
|
|
|
|
|
2024-08-03 21:18:38 +01:00
|
|
|
/// The invalid part of a package name
|
2024-07-12 23:09:37 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ErrorReason {
|
2024-08-03 21:18:38 +01:00
|
|
|
/// The scope of the package name is invalid
|
2024-07-12 23:09:37 +01:00
|
|
|
Scope,
|
2024-08-03 21:18:38 +01:00
|
|
|
/// The name of the package name is invalid
|
2024-07-12 23:09:37 +01:00
|
|
|
Name,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for ErrorReason {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
ErrorReason::Scope => write!(f, "scope"),
|
|
|
|
ErrorReason::Name => write!(f, "name"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-03 21:18:38 +01:00
|
|
|
/// A pesde package name
|
2024-07-12 23:09:37 +01:00
|
|
|
#[derive(
|
|
|
|
Debug, DeserializeFromStr, SerializeDisplay, Clone, PartialEq, Eq, Hash, PartialOrd, Ord,
|
|
|
|
)]
|
|
|
|
pub struct PackageName(String, String);
|
|
|
|
|
|
|
|
impl FromStr for PackageName {
|
|
|
|
type Err = errors::PackageNameError;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
let (scope, name) = s
|
|
|
|
.split_once('/')
|
|
|
|
.ok_or(Self::Err::InvalidFormat(s.to_string()))?;
|
|
|
|
|
|
|
|
for (reason, part) in [(ErrorReason::Scope, scope), (ErrorReason::Name, name)] {
|
|
|
|
if part.len() < 3 || part.len() > 32 {
|
|
|
|
return Err(Self::Err::InvalidLength(reason, part.to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if part.chars().all(|c| c.is_ascii_digit()) {
|
|
|
|
return Err(Self::Err::OnlyDigits(reason, part.to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if part.starts_with('_') || part.ends_with('_') {
|
|
|
|
return Err(Self::Err::PrePostfixUnderscore(reason, part.to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if !part.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
|
|
|
|
return Err(Self::Err::InvalidCharacters(reason, part.to_string()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Self(scope.to_string(), name.to_string()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for PackageName {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "{}/{}", self.0, self.1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PackageName {
|
2024-08-03 21:18:38 +01:00
|
|
|
/// Returns the parts of the package name
|
2024-07-12 23:09:37 +01:00
|
|
|
pub fn as_str(&self) -> (&str, &str) {
|
|
|
|
(&self.0, &self.1)
|
|
|
|
}
|
2024-07-17 18:38:01 +01:00
|
|
|
|
2024-08-03 21:18:38 +01:00
|
|
|
/// Returns the package name as a string suitable for use in the filesystem
|
2024-07-17 18:38:01 +01:00
|
|
|
pub fn escaped(&self) -> String {
|
|
|
|
format!("{}+{}", self.0, self.1)
|
|
|
|
}
|
2024-07-12 23:09:37 +01:00
|
|
|
}
|
|
|
|
|
2024-08-03 21:18:38 +01:00
|
|
|
/// All possible package names
|
2024-07-23 15:37:47 +01:00
|
|
|
#[derive(
|
|
|
|
Debug, DeserializeFromStr, SerializeDisplay, Clone, Hash, PartialEq, Eq, PartialOrd, Ord,
|
|
|
|
)]
|
2024-07-12 23:09:37 +01:00
|
|
|
pub enum PackageNames {
|
2024-08-03 21:18:38 +01:00
|
|
|
/// A pesde package name
|
2024-07-12 23:09:37 +01:00
|
|
|
Pesde(PackageName),
|
2024-08-08 16:59:59 +01:00
|
|
|
/// A Wally package name
|
|
|
|
#[cfg(feature = "wally-compat")]
|
|
|
|
Wally(wally::WallyPackageName),
|
2024-07-12 23:09:37 +01:00
|
|
|
}
|
|
|
|
|
2024-07-17 18:38:01 +01:00
|
|
|
impl PackageNames {
|
2024-08-03 21:18:38 +01:00
|
|
|
/// Returns the parts of the package name
|
2024-07-17 18:38:01 +01:00
|
|
|
pub fn as_str(&self) -> (&str, &str) {
|
|
|
|
match self {
|
|
|
|
PackageNames::Pesde(name) => name.as_str(),
|
2024-08-08 16:59:59 +01:00
|
|
|
#[cfg(feature = "wally-compat")]
|
|
|
|
PackageNames::Wally(name) => name.as_str(),
|
2024-07-17 18:38:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-03 21:18:38 +01:00
|
|
|
/// Returns the package name as a string suitable for use in the filesystem
|
2024-07-17 18:38:01 +01:00
|
|
|
pub fn escaped(&self) -> String {
|
|
|
|
match self {
|
|
|
|
PackageNames::Pesde(name) => name.escaped(),
|
2024-08-08 16:59:59 +01:00
|
|
|
#[cfg(feature = "wally-compat")]
|
|
|
|
PackageNames::Wally(name) => name.escaped(),
|
2024-07-17 18:38:01 +01:00
|
|
|
}
|
|
|
|
}
|
2024-08-10 13:25:46 +01:00
|
|
|
|
|
|
|
/// The reverse of `escaped`
|
|
|
|
pub fn from_escaped(s: &str) -> Result<Self, errors::PackageNamesError> {
|
|
|
|
PackageNames::from_str(s.replacen('+', "/", 1).as_str())
|
|
|
|
}
|
2024-07-17 18:38:01 +01:00
|
|
|
}
|
|
|
|
|
2024-07-14 14:19:15 +01:00
|
|
|
impl Display for PackageNames {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
2024-07-22 15:41:45 +01:00
|
|
|
PackageNames::Pesde(name) => write!(f, "{name}"),
|
2024-08-08 16:59:59 +01:00
|
|
|
#[cfg(feature = "wally-compat")]
|
|
|
|
PackageNames::Wally(name) => write!(f, "{name}"),
|
2024-07-14 14:19:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-23 15:37:47 +01:00
|
|
|
impl FromStr for PackageNames {
|
|
|
|
type Err = errors::PackageNamesError;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
2024-08-08 16:59:59 +01:00
|
|
|
#[cfg(feature = "wally-compat")]
|
|
|
|
if let Some(wally_name) = s
|
|
|
|
.strip_prefix("wally#")
|
|
|
|
.or_else(|| if s.contains('-') { Some(s) } else { None })
|
|
|
|
.and_then(|s| wally::WallyPackageName::from_str(s).ok())
|
|
|
|
{
|
|
|
|
return Ok(PackageNames::Wally(wally_name));
|
|
|
|
}
|
|
|
|
|
2024-07-23 15:37:47 +01:00
|
|
|
if let Ok(name) = PackageName::from_str(s) {
|
|
|
|
Ok(PackageNames::Pesde(name))
|
|
|
|
} else {
|
|
|
|
Err(errors::PackageNamesError::InvalidPackageName(s.to_string()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-08 16:59:59 +01:00
|
|
|
/// Wally package names
|
|
|
|
#[cfg(feature = "wally-compat")]
|
|
|
|
pub mod wally {
|
|
|
|
use std::{fmt::Display, str::FromStr};
|
|
|
|
|
|
|
|
use serde_with::{DeserializeFromStr, SerializeDisplay};
|
|
|
|
|
|
|
|
use crate::names::{errors, ErrorReason};
|
|
|
|
|
|
|
|
/// A Wally package name
|
|
|
|
#[derive(
|
|
|
|
Debug, DeserializeFromStr, SerializeDisplay, Clone, PartialEq, Eq, Hash, PartialOrd, Ord,
|
|
|
|
)]
|
|
|
|
pub struct WallyPackageName(String, String);
|
|
|
|
|
|
|
|
impl FromStr for WallyPackageName {
|
|
|
|
type Err = errors::WallyPackageNameError;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
let (scope, name) = s
|
|
|
|
.strip_prefix("wally#")
|
|
|
|
.unwrap_or(s)
|
|
|
|
.split_once('/')
|
|
|
|
.ok_or(Self::Err::InvalidFormat(s.to_string()))?;
|
|
|
|
|
|
|
|
for (reason, part) in [(ErrorReason::Scope, scope), (ErrorReason::Name, name)] {
|
|
|
|
if part.is_empty() || part.len() > 64 {
|
|
|
|
return Err(Self::Err::InvalidLength(reason, part.to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if !part.chars().all(|c| c.is_ascii_alphanumeric() || c == '-') {
|
|
|
|
return Err(Self::Err::InvalidCharacters(reason, part.to_string()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Self(scope.to_string(), name.to_string()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for WallyPackageName {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "wally#{}/{}", self.0, self.1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WallyPackageName {
|
|
|
|
/// Returns the parts of the package name
|
|
|
|
pub fn as_str(&self) -> (&str, &str) {
|
|
|
|
(&self.0, &self.1)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the package name as a string suitable for use in the filesystem
|
|
|
|
pub fn escaped(&self) -> String {
|
|
|
|
format!("wally#{}+{}", self.0, self.1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-03 21:18:38 +01:00
|
|
|
/// Errors that can occur when working with package names
|
2024-07-12 23:09:37 +01:00
|
|
|
pub mod errors {
|
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
use crate::names::ErrorReason;
|
|
|
|
|
2024-08-03 21:18:38 +01:00
|
|
|
/// Errors that can occur when working with pesde package names
|
2024-07-12 23:09:37 +01:00
|
|
|
#[derive(Debug, Error)]
|
|
|
|
pub enum PackageNameError {
|
2024-08-03 21:18:38 +01:00
|
|
|
/// The package name is not in the format `scope/name`
|
2024-07-12 23:09:37 +01:00
|
|
|
#[error("package name `{0}` is not in the format `scope/name`")]
|
|
|
|
InvalidFormat(String),
|
|
|
|
|
2024-08-03 21:18:38 +01:00
|
|
|
/// The package name is outside the allowed characters: a-z, 0-9, and _
|
2024-07-12 23:09:37 +01:00
|
|
|
#[error("package {0} `{1}` contains characters outside a-z, 0-9, and _")]
|
|
|
|
InvalidCharacters(ErrorReason, String),
|
|
|
|
|
2024-08-03 21:18:38 +01:00
|
|
|
/// The package name contains only digits
|
2024-07-12 23:09:37 +01:00
|
|
|
#[error("package {0} `{1}` contains only digits")]
|
|
|
|
OnlyDigits(ErrorReason, String),
|
|
|
|
|
2024-08-03 21:18:38 +01:00
|
|
|
/// The package name starts or ends with an underscore
|
2024-07-12 23:09:37 +01:00
|
|
|
#[error("package {0} `{1}` starts or ends with an underscore")]
|
|
|
|
PrePostfixUnderscore(ErrorReason, String),
|
|
|
|
|
2024-08-03 21:18:38 +01:00
|
|
|
/// The package name is not within 3-32 characters long
|
2024-07-12 23:09:37 +01:00
|
|
|
#[error("package {0} `{1}` is not within 3-32 characters long")]
|
|
|
|
InvalidLength(ErrorReason, String),
|
|
|
|
}
|
2024-07-23 15:37:47 +01:00
|
|
|
|
2024-08-08 16:59:59 +01:00
|
|
|
/// Errors that can occur when working with Wally package names
|
|
|
|
#[cfg(feature = "wally-compat")]
|
|
|
|
#[derive(Debug, Error)]
|
|
|
|
pub enum WallyPackageNameError {
|
|
|
|
/// The package name is not in the format `scope/name`
|
|
|
|
#[error("wally package name `{0}` is not in the format `scope/name`")]
|
|
|
|
InvalidFormat(String),
|
|
|
|
|
|
|
|
/// The package name is outside the allowed characters: a-z, 0-9, and -
|
|
|
|
#[error("wally package {0} `{1}` contains characters outside a-z, 0-9, and -")]
|
|
|
|
InvalidCharacters(ErrorReason, String),
|
|
|
|
|
|
|
|
/// The package name is not within 1-64 characters long
|
|
|
|
#[error("wally package {0} `{1}` is not within 1-64 characters long")]
|
|
|
|
InvalidLength(ErrorReason, String),
|
|
|
|
}
|
|
|
|
|
2024-08-03 21:18:38 +01:00
|
|
|
/// Errors that can occur when working with package names
|
2024-07-23 15:37:47 +01:00
|
|
|
#[derive(Debug, Error)]
|
|
|
|
#[non_exhaustive]
|
|
|
|
pub enum PackageNamesError {
|
2024-08-08 16:59:59 +01:00
|
|
|
/// The package name is invalid
|
2024-07-23 15:37:47 +01:00
|
|
|
#[error("invalid package name {0}")]
|
|
|
|
InvalidPackageName(String),
|
|
|
|
}
|
2024-07-12 23:09:37 +01:00
|
|
|
}
|