From 8e3423f8b63e34fca62d7fe05a940975e5ce5e76 Mon Sep 17 00:00:00 2001 From: Darrell Roberts Date: Tue, 4 Jun 2024 15:26:11 -0400 Subject: [PATCH 1/2] Allow swift decorator constraints to apply to generics --- .../input.rs | 17 +++++++++++ .../output.swift | 28 +++++++++++++++++++ core/src/language/swift.rs | 8 +++++- core/tests/snapshot_tests.rs | 1 + 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 core/data/tests/generic_struct_with_constraints_and_decorators/input.rs create mode 100644 core/data/tests/generic_struct_with_constraints_and_decorators/output.swift diff --git a/core/data/tests/generic_struct_with_constraints_and_decorators/input.rs b/core/data/tests/generic_struct_with_constraints_and_decorators/input.rs new file mode 100644 index 00000000..962e5e4c --- /dev/null +++ b/core/data/tests/generic_struct_with_constraints_and_decorators/input.rs @@ -0,0 +1,17 @@ +#[typeshare(swift = "Equatable")] +pub struct Button { + /// Label of the button + pub label: String, + /// Accessibility label if it needed to be different than label + pub accessibility_label: Option, + /// Optional tooltips that provide extra explanation for a button + pub tooltip: Option, + /// Button action if there one + pub action: Option, + /// Icon if there is one + pub icon: Option, + /// Button state + pub state: ButtonState, + /// Button Mode + pub style: ButtonStyle, +} diff --git a/core/data/tests/generic_struct_with_constraints_and_decorators/output.swift b/core/data/tests/generic_struct_with_constraints_and_decorators/output.swift new file mode 100644 index 00000000..1d403024 --- /dev/null +++ b/core/data/tests/generic_struct_with_constraints_and_decorators/output.swift @@ -0,0 +1,28 @@ +import Foundation + +public struct Button: Codable, Equatable { + /// Label of the button + public let label: String + /// Accessibility label if it needed to be different than label + public let accessibility_label: String? + /// Optional tooltips that provide extra explanation for a button + public let tooltip: String? + /// Button action if there one + public let action: T? + /// Icon if there is one + public let icon: Icon? + /// Button state + public let state: ButtonState + /// Button Mode + public let style: ButtonStyle + + public init(label: String, accessibility_label: String?, tooltip: String?, action: T?, icon: Icon?, state: ButtonState, style: ButtonStyle) { + self.label = label + self.accessibility_label = accessibility_label + self.tooltip = tooltip + self.action = action + self.icon = icon + self.state = state + self.style = style + } +} diff --git a/core/src/language/swift.rs b/core/src/language/swift.rs index 83df5f17..f2fba105 100644 --- a/core/src/language/swift.rs +++ b/core/src/language/swift.rs @@ -278,7 +278,13 @@ impl Language for Swift { .for_each(|d| decs.push(d.clone())); } - let generic_constraint_string = default_generic_constraints.get_constraints().join(" & "); + // Include any decorator constraints on the generic types. + let generic_constraint_string = default_generic_constraints + .get_constraints() + .chain(decs.iter()) + .collect::>() + .into_iter() + .join(" & "); writeln!( w, diff --git a/core/tests/snapshot_tests.rs b/core/tests/snapshot_tests.rs index 6e119bfa..346d3b89 100644 --- a/core/tests/snapshot_tests.rs +++ b/core/tests/snapshot_tests.rs @@ -569,4 +569,5 @@ tests! { go ]; can_generate_anonymous_struct_with_skipped_fields: [swift, kotlin, scala, typescript, go]; + generic_struct_with_constraints_and_decorators: [swift]; } From 99504997947ddb3c41c8b2e22bf44d6feaf49a98 Mon Sep 17 00:00:00 2001 From: Darrell Roberts Date: Tue, 4 Jun 2024 17:00:00 -0400 Subject: [PATCH 2/2] Update generics for writing enum --- core/src/language/swift.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/src/language/swift.rs b/core/src/language/swift.rs index f2fba105..5b9983f2 100644 --- a/core/src/language/swift.rs +++ b/core/src/language/swift.rs @@ -452,10 +452,15 @@ impl Language for Swift { self.write_comments(w, 0, &shared.comments)?; let indirect = if shared.is_recursive { "indirect " } else { "" }; + let generic_constraint_string = self .default_generic_constraints .get_constraints() + .chain(decs.iter()) + .collect::>() + .into_iter() .join(" & "); + writeln!( w, "public {}enum {}{}: {} {{",