aboutsummaryrefslogtreecommitdiff
path: root/azalea-brigadier/src/suggestion
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2023-10-12 22:01:15 -0500
committermat <git@matdoes.dev>2023-10-12 22:01:15 -0500
commit79ad1e93bf6ce2b7c2da6925a7c85b33bb76f154 (patch)
tree19004abf51cb19eb3b5cc13c61d670889cb228a0 /azalea-brigadier/src/suggestion
parentf505ace721d4c214cd32143febd0a066b6b95ce5 (diff)
downloadazalea-drasl-79ad1e93bf6ce2b7c2da6925a7c85b33bb76f154.tar.xz
brigadier suggestions
closes #109
Diffstat (limited to 'azalea-brigadier/src/suggestion')
-rwxr-xr-xazalea-brigadier/src/suggestion/mod.rs24
-rwxr-xr-xazalea-brigadier/src/suggestion/suggestions.rs36
-rwxr-xr-xazalea-brigadier/src/suggestion/suggestions_builder.rs24
3 files changed, 32 insertions, 52 deletions
diff --git a/azalea-brigadier/src/suggestion/mod.rs b/azalea-brigadier/src/suggestion/mod.rs
index bc0e7608..fbebfe8a 100755
--- a/azalea-brigadier/src/suggestion/mod.rs
+++ b/azalea-brigadier/src/suggestion/mod.rs
@@ -20,13 +20,10 @@ pub use suggestions_builder::SuggestionsBuilder;
/// The `M` generic is the type of the tooltip, so for example a `String` or
/// just `()` if you don't care about it.
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
-pub struct Suggestion<M = ()>
-where
- M: Clone,
-{
+pub struct Suggestion {
pub range: StringRange,
value: SuggestionValue,
- pub tooltip: Option<M>,
+ pub tooltip: Option<String>,
}
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
@@ -35,18 +32,16 @@ pub enum SuggestionValue {
Text(String),
}
-impl Suggestion<()> {
- pub fn new(range: StringRange, text: &str) -> Suggestion<()> {
+impl Suggestion {
+ pub fn new(range: StringRange, text: &str) -> Suggestion {
Suggestion {
range,
value: SuggestionValue::Text(text.to_string()),
tooltip: None,
}
}
-}
-impl<M: Clone> Suggestion<M> {
- pub fn new_with_tooltip(range: StringRange, text: &str, tooltip: M) -> Self {
+ pub fn new_with_tooltip(range: StringRange, text: &str, tooltip: String) -> Self {
Self {
range,
value: SuggestionValue::Text(text.to_string()),
@@ -71,7 +66,7 @@ impl<M: Clone> Suggestion<M> {
result
}
- pub fn expand(&self, command: &str, range: StringRange) -> Suggestion<M> {
+ pub fn expand(&self, command: &str, range: StringRange) -> Suggestion {
if range == self.range {
return self.clone();
}
@@ -140,10 +135,13 @@ impl PartialOrd for SuggestionValue {
}
#[cfg(feature = "azalea-buf")]
-impl McBufWritable for Suggestion<FormattedText> {
+impl McBufWritable for Suggestion {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
self.value.to_string().write_into(buf)?;
- self.tooltip.write_into(buf)?;
+ self.tooltip
+ .clone()
+ .map(FormattedText::from)
+ .write_into(buf)?;
Ok(())
}
}
diff --git a/azalea-brigadier/src/suggestion/suggestions.rs b/azalea-brigadier/src/suggestion/suggestions.rs
index a5119d88..487e4233 100755
--- a/azalea-brigadier/src/suggestion/suggestions.rs
+++ b/azalea-brigadier/src/suggestion/suggestions.rs
@@ -12,21 +12,18 @@ use azalea_chat::FormattedText;
use std::io::{Cursor, Write};
use std::{collections::HashSet, hash::Hash};
-#[derive(Debug, Clone, Eq, PartialEq, Hash)]
-pub struct Suggestions<M>
-where
- M: Clone + PartialEq + Hash,
-{
+#[derive(Debug, Clone, Eq, PartialEq, Hash, Default)]
+pub struct Suggestions {
range: StringRange,
- suggestions: Vec<Suggestion<M>>,
+ suggestions: Vec<Suggestion>,
}
-impl<M: Clone + Eq + Hash> Suggestions<M> {
- pub fn new(range: StringRange, suggestions: Vec<Suggestion<M>>) -> Self {
+impl Suggestions {
+ pub fn new(range: StringRange, suggestions: Vec<Suggestion>) -> Self {
Self { range, suggestions }
}
- pub fn merge(command: &str, input: &[Suggestions<M>]) -> Self {
+ pub fn merge(command: &str, input: &[Suggestions]) -> Self {
if input.is_empty() {
return Suggestions::default();
} else if input.len() == 1 {
@@ -41,7 +38,7 @@ impl<M: Clone + Eq + Hash> Suggestions<M> {
Suggestions::create(command, &texts)
}
- pub fn create(command: &str, suggestions: &HashSet<Suggestion<M>>) -> Self {
+ pub fn create(command: &str, suggestions: &HashSet<Suggestion>) -> Self {
if suggestions.is_empty() {
return Suggestions::default();
};
@@ -70,7 +67,7 @@ impl<M: Clone + Eq + Hash> Suggestions<M> {
self.suggestions.is_empty()
}
- pub fn list(&self) -> &[Suggestion<M>] {
+ pub fn list(&self) -> &[Suggestion] {
&self.suggestions
}
@@ -79,19 +76,8 @@ impl<M: Clone + Eq + Hash> Suggestions<M> {
}
}
-// this can't be derived because that'd require the generic to have `Default`
-// too even if it's not actually necessary
-impl<M: Clone + Hash + Eq> Default for Suggestions<M> {
- fn default() -> Self {
- Self {
- range: StringRange::default(),
- suggestions: Vec::new(),
- }
- }
-}
-
#[cfg(feature = "azalea-buf")]
-impl McBufReadable for Suggestions<FormattedText> {
+impl McBufReadable for Suggestions {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
#[derive(McBuf)]
struct StandaloneSuggestion {
@@ -109,7 +95,7 @@ impl McBufReadable for Suggestions<FormattedText> {
.into_iter()
.map(|s| Suggestion {
value: SuggestionValue::Text(s.text),
- tooltip: s.tooltip,
+ tooltip: s.tooltip.map(|t| t.to_string()),
range,
})
.collect::<Vec<_>>();
@@ -120,7 +106,7 @@ impl McBufReadable for Suggestions<FormattedText> {
}
#[cfg(feature = "azalea-buf")]
-impl McBufWritable for Suggestions<FormattedText> {
+impl McBufWritable for Suggestions {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
(self.range.start() as u32).var_write_into(buf)?;
(self.range.length() as u32).var_write_into(buf)?;
diff --git a/azalea-brigadier/src/suggestion/suggestions_builder.rs b/azalea-brigadier/src/suggestion/suggestions_builder.rs
index 469a7b98..4e6296dd 100755
--- a/azalea-brigadier/src/suggestion/suggestions_builder.rs
+++ b/azalea-brigadier/src/suggestion/suggestions_builder.rs
@@ -1,24 +1,20 @@
use std::collections::HashSet;
-use std::hash::Hash;
use crate::context::StringRange;
use super::{Suggestion, SuggestionValue, Suggestions};
#[derive(PartialEq, Debug)]
-pub struct SuggestionsBuilder<M = ()>
-where
- M: Clone + Eq + Hash,
-{
+pub struct SuggestionsBuilder {
input: String,
input_lowercase: String,
start: usize,
remaining: String,
remaining_lowercase: String,
- result: HashSet<Suggestion<M>>,
+ result: HashSet<Suggestion>,
}
-impl SuggestionsBuilder<()> {
+impl SuggestionsBuilder {
pub fn new(input: &str, start: usize) -> Self {
Self::new_with_lowercase(input, input.to_lowercase().as_str(), start)
}
@@ -35,7 +31,7 @@ impl SuggestionsBuilder<()> {
}
}
-impl<M: Clone + Eq + Hash> SuggestionsBuilder<M> {
+impl SuggestionsBuilder {
pub fn input(&self) -> &str {
&self.input
}
@@ -52,7 +48,7 @@ impl<M: Clone + Eq + Hash> SuggestionsBuilder<M> {
&self.remaining_lowercase
}
- pub fn build(&self) -> Suggestions<M> {
+ pub fn build(&self) -> Suggestions {
Suggestions::create(&self.input, &self.result)
}
@@ -68,7 +64,7 @@ impl<M: Clone + Eq + Hash> SuggestionsBuilder<M> {
self
}
- pub fn suggest_with_tooltip(mut self, text: &str, tooltip: M) -> Self {
+ pub fn suggest_with_tooltip(mut self, text: &str, tooltip: String) -> Self {
if text == self.remaining {
return self;
}
@@ -89,7 +85,7 @@ impl<M: Clone + Eq + Hash> SuggestionsBuilder<M> {
self
}
- pub fn suggest_integer_with_tooltip(mut self, value: i32, tooltip: M) -> Self {
+ pub fn suggest_integer_with_tooltip(mut self, value: i32, tooltip: String) -> Self {
self.result.insert(Suggestion {
range: StringRange::between(self.start, self.input.len()),
value: SuggestionValue::Integer(value),
@@ -99,16 +95,16 @@ impl<M: Clone + Eq + Hash> SuggestionsBuilder<M> {
}
#[allow(clippy::should_implement_trait)]
- pub fn add(mut self, other: SuggestionsBuilder<M>) -> Self {
+ pub fn add(mut self, other: SuggestionsBuilder) -> Self {
self.result.extend(other.result);
self
}
- pub fn create_offset(&self, start: usize) -> SuggestionsBuilder<()> {
+ pub fn create_offset(&self, start: usize) -> SuggestionsBuilder {
SuggestionsBuilder::new_with_lowercase(&self.input, &self.input_lowercase, start)
}
- pub fn restart(&self) -> SuggestionsBuilder<()> {
+ pub fn restart(&self) -> SuggestionsBuilder {
self.create_offset(self.start)
}
}