Skip to content

Commit

Permalink
docs(schema): Document newtypes
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Dec 15, 2023
1 parent acef3c0 commit cfa9421
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/cargo/util_schemas/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,7 @@ impl TomlTarget {

macro_rules! str_newtype {
($name:ident) => {
/// Verified string newtype
#[derive(Serialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[serde(transparent)]
pub struct $name<T: AsRef<str> = String>(T);
Expand Down Expand Up @@ -1171,13 +1172,17 @@ macro_rules! str_newtype {
str_newtype!(PackageName);

impl<T: AsRef<str>> PackageName<T> {
/// Validated package name
pub fn new(name: T) -> Result<Self> {
restricted_names::validate_package_name(name.as_ref(), "package name", "")?;
Ok(Self(name))
}
}

impl PackageName {
/// Coerce a value to be a validate package name
///
/// Replaces invalid values with `placeholder`
pub fn sanitize(name: impl AsRef<str>, placeholder: char) -> Self {
PackageName(restricted_names::sanitize_package_name(
name.as_ref(),
Expand All @@ -1189,6 +1194,7 @@ impl PackageName {
str_newtype!(RegistryName);

impl<T: AsRef<str>> RegistryName<T> {
/// Validated registry name
pub fn new(name: T) -> Result<Self> {
restricted_names::validate_package_name(name.as_ref(), "registry name", "")?;
Ok(Self(name))
Expand All @@ -1198,6 +1204,7 @@ impl<T: AsRef<str>> RegistryName<T> {
str_newtype!(ProfileName);

impl<T: AsRef<str>> ProfileName<T> {
/// Validated profile name
pub fn new(name: T) -> Result<Self> {
restricted_names::validate_profile_name(name.as_ref())?;
Ok(Self(name))
Expand All @@ -1207,6 +1214,7 @@ impl<T: AsRef<str>> ProfileName<T> {
str_newtype!(FeatureName);

impl<T: AsRef<str>> FeatureName<T> {
/// Validated feature name
pub fn new(name: T) -> Result<Self> {
restricted_names::validate_feature_name(name.as_ref())?;
Ok(Self(name))
Expand Down

0 comments on commit cfa9421

Please sign in to comment.