Skip to content

Commit 558bcf4

Browse files
committed
generate try_into instead of into
1 parent d4b6cbe commit 558bcf4

File tree

3 files changed

+96
-72
lines changed

3 files changed

+96
-72
lines changed

crates/ide_assists/src/handlers/generate_enum_projection_method.rs

Lines changed: 66 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use crate::{
88
AssistContext, AssistId, AssistKind, Assists,
99
};
1010

11-
// Assist: generate_enum_into_method
11+
// Assist: generate_enum_try_into_method
1212
//
13-
// Generate an `into_` method for an enum variant.
13+
// Generate an `try_into_` method for an enum variant.
1414
//
1515
// ```
1616
// enum Value {
@@ -26,23 +26,29 @@ use crate::{
2626
// }
2727
//
2828
// impl Value {
29-
// fn into_text(self) -> Option<String> {
29+
// fn try_into_text(self) -> Result<String, Self> {
3030
// if let Self::Text(v) = self {
31-
// Some(v)
31+
// Ok(v)
3232
// } else {
33-
// None
33+
// Err(self)
3434
// }
3535
// }
3636
// }
3737
// ```
38-
pub(crate) fn generate_enum_into_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
38+
pub(crate) fn generate_enum_try_into_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
3939
generate_enum_projection_method(
4040
acc,
4141
ctx,
42-
"generate_enum_into_method",
43-
"Generate an `into_` method for an enum variant",
44-
"into",
45-
"",
42+
"generate_enum_try_into_method",
43+
"Generate an `try_into_` method for an enum variant",
44+
ProjectionProps {
45+
fn_name_prefix: "try_into",
46+
self_param: "self",
47+
return_prefix: "Result<",
48+
return_suffix: ", Self>",
49+
happy_case: "Ok",
50+
sad_case: "Err(self)",
51+
},
4652
)
4753
}
4854

@@ -79,18 +85,32 @@ pub(crate) fn generate_enum_as_method(acc: &mut Assists, ctx: &AssistContext) ->
7985
ctx,
8086
"generate_enum_as_method",
8187
"Generate an `as_` method for an enum variant",
82-
"as",
83-
"&",
88+
ProjectionProps {
89+
fn_name_prefix: "as",
90+
self_param: "&self",
91+
return_prefix: "Option<&",
92+
return_suffix: ">",
93+
happy_case: "Some",
94+
sad_case: "None",
95+
},
8496
)
8597
}
8698

87-
pub(crate) fn generate_enum_projection_method(
99+
struct ProjectionProps {
100+
fn_name_prefix: &'static str,
101+
self_param: &'static str,
102+
return_prefix: &'static str,
103+
return_suffix: &'static str,
104+
happy_case: &'static str,
105+
sad_case: &'static str,
106+
}
107+
108+
fn generate_enum_projection_method(
88109
acc: &mut Assists,
89110
ctx: &AssistContext,
90111
assist_id: &'static str,
91112
assist_description: &str,
92-
fn_name_prefix: &str,
93-
ref_prefix: &str,
113+
props: ProjectionProps,
94114
) -> Option<()> {
95115
let variant = ctx.find_node_at_offset::<ast::Variant>()?;
96116
let variant_name = variant.name()?;
@@ -112,7 +132,7 @@ pub(crate) fn generate_enum_projection_method(
112132
ast::StructKind::Unit => return None,
113133
};
114134

115-
let fn_name = format!("{}_{}", fn_name_prefix, &to_lower_snake_case(variant_name.text()));
135+
let fn_name = format!("{}_{}", props.fn_name_prefix, &to_lower_snake_case(variant_name.text()));
116136

117137
// Return early if we've found an existing new fn
118138
let impl_def = find_struct_impl(&ctx, &parent_enum, &fn_name)?;
@@ -121,20 +141,24 @@ pub(crate) fn generate_enum_projection_method(
121141
acc.add(AssistId(assist_id, AssistKind::Generate), assist_description, target, |builder| {
122142
let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v));
123143
let method = format!(
124-
" {0}fn {1}({2}self) -> Option<{2}{3}> {{
125-
if let Self::{4}{5} = self {{
126-
Some({6})
144+
" {0}fn {1}({2}) -> {3}{4}{5} {{
145+
if let Self::{6}{7} = self {{
146+
{8}({9})
127147
}} else {{
128-
None
148+
{10}
129149
}}
130150
}}",
131151
vis,
132152
fn_name,
133-
ref_prefix,
153+
props.self_param,
154+
props.return_prefix,
134155
field_type.syntax(),
156+
props.return_suffix,
135157
variant_name,
136158
pattern_suffix,
159+
props.happy_case,
137160
bound_name,
161+
props.sad_case,
138162
);
139163

140164
add_method_to_adt(builder, &parent_enum, impl_def, &method);
@@ -148,9 +172,9 @@ mod tests {
148172
use super::*;
149173

150174
#[test]
151-
fn test_generate_enum_into_tuple_variant() {
175+
fn test_generate_enum_try_into_tuple_variant() {
152176
check_assist(
153-
generate_enum_into_method,
177+
generate_enum_try_into_method,
154178
r#"
155179
enum Value {
156180
Number(i32),
@@ -162,42 +186,42 @@ enum Value {
162186
}
163187
164188
impl Value {
165-
fn into_text(self) -> Option<String> {
189+
fn try_into_text(self) -> Result<String, Self> {
166190
if let Self::Text(v) = self {
167-
Some(v)
191+
Ok(v)
168192
} else {
169-
None
193+
Err(self)
170194
}
171195
}
172196
}"#,
173197
);
174198
}
175199

176200
#[test]
177-
fn test_generate_enum_into_already_implemented() {
201+
fn test_generate_enum_try_into_already_implemented() {
178202
check_assist_not_applicable(
179-
generate_enum_into_method,
203+
generate_enum_try_into_method,
180204
r#"enum Value {
181205
Number(i32),
182206
Text(String)$0,
183207
}
184208
185209
impl Value {
186-
fn into_text(self) -> Option<String> {
210+
fn try_into_text(self) -> Result<String, Self> {
187211
if let Self::Text(v) = self {
188-
Some(v)
212+
Ok(v)
189213
} else {
190-
None
214+
Err(self)
191215
}
192216
}
193217
}"#,
194218
);
195219
}
196220

197221
#[test]
198-
fn test_generate_enum_into_unit_variant() {
222+
fn test_generate_enum_try_into_unit_variant() {
199223
check_assist_not_applicable(
200-
generate_enum_into_method,
224+
generate_enum_try_into_method,
201225
r#"enum Value {
202226
Number(i32),
203227
Text(String),
@@ -207,9 +231,9 @@ impl Value {
207231
}
208232

209233
#[test]
210-
fn test_generate_enum_into_record_with_multiple_fields() {
234+
fn test_generate_enum_try_into_record_with_multiple_fields() {
211235
check_assist_not_applicable(
212-
generate_enum_into_method,
236+
generate_enum_try_into_method,
213237
r#"enum Value {
214238
Number(i32),
215239
Text(String),
@@ -219,9 +243,9 @@ impl Value {
219243
}
220244

221245
#[test]
222-
fn test_generate_enum_into_tuple_with_multiple_fields() {
246+
fn test_generate_enum_try_into_tuple_with_multiple_fields() {
223247
check_assist_not_applicable(
224-
generate_enum_into_method,
248+
generate_enum_try_into_method,
225249
r#"enum Value {
226250
Number(i32),
227251
Text(String, String)$0,
@@ -230,9 +254,9 @@ impl Value {
230254
}
231255

232256
#[test]
233-
fn test_generate_enum_into_record_variant() {
257+
fn test_generate_enum_try_into_record_variant() {
234258
check_assist(
235-
generate_enum_into_method,
259+
generate_enum_try_into_method,
236260
r#"enum Value {
237261
Number(i32),
238262
Text { text: String }$0,
@@ -243,11 +267,11 @@ impl Value {
243267
}
244268
245269
impl Value {
246-
fn into_text(self) -> Option<String> {
270+
fn try_into_text(self) -> Result<String, Self> {
247271
if let Self::Text { text } = self {
248-
Some(text)
272+
Ok(text)
249273
} else {
250-
None
274+
Err(self)
251275
}
252276
}
253277
}"#,

crates/ide_assists/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ mod handlers {
191191
generate_default_from_enum_variant::generate_default_from_enum_variant,
192192
generate_derive::generate_derive,
193193
generate_enum_is_method::generate_enum_is_method,
194-
generate_enum_projection_method::generate_enum_into_method,
194+
generate_enum_projection_method::generate_enum_try_into_method,
195195
generate_enum_projection_method::generate_enum_as_method,
196196
generate_from_impl_for_enum::generate_from_impl_for_enum,
197197
generate_function::generate_function,

crates/ide_assists/src/tests/generated.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -511,35 +511,6 @@ impl Value {
511511
)
512512
}
513513

514-
#[test]
515-
fn doctest_generate_enum_into_method() {
516-
check_doc_test(
517-
"generate_enum_into_method",
518-
r#####"
519-
enum Value {
520-
Number(i32),
521-
Text(String)$0,
522-
}
523-
"#####,
524-
r#####"
525-
enum Value {
526-
Number(i32),
527-
Text(String),
528-
}
529-
530-
impl Value {
531-
fn into_text(self) -> Option<String> {
532-
if let Self::Text(v) = self {
533-
Some(v)
534-
} else {
535-
None
536-
}
537-
}
538-
}
539-
"#####,
540-
)
541-
}
542-
543514
#[test]
544515
fn doctest_generate_enum_is_method() {
545516
check_doc_test(
@@ -568,6 +539,35 @@ impl Version {
568539
)
569540
}
570541

542+
#[test]
543+
fn doctest_generate_enum_try_into_method() {
544+
check_doc_test(
545+
"generate_enum_try_into_method",
546+
r#####"
547+
enum Value {
548+
Number(i32),
549+
Text(String)$0,
550+
}
551+
"#####,
552+
r#####"
553+
enum Value {
554+
Number(i32),
555+
Text(String),
556+
}
557+
558+
impl Value {
559+
fn try_into_text(self) -> Result<String, Self> {
560+
if let Self::Text(v) = self {
561+
Ok(v)
562+
} else {
563+
Err(self)
564+
}
565+
}
566+
}
567+
"#####,
568+
)
569+
}
570+
571571
#[test]
572572
fn doctest_generate_from_impl_for_enum() {
573573
check_doc_test(

0 commit comments

Comments
 (0)