feat: relax plugin identifier requirements to alphanumeric and - (#8856)

closes #8820
This commit is contained in:
Amr Bashir 2024-02-16 12:27:56 +02:00 committed by GitHub
parent 11a5816bdf
commit 5618f6d2ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 8 deletions

View File

@ -0,0 +1,6 @@
---
'tauri': 'patch:enhance'
'tauri-utils': 'patch:enhance'
---
Relax requirements on plugin's identifiers to be alphanumeric and `-` instead of only lower alpha and `-`.

View File

@ -73,12 +73,12 @@ enum ValidByte {
}
impl ValidByte {
fn lower_alpha(byte: u8) -> Option<Self> {
byte.is_ascii_lowercase().then_some(Self::Byte(byte))
fn alpha_numeric(byte: u8) -> Option<Self> {
byte.is_ascii_alphanumeric().then_some(Self::Byte(byte))
}
fn lower_alpha_hyphen(byte: u8) -> Option<Self> {
matches!(byte, b'a'..=b'z' | b'-').then_some(Self::Byte(byte))
fn alpha_numeric_hyphen(byte: u8) -> Option<Self> {
(byte.is_ascii_alphanumeric() || byte == b'-').then_some(Self::Byte(byte))
}
fn next(&self, next: u8) -> Option<ValidByte> {
@ -87,9 +87,9 @@ impl ValidByte {
(ValidByte::Separator, b'-') => None,
(_, IDENTIFIER_SEPARATOR) => Some(ValidByte::Separator),
(ValidByte::Separator, next) => ValidByte::lower_alpha(next),
(ValidByte::Byte(b'-'), next) => ValidByte::lower_alpha(next),
(ValidByte::Byte(_), next) => ValidByte::lower_alpha_hyphen(next),
(ValidByte::Separator, next) => ValidByte::alpha_numeric(next),
(ValidByte::Byte(b'-'), next) => ValidByte::alpha_numeric(next),
(ValidByte::Byte(_), next) => ValidByte::alpha_numeric_hyphen(next),
}
}
}
@ -149,7 +149,7 @@ impl TryFrom<String> for Identifier {
// grab the first byte only before parsing the rest
let mut prev = bytes
.next()
.and_then(ValidByte::lower_alpha)
.and_then(ValidByte::alpha_numeric)
.ok_or(Self::Error::InvalidFormat)?;
let mut idx = 0;
@ -222,6 +222,8 @@ mod tests {
#[test]
fn format() {
assert!(ident("prefix:base").is_ok());
assert!(ident("prefix3:base").is_ok());
assert!(ident("preFix:base").is_ok());
// bad
assert!(ident("tauri-plugin-prefix:base").is_err());