Skip to content

Commit 4e45d36

Browse files
committed
refactor: remove autocomplete interaction parsing
1 parent acab260 commit 4e45d36

File tree

5 files changed

+21
-49
lines changed

5 files changed

+21
-49
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Changed
1010
- Upgraded to `twilight-model` 0.10.0
1111

12+
### Removed
13+
- Autocomplete interactions parsing has been removed. See [#9](https://github.com/baptiste0928/twilight-interactions/issues/9).
14+
1215
## [0.9.1] - 2022-03-09
1316
### Changed
1417
- Implemented `Attachment` command option type. (by @Lyssieth)

twilight-interactions-derive/src/command/model/command_model.rs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@ use proc_macro2::TokenStream;
22
use quote::{quote, quote_spanned};
33
use syn::{DeriveInput, FieldsNamed, Result};
44

5-
use crate::{
6-
command::model::parse::{channel_type, command_option_value},
7-
parse::find_attr,
8-
};
5+
use crate::command::model::parse::{channel_type, command_option_value};
96

10-
use super::parse::{FieldType, StructField, TypeAttribute};
7+
use super::parse::{FieldType, StructField};
118

129
/// Implementation of CommandModel derive macro
1310
pub fn impl_command_model(input: DeriveInput, fields: Option<FieldsNamed>) -> Result<TokenStream> {
@@ -17,12 +14,7 @@ pub fn impl_command_model(input: DeriveInput, fields: Option<FieldsNamed>) -> Re
1714
None => Vec::new(),
1815
};
1916

20-
let partial = match find_attr(&input.attrs, "command") {
21-
Some(attr) => TypeAttribute::parse(attr)?.partial,
22-
None => false,
23-
};
24-
25-
let field_unknown = field_unknown(partial);
17+
let field_unknown = field_unknown();
2618
let fields_init = fields.iter().map(field_init);
2719
let fields_match_arms = fields.iter().map(field_match_arm);
2820
let fields_constructor = fields.iter().map(field_constructor);
@@ -108,18 +100,14 @@ fn field_constructor(field: &StructField) -> TokenStream {
108100
}
109101

110102
/// Generate unknown field match arm
111-
fn field_unknown(partial: bool) -> TokenStream {
112-
if partial {
113-
quote!(continue)
114-
} else {
115-
quote! {
116-
return ::std::result::Result::Err(
117-
::twilight_interactions::error::ParseError::Option(
118-
::twilight_interactions::error::ParseOptionError {
119-
field: ::std::convert::From::from(other),
120-
kind: ::twilight_interactions::error::ParseOptionErrorType::UnknownField,
121-
})
122-
)
123-
}
103+
fn field_unknown() -> TokenStream {
104+
quote! {
105+
return ::std::result::Result::Err(
106+
::twilight_interactions::error::ParseError::Option(
107+
::twilight_interactions::error::ParseOptionError {
108+
field: ::std::convert::From::from(other),
109+
kind: ::twilight_interactions::error::ParseOptionErrorType::UnknownField,
110+
})
111+
)
124112
}
125113
}

twilight-interactions-derive/src/command/model/create_command.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@ pub fn impl_create_command(input: DeriveInput, fields: Option<FieldsNamed>) -> R
2828
}
2929
};
3030

31-
if attributes.partial {
32-
return Err(Error::new(
33-
attr_span,
34-
"Cannot implement `CreateCommand` on partial model",
35-
));
36-
}
37-
3831
let name = match &attributes.name {
3932
Some(name) => name,
4033
None => return Err(Error::new(attr_span, "Missing required attribute `name`")),

twilight-interactions-derive/src/command/model/parse.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ pub struct TypeAttribute {
6666
pub name: Option<String>,
6767
/// Overwrite the field description
6868
pub desc: Option<String>,
69-
/// Whether the type is partial
70-
pub partial: bool,
7169
/// Whether the command should be enabled by default.
7270
pub default_permission: bool,
7371
}
@@ -76,15 +74,10 @@ impl TypeAttribute {
7674
/// Parse a single [`Attribute`]
7775
pub fn parse(attr: &Attribute) -> Result<Self> {
7876
let meta = attr.parse_meta()?;
79-
let attrs = NamedAttrs::parse(meta, &["name", "desc", "partial", "default_permission"])?;
77+
let attrs = NamedAttrs::parse(meta, &["name", "desc", "default_permission"])?;
8078

8179
let name = attrs.get("name").map(parse_name).transpose()?;
8280
let desc = attrs.get("desc").map(parse_desc).transpose()?;
83-
let partial = attrs
84-
.get("partial")
85-
.map(|v| v.parse_bool())
86-
.transpose()?
87-
.unwrap_or(false);
8881
let default_permission = attrs
8982
.get("default_permission")
9083
.map(|v| v.parse_bool())
@@ -94,7 +87,6 @@ impl TypeAttribute {
9487
Ok(Self {
9588
name,
9689
desc,
97-
partial,
9890
default_permission,
9991
})
10092
}
@@ -107,7 +99,7 @@ pub struct FieldAttribute {
10799
pub rename: Option<String>,
108100
/// Overwrite the field description
109101
pub desc: Option<String>,
110-
/// Whether the command supports autocomplete
102+
/// Whether the field supports autocomplete
111103
pub autocomplete: bool,
112104
/// Limit to specific channel types
113105
pub channel_types: Vec<ChannelType>,

twilight-interactions/src/command/command_model.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,9 @@ use super::internal::CommandOptionData;
5858
/// that can be initialized from the command model.
5959
///
6060
/// ### Autocomplete interactions
61-
/// When receiving an autocomplete interaction, you sometimes only care
62-
/// about a subset of received fields. You can use the
63-
/// `#[command(partial = true)]` attribute to ignore errors related to
64-
/// unknown fields. The [`CreateCommand`] trait cannot be applied on a
65-
/// partial model.
61+
/// Autocomplete interactions are no longer supported since 0.10, as the previous
62+
/// implementation was incorrect. See [#9](https://github.com/baptiste0928/twilight-interactions/issues/9)
63+
/// for more information.
6664
///
6765
/// ## Subcommands and subcommands groups
6866
/// This trait also support parsing subcommands and subcommands group when
@@ -101,18 +99,16 @@ use super::internal::CommandOptionData;
10199
///
102100
/// | Attribute | Type | Location | Description |
103101
/// |--------------------------|----------------|----------------------|-----------------------------------------------------------------|
104-
/// | `partial` | `bool` | Type | Ignore unknown fields when parsing. |
105102
/// | `name` | `str` | Variant (subcommand) | Subcommand name (required). |
106103
/// | `rename` | `str` | Field | Use a different name for the field when parsing. |
107104
/// | `channel_types` | `str` | Field | Restricts the channel choice to specific types.[^channel_types] |
108105
/// | `max_value`, `min_value` | `i64` or `f64` | Field | Maximum and/or minimum value permitted. |
109106
///
110107
/// ### Example
111108
/// ```
112-
/// use twilight_interactions::command::{CommandModel, ResolvedUser};
109+
/// use twilight_interactions::command::CommandModel;
113110
///
114111
/// #[derive(CommandModel)]
115-
/// #[command(partial = true)]
116112
/// struct HelloCommand {
117113
/// #[command(rename = "text")]
118114
/// message: String,

0 commit comments

Comments
 (0)