Skip to content

Commit f97cc3b

Browse files
committed
Better utilize match in kotlin.rs
1 parent a281654 commit f97cc3b

File tree

1 file changed

+47
-73
lines changed

1 file changed

+47
-73
lines changed

core/src/language/kotlin.rs

Lines changed: 47 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -124,39 +124,25 @@ impl Language for Kotlin {
124124
writeln!(w, "@JvmInline")?;
125125
writeln!(w, "value class {}{}(", self.prefix, ty.id.renamed)?;
126126

127-
if ty.is_redacted {
128-
self.write_private_element(
129-
w,
130-
&RustField {
131-
id: Id {
132-
original: String::from("value"),
133-
renamed: String::from("value"),
134-
},
135-
ty: ty.r#type.clone(),
136-
comments: vec![],
137-
has_default: false,
138-
decorators: HashMap::new(),
139-
},
140-
&[],
141-
false,
142-
)?;
143-
} else {
144-
self.write_element(
145-
w,
146-
&RustField {
147-
id: Id {
148-
original: String::from("value"),
149-
renamed: String::from("value"),
150-
},
151-
ty: ty.r#type.clone(),
152-
comments: vec![],
153-
has_default: false,
154-
decorators: HashMap::new(),
127+
self.write_element(
128+
w,
129+
&RustField {
130+
id: Id {
131+
original: String::from("value"),
132+
renamed: String::from("value"),
155133
},
156-
&[],
157-
false,
158-
)?;
159-
}
134+
ty: ty.r#type.clone(),
135+
comments: vec![],
136+
has_default: false,
137+
decorators: HashMap::new(),
138+
},
139+
&[],
140+
false,
141+
match ty.is_redacted {
142+
true => Visibility::Private,
143+
false => Visibility::Public
144+
},
145+
)?;
160146

161147
writeln!(w)?;
162148

@@ -216,10 +202,10 @@ impl Language for Kotlin {
216202

217203
if let Some((last, elements)) = rs.fields.split_last() {
218204
for f in elements.iter() {
219-
self.write_element(w, f, rs.generic_types.as_slice(), requires_serial_name)?;
205+
self.write_element(w, f, rs.generic_types.as_slice(), requires_serial_name, Visibility::Public)?;
220206
writeln!(w, ",")?;
221207
}
222-
self.write_element(w, last, rs.generic_types.as_slice(), requires_serial_name)?;
208+
self.write_element(w, last, rs.generic_types.as_slice(), requires_serial_name, Visibility::Public)?;
223209
writeln!(w)?;
224210
}
225211

@@ -295,6 +281,11 @@ impl Language for Kotlin {
295281
}
296282
}
297283

284+
enum Visibility {
285+
Public,
286+
Private
287+
}
288+
298289
impl Kotlin {
299290
fn write_enum_variants(&mut self, w: &mut dyn Write, e: &RustEnum) -> std::io::Result<()> {
300291
match e {
@@ -422,6 +413,7 @@ impl Kotlin {
422413
f: &RustField,
423414
generic_types: &[String],
424415
requires_serial_name: bool,
416+
visibility: Visibility,
425417
) -> std::io::Result<()> {
426418
self.write_comments(w, 1, &f.comments)?;
427419
if requires_serial_name {
@@ -434,46 +426,28 @@ impl Kotlin {
434426
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?,
435427
};
436428

437-
write!(
438-
w,
439-
"\tval {}: {}{}",
440-
remove_dash_from_identifier(&f.id.renamed),
441-
ty,
442-
(f.has_default && !f.ty.is_optional())
443-
.then_some("? = null")
444-
.or_else(|| f.ty.is_optional().then_some(" = null"))
445-
.unwrap_or_default()
446-
)
447-
}
448-
449-
fn write_private_element(
450-
&mut self,
451-
w: &mut dyn Write,
452-
f: &RustField,
453-
generic_types: &[String],
454-
requires_serial_name: bool,
455-
) -> std::io::Result<()> {
456-
self.write_comments(w, 1, &f.comments)?;
457-
if requires_serial_name {
458-
writeln!(w, "\t@SerialName({:?})", &f.id.renamed)?;
429+
match visibility {
430+
Visibility::Public => write!(
431+
w,
432+
"\tval {}: {}{}",
433+
remove_dash_from_identifier(&f.id.renamed),
434+
ty,
435+
(f.has_default && !f.ty.is_optional())
436+
.then_some("? = null")
437+
.or_else(|| f.ty.is_optional().then_some(" = null"))
438+
.unwrap_or_default()
439+
),
440+
Visibility::Private => write!(
441+
w,
442+
"\tprivate val {}: {}{}",
443+
remove_dash_from_identifier(&f.id.renamed),
444+
ty,
445+
(f.has_default && !f.ty.is_optional())
446+
.then_some("? = null")
447+
.or_else(|| f.ty.is_optional().then_some(" = null"))
448+
.unwrap_or_default()
449+
)
459450
}
460-
let ty = match f.type_override(SupportedLanguage::Kotlin) {
461-
Some(type_override) => type_override.to_owned(),
462-
None => self
463-
.format_type(&f.ty, generic_types)
464-
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?,
465-
};
466-
467-
write!(
468-
w,
469-
"\tprivate val {}: {}{}",
470-
remove_dash_from_identifier(&f.id.renamed),
471-
ty,
472-
(f.has_default && !f.ty.is_optional())
473-
.then_some("? = null")
474-
.or_else(|| f.ty.is_optional().then_some(" = null"))
475-
.unwrap_or_default()
476-
)
477451
}
478452

479453
fn write_comment(

0 commit comments

Comments
 (0)