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