2022-10-11 19:10:49 +11:00
|
|
|
//! Resource identifiers.
|
2022-07-15 16:18:20 +10:00
|
|
|
|
2023-03-26 12:44:45 +11:00
|
|
|
use std::borrow::{Borrow, Cow};
|
2022-10-11 19:10:49 +11:00
|
|
|
use std::cmp::Ordering;
|
2022-10-12 21:53:59 +11:00
|
|
|
use std::fmt;
|
|
|
|
use std::fmt::Formatter;
|
2022-09-09 14:39:08 +10:00
|
|
|
use std::io::Write;
|
2022-04-15 07:55:45 +10:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
2022-10-12 21:53:59 +11:00
|
|
|
use serde::de::Error as _;
|
|
|
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
2023-03-26 12:44:45 +11:00
|
|
|
use thiserror::Error;
|
2022-04-15 07:55:45 +10:00
|
|
|
|
2023-05-29 18:36:11 +10:00
|
|
|
use crate::protocol::{Decode, Encode};
|
Reorganize Project (#321)
## Description
- `valence` and `valence_protocol` have been divided into smaller crates
in order to parallelize the build and improve IDE responsiveness. In the
process, code architecture has been made clearer by removing circular
dependencies between modules. `valence` is now just a shell around the
other crates.
- `workspace.packages` and `workspace.dependencies` are now used. This
makes dependency managements and crate configuration much easier.
- `valence_protocol` is no more. Most things from `valence_protocol`
ended up in `valence_core`. We won't advertise `valence_core` as a
general-purpose protocol library since it contains too much
valence-specific stuff. Closes #308.
- Networking code (login, initial TCP connection handling, etc.) has
been extracted into the `valence_network` crate. The API has been
expanded and improved with better defaults. Player counts and initial
connections to the server are now tracked separately. Player counts
function by default without any user configuration.
- Some crates like `valence_anvil`, `valence_network`,
`valence_player_list`, `valence_inventory`, etc. are now optional. They
can be enabled/disabled with feature flags and `DefaultPlugins` just
like bevy.
- Whole-server unit tests have been moved to `valence/src/tests` in
order to avoid [cyclic
dev-dependencies](https://github.com/rust-lang/cargo/issues/4242).
- Tools like `valence_stresser` and `packet_inspector` have been moved
to a new `tools` directory. Renamed `valence_stresser` to `stresser`.
Closes #241.
- Moved all benches to `valence/benches/` to make them easier to run and
organize.
Ignoring transitive dependencies and `valence_core`, here's what the
dependency graph looks like now:
```mermaid
graph TD
network --> client
client --> instance
biome --> registry
dimension --> registry
instance --> biome
instance --> dimension
instance --> entity
player_list --> client
inventory --> client
anvil --> instance
entity --> block
```
### Issues
- Inventory tests inspect many private implementation details of the
inventory module, forcing us to mark things as `pub` and
`#[doc(hidden)]`. It would be ideal if the tests only looked at
observable behavior.
- Consider moving packets in `valence_core` elsewhere. `Particle` wants
to use `BlockState`, but that's defined in `valence_block`, so we can't
use it without causing cycles.
- Unsure what exactly should go in `valence::prelude`.
- This could use some more tests of course, but I'm holding off on that
until I'm confident this is the direction we want to take things.
## TODOs
- [x] Update examples.
- [x] Update benches.
- [x] Update main README.
- [x] Add short READMEs to crates.
- [x] Test new schedule to ensure behavior is the same.
- [x] Update tools.
- [x] Copy lints to all crates.
- [x] Fix docs, clippy, etc.
2023-04-22 07:43:59 +10:00
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub mod __private {
|
|
|
|
pub use valence_core_macros::parse_ident_str;
|
|
|
|
}
|
2022-04-15 07:55:45 +10:00
|
|
|
|
2022-10-12 21:53:59 +11:00
|
|
|
/// A wrapper around a string type `S` which guarantees the wrapped string is a
|
|
|
|
/// valid resource identifier.
|
|
|
|
///
|
2022-10-11 19:10:49 +11:00
|
|
|
/// A resource identifier is a string divided into a "namespace" part and a
|
|
|
|
/// "path" part. For instance `minecraft:apple` and `valence:frobnicator` are
|
2022-10-14 16:28:12 +11:00
|
|
|
/// both valid identifiers. A string must match the regex
|
2023-03-26 12:44:45 +11:00
|
|
|
/// `^([a-z0-9_.-]+:)?[a-z0-9_.-\/]+$` to be successfully parsed.
|
2022-04-15 07:55:45 +10:00
|
|
|
///
|
2023-03-26 12:44:45 +11:00
|
|
|
/// While parsing, if the namespace part is left off (the part before and
|
|
|
|
/// including the colon) then "minecraft:" is inserted at the beginning of the
|
|
|
|
/// string.
|
|
|
|
#[derive(Copy, Clone, Eq, Ord, Hash)]
|
2022-10-12 21:53:59 +11:00
|
|
|
pub struct Ident<S> {
|
|
|
|
string: S,
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
|
Reorganize Project (#321)
## Description
- `valence` and `valence_protocol` have been divided into smaller crates
in order to parallelize the build and improve IDE responsiveness. In the
process, code architecture has been made clearer by removing circular
dependencies between modules. `valence` is now just a shell around the
other crates.
- `workspace.packages` and `workspace.dependencies` are now used. This
makes dependency managements and crate configuration much easier.
- `valence_protocol` is no more. Most things from `valence_protocol`
ended up in `valence_core`. We won't advertise `valence_core` as a
general-purpose protocol library since it contains too much
valence-specific stuff. Closes #308.
- Networking code (login, initial TCP connection handling, etc.) has
been extracted into the `valence_network` crate. The API has been
expanded and improved with better defaults. Player counts and initial
connections to the server are now tracked separately. Player counts
function by default without any user configuration.
- Some crates like `valence_anvil`, `valence_network`,
`valence_player_list`, `valence_inventory`, etc. are now optional. They
can be enabled/disabled with feature flags and `DefaultPlugins` just
like bevy.
- Whole-server unit tests have been moved to `valence/src/tests` in
order to avoid [cyclic
dev-dependencies](https://github.com/rust-lang/cargo/issues/4242).
- Tools like `valence_stresser` and `packet_inspector` have been moved
to a new `tools` directory. Renamed `valence_stresser` to `stresser`.
Closes #241.
- Moved all benches to `valence/benches/` to make them easier to run and
organize.
Ignoring transitive dependencies and `valence_core`, here's what the
dependency graph looks like now:
```mermaid
graph TD
network --> client
client --> instance
biome --> registry
dimension --> registry
instance --> biome
instance --> dimension
instance --> entity
player_list --> client
inventory --> client
anvil --> instance
entity --> block
```
### Issues
- Inventory tests inspect many private implementation details of the
inventory module, forcing us to mark things as `pub` and
`#[doc(hidden)]`. It would be ideal if the tests only looked at
observable behavior.
- Consider moving packets in `valence_core` elsewhere. `Particle` wants
to use `BlockState`, but that's defined in `valence_block`, so we can't
use it without causing cycles.
- Unsure what exactly should go in `valence::prelude`.
- This could use some more tests of course, but I'm holding off on that
until I'm confident this is the direction we want to take things.
## TODOs
- [x] Update examples.
- [x] Update benches.
- [x] Update main README.
- [x] Add short READMEs to crates.
- [x] Test new schedule to ensure behavior is the same.
- [x] Update tools.
- [x] Copy lints to all crates.
- [x] Fix docs, clippy, etc.
2023-04-22 07:43:59 +10:00
|
|
|
/// Creates a new [`Ident`] at compile time from a string literal. A compile
|
|
|
|
/// error is raised if the string is not a valid resource identifier.
|
|
|
|
///
|
|
|
|
/// The type of the expression returned by this macro is `Ident<&'static str>`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use valence_core::{ident, ident::Ident};
|
|
|
|
/// let my_ident: Ident<&'static str> = ident!("apple");
|
|
|
|
///
|
|
|
|
/// println!("{my_ident}");
|
|
|
|
/// ```
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! ident {
|
|
|
|
($string:literal) => {
|
|
|
|
$crate::ident::Ident::<&'static str>::new_unchecked(
|
|
|
|
$crate::ident::__private::parse_ident_str!($string),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-03-26 12:44:45 +11:00
|
|
|
/// The error type created when an [`Ident`] cannot be parsed from a
|
|
|
|
/// string. Contains the string that failed to parse.
|
|
|
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Error)]
|
|
|
|
#[error("invalid resource identifier \"{0}\"")]
|
|
|
|
pub struct IdentError(pub String);
|
2022-04-15 07:55:45 +10:00
|
|
|
|
2023-03-26 12:44:45 +11:00
|
|
|
impl<'a> Ident<Cow<'a, str>> {
|
|
|
|
pub fn new(string: impl Into<Cow<'a, str>>) -> Result<Self, IdentError> {
|
|
|
|
parse(string.into())
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
2023-03-26 12:44:45 +11:00
|
|
|
}
|
2022-04-15 07:55:45 +10:00
|
|
|
|
2023-03-26 12:44:45 +11:00
|
|
|
impl<S> Ident<S> {
|
|
|
|
/// Internal API. Do not use.
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub const fn new_unchecked(string: S) -> Self {
|
|
|
|
Self { string }
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
|
2023-03-26 12:44:45 +11:00
|
|
|
pub fn as_str(&self) -> &str
|
|
|
|
where
|
|
|
|
S: AsRef<str>,
|
|
|
|
{
|
2022-10-14 16:28:12 +11:00
|
|
|
self.string.as_ref()
|
2022-10-11 19:10:49 +11:00
|
|
|
}
|
|
|
|
|
2023-03-26 12:44:45 +11:00
|
|
|
pub fn as_str_ident(&self) -> Ident<&str>
|
|
|
|
where
|
|
|
|
S: AsRef<str>,
|
|
|
|
{
|
2022-10-12 21:53:59 +11:00
|
|
|
Ident {
|
2023-03-26 12:44:45 +11:00
|
|
|
string: self.as_str(),
|
2022-10-12 21:53:59 +11:00
|
|
|
}
|
2022-10-11 19:10:49 +11:00
|
|
|
}
|
|
|
|
|
2023-04-01 08:58:47 +11:00
|
|
|
pub fn to_string_ident(&self) -> Ident<String>
|
|
|
|
where
|
|
|
|
S: AsRef<str>,
|
|
|
|
{
|
|
|
|
Ident {
|
|
|
|
string: self.as_str().to_owned(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 01:10:42 +11:00
|
|
|
pub fn into_inner(self) -> S {
|
|
|
|
self.string
|
|
|
|
}
|
|
|
|
|
2023-03-26 12:44:45 +11:00
|
|
|
/// Returns the namespace part of this resource identifier (the part before
|
|
|
|
/// the colon).
|
|
|
|
pub fn namespace(&self) -> &str
|
2022-10-11 19:10:49 +11:00
|
|
|
where
|
2023-03-26 12:44:45 +11:00
|
|
|
S: AsRef<str>,
|
2022-10-11 19:10:49 +11:00
|
|
|
{
|
2023-03-26 12:44:45 +11:00
|
|
|
self.namespace_and_path().0
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the path part of this resource identifier (the part after the
|
|
|
|
/// colon).
|
|
|
|
pub fn path(&self) -> &str
|
|
|
|
where
|
|
|
|
S: AsRef<str>,
|
|
|
|
{
|
|
|
|
self.namespace_and_path().1
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn namespace_and_path(&self) -> (&str, &str)
|
|
|
|
where
|
|
|
|
S: AsRef<str>,
|
|
|
|
{
|
|
|
|
self.as_str()
|
|
|
|
.split_once(':')
|
|
|
|
.expect("invalid resource identifier")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-02 18:35:35 +10:00
|
|
|
impl<'a> Ident<Cow<'a, str>> {
|
|
|
|
pub fn borrowed(&self) -> Ident<Cow<str>> {
|
|
|
|
Ident::new_unchecked(Cow::Borrowed(self.as_str()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-26 12:44:45 +11:00
|
|
|
fn parse(string: Cow<str>) -> Result<Ident<Cow<str>>, IdentError> {
|
|
|
|
let check_namespace = |s: &str| {
|
|
|
|
!s.is_empty()
|
|
|
|
&& s.chars()
|
|
|
|
.all(|c| matches!(c, 'a'..='z' | '0'..='9' | '_' | '.' | '-'))
|
|
|
|
};
|
|
|
|
|
|
|
|
let check_path = |s: &str| {
|
|
|
|
!s.is_empty()
|
|
|
|
&& s.chars()
|
|
|
|
.all(|c| matches!(c, 'a'..='z' | '0'..='9' | '_' | '.' | '-' | '/'))
|
|
|
|
};
|
|
|
|
|
|
|
|
match string.split_once(':') {
|
|
|
|
Some((namespace, path)) if check_namespace(namespace) && check_path(path) => {
|
|
|
|
Ok(Ident { string })
|
2022-10-12 21:53:59 +11:00
|
|
|
}
|
2023-03-26 12:44:45 +11:00
|
|
|
None if check_path(&string) => Ok(Ident {
|
|
|
|
string: format!("minecraft:{string}").into(),
|
|
|
|
}),
|
|
|
|
_ => Err(IdentError(string.into())),
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-26 12:44:45 +11:00
|
|
|
impl<S: AsRef<str>> AsRef<str> for Ident<S> {
|
|
|
|
fn as_ref(&self) -> &str {
|
|
|
|
self.string.as_ref()
|
2022-12-17 03:23:48 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-26 12:44:45 +11:00
|
|
|
impl<S> AsRef<S> for Ident<S> {
|
|
|
|
fn as_ref(&self) -> &S {
|
|
|
|
&self.string
|
2022-11-29 22:37:32 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-26 12:44:45 +11:00
|
|
|
impl<S: Borrow<str>> Borrow<str> for Ident<S> {
|
|
|
|
fn borrow(&self) -> &str {
|
|
|
|
self.string.borrow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-01 08:58:47 +11:00
|
|
|
impl From<Ident<&str>> for String {
|
|
|
|
fn from(value: Ident<&str>) -> Self {
|
|
|
|
value.as_str().to_owned()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-26 12:44:45 +11:00
|
|
|
impl From<Ident<String>> for String {
|
|
|
|
fn from(value: Ident<String>) -> Self {
|
|
|
|
value.into_inner()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<Ident<Cow<'a, str>>> for Cow<'a, str> {
|
|
|
|
fn from(value: Ident<Cow<'a, str>>) -> Self {
|
|
|
|
value.into_inner()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<Ident<Cow<'a, str>>> for Ident<String> {
|
|
|
|
fn from(value: Ident<Cow<'a, str>>) -> Self {
|
|
|
|
Self {
|
|
|
|
string: value.string.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<Ident<String>> for Ident<Cow<'a, str>> {
|
|
|
|
fn from(value: Ident<String>) -> Self {
|
|
|
|
Self {
|
2022-11-29 22:37:32 +11:00
|
|
|
string: value.string.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<Ident<&'a str>> for Ident<Cow<'a, str>> {
|
|
|
|
fn from(value: Ident<&'a str>) -> Self {
|
|
|
|
Ident {
|
2023-03-26 12:44:45 +11:00
|
|
|
string: value.string.into(),
|
2022-11-29 22:37:32 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-26 12:44:45 +11:00
|
|
|
impl<'a> From<Ident<&'a str>> for Ident<String> {
|
|
|
|
fn from(value: Ident<&'a str>) -> Self {
|
2022-11-29 22:37:32 +11:00
|
|
|
Ident {
|
2023-03-26 12:44:45 +11:00
|
|
|
string: value.string.into(),
|
2022-11-29 22:37:32 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-12 21:53:59 +11:00
|
|
|
impl FromStr for Ident<String> {
|
2023-03-26 12:44:45 +11:00
|
|
|
type Err = IdentError;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
Ok(Ident::new(s)?.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for Ident<Cow<'static, str>> {
|
|
|
|
type Err = IdentError;
|
2022-04-15 07:55:45 +10:00
|
|
|
|
2022-10-12 21:53:59 +11:00
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
2023-03-26 12:44:45 +11:00
|
|
|
Ident::<String>::try_from(s).map(From::from)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> TryFrom<&'a str> for Ident<String> {
|
|
|
|
type Error = IdentError;
|
|
|
|
|
|
|
|
fn try_from(value: &'a str) -> Result<Self, Self::Error> {
|
|
|
|
Ok(Ident::new(value)?.into())
|
2022-10-11 19:10:49 +11:00
|
|
|
}
|
|
|
|
}
|
2022-04-15 07:55:45 +10:00
|
|
|
|
2022-10-12 21:53:59 +11:00
|
|
|
impl TryFrom<String> for Ident<String> {
|
2023-03-26 12:44:45 +11:00
|
|
|
type Error = IdentError;
|
2022-10-12 21:53:59 +11:00
|
|
|
|
|
|
|
fn try_from(value: String) -> Result<Self, Self::Error> {
|
2023-03-26 12:44:45 +11:00
|
|
|
Ok(Ident::new(value)?.into())
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-26 12:44:45 +11:00
|
|
|
impl<'a> TryFrom<Cow<'a, str>> for Ident<String> {
|
|
|
|
type Error = IdentError;
|
|
|
|
|
|
|
|
fn try_from(value: Cow<'a, str>) -> Result<Self, Self::Error> {
|
|
|
|
Ok(Ident::new(value)?.into())
|
2022-10-11 19:10:49 +11:00
|
|
|
}
|
|
|
|
}
|
2022-04-15 07:55:45 +10:00
|
|
|
|
2023-03-26 12:44:45 +11:00
|
|
|
impl<'a> TryFrom<&'a str> for Ident<Cow<'a, str>> {
|
|
|
|
type Error = IdentError;
|
|
|
|
|
|
|
|
fn try_from(value: &'a str) -> Result<Self, Self::Error> {
|
|
|
|
Self::new(value)
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-26 12:44:45 +11:00
|
|
|
impl<'a> TryFrom<String> for Ident<Cow<'a, str>> {
|
|
|
|
type Error = IdentError;
|
|
|
|
|
|
|
|
fn try_from(value: String) -> Result<Self, Self::Error> {
|
|
|
|
Self::new(value)
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-26 12:44:45 +11:00
|
|
|
impl<'a> TryFrom<Cow<'a, str>> for Ident<Cow<'a, str>> {
|
|
|
|
type Error = IdentError;
|
2022-10-11 19:10:49 +11:00
|
|
|
|
2023-03-26 12:44:45 +11:00
|
|
|
fn try_from(value: Cow<'a, str>) -> Result<Self, Self::Error> {
|
|
|
|
Self::new(value)
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-26 12:44:45 +11:00
|
|
|
impl<S: fmt::Debug> fmt::Debug for Ident<S> {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
self.string.fmt(f)
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-26 12:44:45 +11:00
|
|
|
impl<S: fmt::Display> fmt::Display for Ident<S> {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
self.string.fmt(f)
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-26 12:44:45 +11:00
|
|
|
impl<S, T> PartialEq<Ident<T>> for Ident<S>
|
2022-10-12 21:53:59 +11:00
|
|
|
where
|
2023-03-26 12:44:45 +11:00
|
|
|
S: PartialEq<T>,
|
2022-10-12 21:53:59 +11:00
|
|
|
{
|
2023-03-26 12:44:45 +11:00
|
|
|
fn eq(&self, other: &Ident<T>) -> bool {
|
|
|
|
self.string == other.string
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-26 12:44:45 +11:00
|
|
|
impl<S, T> PartialOrd<Ident<T>> for Ident<S>
|
2022-10-12 21:53:59 +11:00
|
|
|
where
|
2023-03-26 12:44:45 +11:00
|
|
|
S: PartialOrd<T>,
|
2022-10-12 21:53:59 +11:00
|
|
|
{
|
2023-03-26 12:44:45 +11:00
|
|
|
fn partial_cmp(&self, other: &Ident<T>) -> Option<Ordering> {
|
|
|
|
self.string.partial_cmp(&other.string)
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-12 21:53:59 +11:00
|
|
|
impl<S: Encode> Encode for Ident<S> {
|
2022-11-14 01:10:42 +11:00
|
|
|
fn encode(&self, w: impl Write) -> anyhow::Result<()> {
|
2023-03-26 12:44:45 +11:00
|
|
|
self.as_ref().encode(w)
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 01:10:42 +11:00
|
|
|
impl<'a, S> Decode<'a> for Ident<S>
|
2022-10-12 21:53:59 +11:00
|
|
|
where
|
2023-03-26 12:44:45 +11:00
|
|
|
S: Decode<'a>,
|
|
|
|
Ident<S>: TryFrom<S, Error = IdentError>,
|
2022-10-12 21:53:59 +11:00
|
|
|
{
|
2022-11-14 01:10:42 +11:00
|
|
|
fn decode(r: &mut &'a [u8]) -> anyhow::Result<Self> {
|
2023-03-26 12:44:45 +11:00
|
|
|
Ok(Ident::try_from(S::decode(r)?)?)
|
2022-10-11 19:10:49 +11:00
|
|
|
}
|
2022-10-12 21:53:59 +11:00
|
|
|
}
|
2022-10-11 19:10:49 +11:00
|
|
|
|
Reorganize Project (#321)
## Description
- `valence` and `valence_protocol` have been divided into smaller crates
in order to parallelize the build and improve IDE responsiveness. In the
process, code architecture has been made clearer by removing circular
dependencies between modules. `valence` is now just a shell around the
other crates.
- `workspace.packages` and `workspace.dependencies` are now used. This
makes dependency managements and crate configuration much easier.
- `valence_protocol` is no more. Most things from `valence_protocol`
ended up in `valence_core`. We won't advertise `valence_core` as a
general-purpose protocol library since it contains too much
valence-specific stuff. Closes #308.
- Networking code (login, initial TCP connection handling, etc.) has
been extracted into the `valence_network` crate. The API has been
expanded and improved with better defaults. Player counts and initial
connections to the server are now tracked separately. Player counts
function by default without any user configuration.
- Some crates like `valence_anvil`, `valence_network`,
`valence_player_list`, `valence_inventory`, etc. are now optional. They
can be enabled/disabled with feature flags and `DefaultPlugins` just
like bevy.
- Whole-server unit tests have been moved to `valence/src/tests` in
order to avoid [cyclic
dev-dependencies](https://github.com/rust-lang/cargo/issues/4242).
- Tools like `valence_stresser` and `packet_inspector` have been moved
to a new `tools` directory. Renamed `valence_stresser` to `stresser`.
Closes #241.
- Moved all benches to `valence/benches/` to make them easier to run and
organize.
Ignoring transitive dependencies and `valence_core`, here's what the
dependency graph looks like now:
```mermaid
graph TD
network --> client
client --> instance
biome --> registry
dimension --> registry
instance --> biome
instance --> dimension
instance --> entity
player_list --> client
inventory --> client
anvil --> instance
entity --> block
```
### Issues
- Inventory tests inspect many private implementation details of the
inventory module, forcing us to mark things as `pub` and
`#[doc(hidden)]`. It would be ideal if the tests only looked at
observable behavior.
- Consider moving packets in `valence_core` elsewhere. `Particle` wants
to use `BlockState`, but that's defined in `valence_block`, so we can't
use it without causing cycles.
- Unsure what exactly should go in `valence::prelude`.
- This could use some more tests of course, but I'm holding off on that
until I'm confident this is the direction we want to take things.
## TODOs
- [x] Update examples.
- [x] Update benches.
- [x] Update main README.
- [x] Add short READMEs to crates.
- [x] Test new schedule to ensure behavior is the same.
- [x] Update tools.
- [x] Copy lints to all crates.
- [x] Fix docs, clippy, etc.
2023-04-22 07:43:59 +10:00
|
|
|
impl<S> From<Ident<S>> for valence_nbt::Value
|
2022-10-12 21:53:59 +11:00
|
|
|
where
|
Reorganize Project (#321)
## Description
- `valence` and `valence_protocol` have been divided into smaller crates
in order to parallelize the build and improve IDE responsiveness. In the
process, code architecture has been made clearer by removing circular
dependencies between modules. `valence` is now just a shell around the
other crates.
- `workspace.packages` and `workspace.dependencies` are now used. This
makes dependency managements and crate configuration much easier.
- `valence_protocol` is no more. Most things from `valence_protocol`
ended up in `valence_core`. We won't advertise `valence_core` as a
general-purpose protocol library since it contains too much
valence-specific stuff. Closes #308.
- Networking code (login, initial TCP connection handling, etc.) has
been extracted into the `valence_network` crate. The API has been
expanded and improved with better defaults. Player counts and initial
connections to the server are now tracked separately. Player counts
function by default without any user configuration.
- Some crates like `valence_anvil`, `valence_network`,
`valence_player_list`, `valence_inventory`, etc. are now optional. They
can be enabled/disabled with feature flags and `DefaultPlugins` just
like bevy.
- Whole-server unit tests have been moved to `valence/src/tests` in
order to avoid [cyclic
dev-dependencies](https://github.com/rust-lang/cargo/issues/4242).
- Tools like `valence_stresser` and `packet_inspector` have been moved
to a new `tools` directory. Renamed `valence_stresser` to `stresser`.
Closes #241.
- Moved all benches to `valence/benches/` to make them easier to run and
organize.
Ignoring transitive dependencies and `valence_core`, here's what the
dependency graph looks like now:
```mermaid
graph TD
network --> client
client --> instance
biome --> registry
dimension --> registry
instance --> biome
instance --> dimension
instance --> entity
player_list --> client
inventory --> client
anvil --> instance
entity --> block
```
### Issues
- Inventory tests inspect many private implementation details of the
inventory module, forcing us to mark things as `pub` and
`#[doc(hidden)]`. It would be ideal if the tests only looked at
observable behavior.
- Consider moving packets in `valence_core` elsewhere. `Particle` wants
to use `BlockState`, but that's defined in `valence_block`, so we can't
use it without causing cycles.
- Unsure what exactly should go in `valence::prelude`.
- This could use some more tests of course, but I'm holding off on that
until I'm confident this is the direction we want to take things.
## TODOs
- [x] Update examples.
- [x] Update benches.
- [x] Update main README.
- [x] Add short READMEs to crates.
- [x] Test new schedule to ensure behavior is the same.
- [x] Update tools.
- [x] Copy lints to all crates.
- [x] Fix docs, clippy, etc.
2023-04-22 07:43:59 +10:00
|
|
|
S: Into<valence_nbt::Value>,
|
2022-10-12 21:53:59 +11:00
|
|
|
{
|
2023-03-26 12:44:45 +11:00
|
|
|
fn from(value: Ident<S>) -> Self {
|
|
|
|
value.into_inner().into()
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-26 12:44:45 +11:00
|
|
|
impl<T: Serialize> Serialize for Ident<T> {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
self.string.serialize(serializer)
|
2022-10-14 16:28:12 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-26 12:44:45 +11:00
|
|
|
impl<'de, S> Deserialize<'de> for Ident<S>
|
2022-10-14 16:28:12 +11:00
|
|
|
where
|
2023-03-26 12:44:45 +11:00
|
|
|
S: Deserialize<'de>,
|
|
|
|
Ident<S>: TryFrom<S, Error = IdentError>,
|
2022-10-14 16:28:12 +11:00
|
|
|
{
|
2023-03-26 12:44:45 +11:00
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
Ident::try_from(S::deserialize(deserializer)?).map_err(D::Error::custom)
|
2022-10-14 16:28:12 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 07:55:45 +10:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2023-04-01 08:58:47 +11:00
|
|
|
use super::*;
|
|
|
|
|
2022-10-12 08:05:00 +11:00
|
|
|
#[test]
|
|
|
|
fn check_namespace_and_path() {
|
|
|
|
let id = ident!("namespace:path");
|
|
|
|
assert_eq!(id.namespace(), "namespace");
|
|
|
|
assert_eq!(id.path(), "path");
|
|
|
|
}
|
|
|
|
|
2022-04-15 07:55:45 +10:00
|
|
|
#[test]
|
|
|
|
fn parse_valid() {
|
|
|
|
ident!("minecraft:whatever");
|
|
|
|
ident!("_what-ever55_:.whatever/whatever123456789_");
|
2022-10-11 19:10:49 +11:00
|
|
|
ident!("valence:frobnicator");
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn parse_invalid_0() {
|
2023-04-01 08:58:47 +11:00
|
|
|
Ident::new("").unwrap();
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn parse_invalid_1() {
|
2023-04-01 08:58:47 +11:00
|
|
|
Ident::new(":").unwrap();
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn parse_invalid_2() {
|
2023-04-01 08:58:47 +11:00
|
|
|
Ident::new("foo:bar:baz").unwrap();
|
2022-04-15 07:55:45 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn equality() {
|
|
|
|
assert_eq!(ident!("minecraft:my.identifier"), ident!("my.identifier"));
|
|
|
|
}
|
|
|
|
}
|