-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#394 Add possibility to provide group and mapping tags (searchable)
- Loading branch information
Showing
22 changed files
with
306 additions
and
79 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use ascii::{AsciiStr, AsciiString}; | ||
use core::fmt; | ||
|
||
/// String with a maximum of 16 ASCII characters. | ||
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)] | ||
pub struct SmallAsciiString { | ||
length: u8, | ||
content: [u8; SmallAsciiString::MAX_LENGTH], | ||
} | ||
|
||
impl SmallAsciiString { | ||
pub const MAX_LENGTH: usize = 16; | ||
|
||
/// Crops the string if necessary. | ||
pub fn from_ascii_str_cropping(ascii_str: &AsciiStr) -> Self { | ||
let short = | ||
AsciiString::from(&ascii_str.as_slice()[..Self::MAX_LENGTH.min(ascii_str.len())]); | ||
Self::from_ascii_str(&short) | ||
} | ||
|
||
/// Returns an error if the given string is too long. | ||
pub fn try_from_ascii_str(ascii_str: &AsciiStr) -> Result<Self, &'static str> { | ||
if ascii_str.len() > SmallAsciiString::MAX_LENGTH { | ||
return Err("too large to be a small ASCII string"); | ||
} | ||
Ok(Self::from_ascii_str(ascii_str)) | ||
} | ||
|
||
/// Panics if the given string is too long. | ||
fn from_ascii_str(ascii_str: &AsciiStr) -> Self { | ||
let mut content = [0u8; SmallAsciiString::MAX_LENGTH]; | ||
content[..ascii_str.len()].copy_from_slice(ascii_str.as_bytes()); | ||
Self { | ||
content, | ||
length: ascii_str.len() as u8, | ||
} | ||
} | ||
|
||
pub fn as_ascii_str(&self) -> &AsciiStr { | ||
AsciiStr::from_ascii(self.as_slice()).unwrap() | ||
} | ||
|
||
pub fn as_slice(&self) -> &[u8] { | ||
&self.content[..(self.length as usize)] | ||
} | ||
} | ||
|
||
impl fmt::Display for SmallAsciiString { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
self.as_ascii_str().fmt(f) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use crate::domain::SmallAsciiString; | ||
use ascii::{AsciiChar, AsciiString, ToAsciiChar}; | ||
use core::fmt; | ||
use serde_with::{DeserializeFromStr, SerializeDisplay}; | ||
use std::fmt::{Display, Formatter}; | ||
use std::str::FromStr; | ||
|
||
/// We reduce the number of possible letters in case we want to use tags in the audio thread in | ||
/// future (and therefore need to avoid allocation). | ||
#[derive( | ||
Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash, SerializeDisplay, DeserializeFromStr, | ||
)] | ||
pub struct Tag(SmallAsciiString); | ||
|
||
impl FromStr for Tag { | ||
type Err = &'static str; | ||
|
||
fn from_str(text: &str) -> Result<Self, Self::Err> { | ||
let ascii_string: AsciiString = text | ||
.chars() | ||
// Remove all non-ASCII schars | ||
.filter_map(|c| c.to_ascii_char().ok()) | ||
// Allow only letters, digits and underscore | ||
.filter(|c| c.is_ascii_alphanumeric() || *c == AsciiChar::UnderScore) | ||
// Skip leading digits | ||
.skip_while(|c| c.is_ascii_digit()) | ||
.collect(); | ||
if ascii_string.is_empty() { | ||
return Err("empty tag"); | ||
} | ||
let small_ascii_string = SmallAsciiString::from_ascii_str_cropping(&ascii_string); | ||
Ok(Self(small_ascii_string)) | ||
} | ||
} | ||
|
||
impl Display for Tag { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | ||
self.0.fmt(f) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
pub fn parse_tags() { | ||
assert_eq!(Tag::from_str("hey").unwrap().to_string(), "hey"); | ||
assert_eq!(Tag::from_str("hey_test").unwrap().to_string(), "hey_test"); | ||
assert_eq!( | ||
Tag::from_str("1ähey1ätest").unwrap().to_string(), | ||
"hey1test" | ||
); | ||
assert!(Tag::from_str("1ä").is_err()); | ||
} | ||
} |
Oops, something went wrong.