valence/src/ident.rs

309 lines
8.3 KiB
Rust
Raw Normal View History

2022-07-15 16:18:20 +10:00
//! Namespaced identifiers.
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;
2022-07-11 22:08:02 +10:00
use crate::protocol_inner::{encode_string_bounded, BoundedString, Decode, Encode};
2022-04-15 07:55:45 +10:00
/// 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)
2022-07-11 22:08:02 +10:00
/// the namespace is considered to be "minecraft" for the purposes of equality.
2022-04-15 07:55:45 +10:00
///
2022-07-11 22:08:02 +10:00
/// The identifier must match the regex `^([a-z0-9_-]+:)?[a-z0-9_\/.-]+$`.
2022-04-15 07:55:45 +10:00
#[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,
}
2022-07-15 16:18:20 +10:00
/// The error type which is created when an [`Ident`] cannot be parsed from a
/// string.
#[derive(Clone, Debug, Error)]
2022-04-15 07:55:45 +10:00
#[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.
///
2022-07-15 16:18:20 +10:00
/// An error is returned if the string is not a valid identifier.
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()
}
}
2022-07-15 16:18:20 +10:00
/// Returns the original string as a `str`.
2022-04-15 07:55:45 +10:00
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 {
2022-07-15 16:18:20 +10:00
/// Gets the string that caused the parse error.
2022-04-15 07:55:45 +10:00
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
}
}
2022-07-11 22:08:02 +10:00
/// Convenience macro for constructing an [`Ident`] from a format string.
2022-04-15 07:55:45 +10:00
///
2022-07-11 22:08:02 +10:00
/// The arguments to this macro are forwarded to [`std::format_args`].
///
/// # Panics
///
/// The macro will cause a panic if the formatted string is not a valid
2022-04-15 07:55:45 +10:00
/// identifier.
2022-07-11 22:08:02 +10:00
///
/// # Examples
///
/// ```
/// use valence::ident;
///
/// let namespace = "my_namespace";
/// let apple = ident!("{namespace}:apple");
///
/// assert_eq!(apple.namespace(), Some("my_namespace"));
/// assert_eq!(apple.name(), "apple");
/// ```
2022-04-15 07:55:45 +10:00
#[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() {
Some(s) => $crate::ident::Ident::new(s).expect(errmsg),
None => $crate::ident::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"));
}
}