Skip to content

Commit

Permalink
Merge pull request #174 from darrell-roberts/swift-generic-constraints-2
Browse files Browse the repository at this point in the history
allow defining generic type constraints for swift
  • Loading branch information
charlespierce authored Jun 6, 2024
2 parents e8b7f76 + db5dcae commit a03f5ef
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 154 deletions.
2 changes: 2 additions & 0 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub struct SwiftParams {
pub type_mappings: HashMap<String, String>,
pub default_decorators: Vec<String>,
pub default_generic_constraints: Vec<String>,
/// The contraints to apply to `CodableVoid`.
pub codablevoid_constraints: Vec<String>,
}

#[derive(Default, Serialize, Deserialize, PartialEq, Eq, Debug)]
Expand Down
1 change: 1 addition & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ fn language(
config.swift.default_generic_constraints,
),
multi_file,
codablevoid_constraints: config.swift.codablevoid_constraints,
..Default::default()
}),
SupportedLanguage::Kotlin => Box::new(Kotlin {
Expand Down
2 changes: 1 addition & 1 deletion core/data/tests/can_generate_generic_struct/output.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,4 @@ public enum CoreEnumUsingGenericStruct: Codable {
}

/// () isn't codable, so we use this instead to represent Rust's unit type
public struct CodableVoid: Codable {}
public struct CodableVoid: Codable, Equatable {}
2 changes: 1 addition & 1 deletion core/data/tests/can_handle_unit_type/output.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ public enum EnumHasVoidType: Codable {
}

/// () isn't codable, so we use this instead to represent Rust's unit type
public struct CodableVoid: Codable {}
public struct CodableVoid: Codable, Equatable {}
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
#[typeshare(swift = "Equatable")]
pub struct Button<T> {
#[typeshare(
swift = "Equatable, Identifiable",
swiftGenericConstraints = "T: Equatable & SomeThingElse, V: Equatable"
)]
pub struct Button<T, V, I> {
/// Label of the button
pub label: String,
pub label: I,
/// Accessibility label if it needed to be different than label
pub accessibility_label: Option<String>,
/// Optional tooltips that provide extra explanation for a button
pub tooltip: Option<String>,
/// Button action if there one
pub action: Option<T>,
/// Icon if there is one
pub icon: Option<Icon>,
pub icon: Option<V>,
/// Button state
pub state: ButtonState,
/// Button Mode
pub style: ButtonStyle,
}

#[typeshare]
pub struct ButtonState;

#[typeshare]
pub struct ButtonStyle;
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import Foundation

public struct Button<T: Codable & Equatable>: Codable, Equatable {
public struct ButtonState: Codable {
public init() {}
}

public struct ButtonStyle: Codable {
public init() {}
}

public struct Button<T: Codable & Equatable & SomeThingElse, V: Codable & Equatable, I: Codable>: Codable, Equatable, Identifiable {
/// Label of the button
public let label: String
public let label: I
/// 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?
public let icon: V?
/// 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) {
public init(label: I, accessibility_label: String?, tooltip: String?, action: T?, icon: V?, state: ButtonState, style: ButtonStyle) {
self.label = label
self.accessibility_label = accessibility_label
self.tooltip = tooltip
Expand Down
4 changes: 2 additions & 2 deletions core/src/language/kotlin.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{Language, ScopedCrateTypes};
use crate::language::SupportedLanguage;
use crate::parser::{remove_dash_from_identifier, ParsedData};
use crate::parser::{remove_dash_from_identifier, DecoratorKind, ParsedData};
use crate::rust_types::{RustTypeFormatError, SpecialRustType};
use crate::{
rename::RenameExt,
Expand Down Expand Up @@ -169,7 +169,7 @@ impl Language for Kotlin {
writeln!(w)?;
}
write!(w, ")")?;
if let Some(kotlin_decorators) = rs.decorators.get(&SupportedLanguage::Kotlin) {
if let Some(kotlin_decorators) = rs.decorators.get(&DecoratorKind::Kotlin) {
let redacted_decorator = String::from(REDACTED_TO_STRING);
if kotlin_decorators.iter().any(|d| *d == redacted_decorator) {
writeln!(w, " {{")?;
Expand Down
Loading

0 comments on commit a03f5ef

Please sign in to comment.