Skip to content

Commit

Permalink
fix doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
JieningYu committed Mar 7, 2024
1 parent 00b24cc commit e861ced
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 56 deletions.
3 changes: 1 addition & 2 deletions crates/util/identifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ serde = { version = "1.0", optional = true }
rimecraft-edcode = { path = "../edcode", optional = true }

[features]
default = ["vanilla", "macros"]
default = ["vanilla"]
serde = ["dep:serde"]
edcode = ["dep:rimecraft-edcode"]
vanilla = []
macros = []

[lints]
workspace = true
3 changes: 0 additions & 3 deletions crates/util/identifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ use std::{fmt::Display, str::FromStr};
#[cfg(feature = "vanilla")]
pub mod vanilla;

#[cfg(feature = "macros")]
pub mod macros;

/// An identifier used to identify things.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[doc(alias = "ResourceLocation")]
Expand Down
60 changes: 26 additions & 34 deletions crates/util/identifier/src/vanilla.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::{hash::Hash, str::FromStr, sync::Arc};

use crate::Separate;

mod macros;

/// Namespace of an vanilla Minecraft `Identifier`.
///
/// This is the default value of a [`Namespace`].
Expand Down Expand Up @@ -106,19 +108,19 @@ impl Path {
Ok(Self(ArcCowStr::Arc(value)))
}


/// Creates a new [`Path`] from the given value.
///
/// This function accepts a 2-dimension [`Vec`] which stands for words wrapped in locations.
///
/// # Examples
///
/// ```
/// # use rimecraft_identifier::vanilla::Path;
/// let path = Path::new_formatted(vec![
/// vec!["tags"],
/// vec![],
/// vec!["piglin", "", "likes"],
/// ]);
/// vec!["tags"],
/// vec![],
/// vec!["piglin", "", "likes"],
/// ]);
/// let identifier = Identifier::new(MINECRAFT, path);
///
/// assert_eq!("minecraft:tags/piglin_likes", identifier.to_string());
Expand All @@ -132,8 +134,8 @@ impl Path {
where
T: Into<Arc<str>>,
{
Self::try_new_formatted(values).unwrap()
}
Self::try_new_formatted(values).unwrap()
}

/// Creates a new [`Path`] from the given value.
///
Expand All @@ -142,11 +144,12 @@ impl Path {
/// # Examples
///
/// ```
/// # use rimecraft_identifier::vanilla::Path;
/// let path = Path::try_new_formatted(vec![
/// vec!["tags"],
/// vec![],
/// vec!["piglin", "", "repellents"],
/// ]).unwrap();
/// vec!["tags"],
/// vec![],
/// vec!["piglin", "", "repellents"],
/// ]).unwrap();
/// let identifier = Identifier::new(MINECRAFT, path);
/// assert_eq!("minecraft:tags/piglin_repellents", identifier.to_string());
/// ```
Expand Down Expand Up @@ -174,36 +177,24 @@ impl Path {
})
.collect();

match values.iter().flat_map(|v| v.iter()).find_map(|r| match r {
Ok(_) => None,
Err(error) => Some(error),
}) {
Some(error) => return Err((*error).clone()),
None => (),
};
if let Some(err) = values
.iter()
.flat_map(|v| v.iter())
.find_map(|r| r.as_ref().err())
.cloned()
{
return Err(err);
}

let values: Vec<Vec<Arc<str>>> = values
.into_iter()
.filter_map(|v| {
let v: Vec<Arc<str>> = v
.into_iter()
.filter_map(|r| match r {
Ok(s) => {
if s.len() > 0 {
Some(s)
} else {
None
}
}
Err(_) => None,
})
.filter_map(|r| r.ok().and_then(|s| (!s.is_empty()).then_some(s)))
.collect();

if v.len() > 0 {
Some(v)
} else {
None
}
(!v.is_empty()).then_some(v)
})
.collect();

Expand All @@ -219,7 +210,7 @@ impl Path {
/// Creates a new [`Path`] from the given value
/// at compile time.
///
/// The given path shoule be all [a-z0-9/_.-] character.
/// The given path should be all [a-z0-9/_.-] character.
#[inline]
pub const fn new_unchecked(value: &'static str) -> Self {
Self(ArcCowStr::Ref(value))
Expand Down Expand Up @@ -339,6 +330,7 @@ fn validate_path(value: &str) -> Result<(), Error> {

/// Error type for `Namespace` and `Path`.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum Error {
/// The given namespace is invalid.
InvalidNamespace(String),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ use crate::*; // Used in docs
/// # Examples
///
/// ```
/// # use rimecraft_identifier::{*, vanilla::*};
/// let identifier = format_identifier!("namespace".parse().unwrap() =>
/// "a", "b"; "c"; "42"
/// "a", "b"; "c"; "42"
/// );
/// assert_eq!("namespace:a_b/c/42", identifier.to_string());
///
/// let identifier = format_identifier!(MINECRAFT =>
/// "tags"; "piglin", "repellents"
/// "tags"; "piglin", "repellents"
/// );
/// assert_eq!("minecraft:tags/piglin_repellents", identifier.to_string());
/// ```
Expand All @@ -23,21 +24,13 @@ use crate::*; // Used in docs
macro_rules! format_identifier {
($namespace:expr => $($($word:expr),*);*) => {
{
let mut locations = Vec::new();

$(
{
let mut words = Vec::new();

$(
words.push($word);
)*

locations.push(words);
}
)*

Identifier::<Namespace, Path>::new($namespace, Path::new_formatted(locations))
$crate::Identifier::<$crate::vanilla::Namespace, $crate::vanilla::Path>::new(
$namespace,
$crate::vanilla::Path::new_formatted(
::std::vec![
$(::std::vec![$($word),*]),*
])
)
}
};
}

0 comments on commit e861ced

Please sign in to comment.