valence/src/ident.rs

289 lines
7.8 KiB
Rust
Raw Normal View History

2022-04-15 07:55:45 +10:00
use std::borrow::Cow;
use std::io::{Read, Write};
use std::str::FromStr;
use ascii::{AsAsciiStr, AsciiChar, AsciiStr, IntoAsciiString};
use serde::de::Visitor;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::protocol::{encode_string_bounded, BoundedString, Decode, Encode};
/// An identifier is a string split into a "namespace" part and a "name" part.
/// For instance `minecraft:apple` and `apple` are both valid identifiers.
///
/// If the namespace part is left off (the part before and including the colon)
/// the namespace is considered to be "minecraft".
///
/// The entire identifier must match the regex `([a-z0-9_-]+:)?[a-z0-9_\/.-]+`.
#[derive(Clone, Eq)]
2022-06-10 13:26:21 +10:00
pub struct Ident {
2022-04-15 07:55:45 +10:00
ident: Cow<'static, AsciiStr>,
/// The index of the ':' character in the string.
/// If there is no namespace then it is `usize::MAX`.
///
/// Since the string only contains ASCII characters, we can slice it
/// in O(1) time.
colon_idx: usize,
}
#[derive(Clone, Error, PartialEq, Eq, Debug)]
#[error("invalid identifier \"{src}\"")]
pub struct ParseError {
src: Cow<'static, str>,
}
2022-06-10 13:26:21 +10:00
impl Ident {
2022-04-15 07:55:45 +10:00
/// Parses a new identifier from a string.
///
/// The string must match the regex `([a-z0-9_-]+:)?[a-z0-9_\/.-]+`.
/// If not, an error is returned.
2022-06-10 13:26:21 +10:00
pub fn new(str: impl Into<Cow<'static, str>>) -> Result<Ident, ParseError> {
2022-04-15 07:55:45 +10:00
#![allow(bindings_with_variant_name)]
let cow = match str.into() {
Cow::Borrowed(s) => {
Cow::Borrowed(s.as_ascii_str().map_err(|_| ParseError { src: s.into() })?)
}
Cow::Owned(s) => Cow::Owned(s.into_ascii_string().map_err(|e| ParseError {
src: e.into_source().into(),
})?),
};
let s = cow.as_ref();
let check_namespace = |s: &AsciiStr| {
!s.is_empty()
&& s.chars()
.all(|c| matches!(c.as_char(), 'a'..='z' | '0'..='9' | '_' | '-'))
};
let check_name = |s: &AsciiStr| {
!s.is_empty()
&& s.chars()
.all(|c| matches!(c.as_char(), 'a'..='z' | '0'..='9' | '_' | '/' | '.' | '-'))
};
if let Some(colon_idx) = s.chars().position(|c| c == AsciiChar::Colon) {
if check_namespace(&s[..colon_idx]) && check_name(&s[colon_idx + 1..]) {
Ok(Self {
ident: cow,
colon_idx,
})
} else {
Err(ParseError {
src: ascii_cow_to_str_cow(cow),
})
}
} else if check_name(s) {
Ok(Self {
ident: cow,
colon_idx: usize::MAX,
})
} else {
Err(ParseError {
src: ascii_cow_to_str_cow(cow),
})
}
}
/// Returns the namespace part of this namespaced identifier.
/// If this identifier was constructed from a string without a namespace,
/// then `None` is returned.
pub fn namespace(&self) -> Option<&str> {
if self.colon_idx == usize::MAX {
None
} else {
Some(self.ident[..self.colon_idx].as_str())
}
}
/// Returns the name part of this namespaced identifier.
pub fn name(&self) -> &str {
if self.colon_idx == usize::MAX {
self.ident.as_str()
} else {
self.ident[self.colon_idx + 1..].as_str()
}
}
/// Returns the identifier as a `str`.
pub fn as_str(&self) -> &str {
self.ident.as_str()
}
}
fn ascii_cow_to_str_cow(cow: Cow<AsciiStr>) -> Cow<str> {
match cow {
Cow::Borrowed(s) => Cow::Borrowed(s.as_str()),
Cow::Owned(s) => Cow::Owned(s.into()),
}
}
impl ParseError {
pub fn into_source(self) -> Cow<'static, str> {
self.src
}
}
2022-06-10 13:26:21 +10:00
impl std::fmt::Debug for Ident {
2022-04-15 07:55:45 +10:00
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("Identifier").field(&self.as_str()).finish()
}
}
2022-06-10 13:26:21 +10:00
impl FromStr for Ident {
2022-04-15 07:55:45 +10:00
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ident::new(s.to_owned())
2022-04-15 07:55:45 +10:00
}
}
2022-06-10 13:26:21 +10:00
impl From<Ident> for String {
fn from(id: Ident) -> Self {
2022-04-15 07:55:45 +10:00
id.ident.into_owned().into()
}
}
2022-06-10 13:26:21 +10:00
impl From<Ident> for Cow<'static, str> {
fn from(id: Ident) -> Self {
2022-04-15 07:55:45 +10:00
ascii_cow_to_str_cow(id.ident)
}
}
2022-06-10 13:26:21 +10:00
impl AsRef<str> for Ident {
2022-04-15 07:55:45 +10:00
fn as_ref(&self) -> &str {
self.as_str()
}
}
2022-06-10 13:26:21 +10:00
impl TryFrom<String> for Ident {
2022-04-15 07:55:45 +10:00
type Error = ParseError;
fn try_from(value: String) -> Result<Self, Self::Error> {
2022-06-10 13:26:21 +10:00
Ident::new(value)
2022-04-15 07:55:45 +10:00
}
}
2022-06-10 13:26:21 +10:00
impl TryFrom<&'static str> for Ident {
2022-04-15 07:55:45 +10:00
type Error = ParseError;
fn try_from(value: &'static str) -> Result<Self, Self::Error> {
2022-06-10 13:26:21 +10:00
Ident::new(value)
2022-04-15 07:55:45 +10:00
}
}
2022-06-10 13:26:21 +10:00
impl std::fmt::Display for Ident {
2022-04-15 07:55:45 +10:00
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.as_str())
}
}
/// Equality for identifiers respects the fact that "minecraft:apple" and
/// "apple" have the same meaning.
2022-06-10 13:26:21 +10:00
impl PartialEq for Ident {
2022-04-15 07:55:45 +10:00
fn eq(&self, other: &Self) -> bool {
self.namespace().unwrap_or("minecraft") == other.namespace().unwrap_or("minecraft")
&& self.name() == other.name()
}
}
2022-06-10 13:26:21 +10:00
impl std::hash::Hash for Ident {
2022-04-15 07:55:45 +10:00
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.namespace().unwrap_or("minecraft").hash(state);
self.name().hash(state);
}
}
2022-06-10 13:26:21 +10:00
impl Encode for Ident {
2022-04-15 07:55:45 +10:00
fn encode(&self, w: &mut impl Write) -> anyhow::Result<()> {
encode_string_bounded(self.as_str(), 0, 32767, w)
}
}
2022-06-10 13:26:21 +10:00
impl Decode for Ident {
2022-04-15 07:55:45 +10:00
fn decode(r: &mut impl Read) -> anyhow::Result<Self> {
let string = BoundedString::<0, 32767>::decode(r)?.0;
2022-06-10 13:26:21 +10:00
Ok(Ident::new(string)?)
2022-04-15 07:55:45 +10:00
}
}
2022-06-10 13:26:21 +10:00
impl Serialize for Ident {
2022-04-15 07:55:45 +10:00
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.as_str().serialize(serializer)
}
}
2022-06-10 13:26:21 +10:00
impl<'de> Deserialize<'de> for Ident {
2022-04-15 07:55:45 +10:00
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
deserializer.deserialize_str(IdentifierVisitor)
}
}
/// An implementation of `serde::de::Visitor` for Minecraft identifiers.
struct IdentifierVisitor;
impl<'de> Visitor<'de> for IdentifierVisitor {
2022-06-10 13:26:21 +10:00
type Value = Ident;
2022-04-15 07:55:45 +10:00
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "a valid Minecraft identifier")
}
fn visit_str<E: serde::de::Error>(self, s: &str) -> Result<Self::Value, E> {
2022-06-10 13:26:21 +10:00
Ident::from_str(s).map_err(E::custom)
2022-04-15 07:55:45 +10:00
}
fn visit_string<E: serde::de::Error>(self, s: String) -> Result<Self::Value, E> {
2022-06-10 13:26:21 +10:00
Ident::new(s).map_err(E::custom)
2022-04-15 07:55:45 +10:00
}
}
/// Convenience macro for constructing an identifier from a format string.
///
/// The macro will panic if the formatted string is not a valid
/// identifier.
#[macro_export]
macro_rules! ident {
($($arg:tt)*) => {{
let errmsg = "invalid identifier in `ident` macro";
#[allow(clippy::redundant_closure_call)]
(|args: ::std::fmt::Arguments| match args.as_str() {
2022-06-10 13:26:21 +10:00
Some(s) => $crate::Ident::new(s).expect(errmsg),
None => $crate::Ident::new(args.to_string()).expect(errmsg),
2022-04-15 07:55:45 +10:00
})(format_args!($($arg)*))
}}
}
#[cfg(test)]
mod tests {
#[test]
fn parse_valid() {
ident!("minecraft:whatever");
ident!("_what-ever55_:.whatever/whatever123456789_");
}
#[test]
#[should_panic]
fn parse_invalid_0() {
ident!("");
}
#[test]
#[should_panic]
fn parse_invalid_1() {
ident!(":");
}
#[test]
#[should_panic]
fn parse_invalid_2() {
ident!("foo:bar:baz");
}
#[test]
fn equality() {
assert_eq!(ident!("minecraft:my.identifier"), ident!("my.identifier"));
}
}