Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support enums with named data #175

Merged
merged 7 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,33 @@ class SharedEnumTests: XCTestCase {
default:
XCTFail()
}
}

func testEnumWithNamedData() {
let enumWithNamedData1 = EnumWithNamedData.Variant1(hello: create_string("hello"), data_u8: 123)
switch reflect_enum_with_named_data(enumWithNamedData1) {
case .Variant1(let hello, let dataU8):
XCTAssertEqual(hello.toString(), "hello")
XCTAssertEqual(dataU8, 123)
default:
XCTFail()
}

let enumWithNamedData2 = EnumWithNamedData.Variant2(data_i32: -123)
switch reflect_enum_with_named_data(enumWithNamedData2) {
case .Variant2(let dataI32):
XCTAssertEqual(dataI32, -123)
default:
XCTFail()
}

let enumWithNamedData3 = EnumWithNamedData.Variant3
switch reflect_enum_with_named_data(enumWithNamedData3) {
case .Variant3:
break
default:
XCTFail()
}

}
}
48 changes: 25 additions & 23 deletions crates/swift-bridge-ir/src/bridged_type/shared_enum/enum_variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::bridged_type::{BridgedType, StructFields, TypePosition};
use crate::parse::TypeDeclarations;
use proc_macro2::Ident;
use proc_macro2::TokenStream;
use quote::quote;
use quote::{format_ident, quote};
use std::fmt::{Debug, Formatter};
use syn::spanned::Spanned;
use syn::Path;
Expand All @@ -27,26 +27,29 @@ impl EnumVariant {
.fields
.normalized_fields()
.iter()
.map(|norm_field| norm_field.to_enum_field(&quote! {value}))
.map(|norm_field| norm_field.ffi_field_name())
.map(|norm_field| format_ident!("{}", norm_field))
.map(|norm_field| quote! {#norm_field})
.collect();
let converted_fields: Vec<TokenStream> = self
.fields
.normalized_fields()
.iter()
.map(|norm_field| {
let maybe_name_and_colon = norm_field.maybe_name_and_colon();
let access_field = norm_field.to_enum_field(&quote! {value});
let access_field = format_ident!("{}", norm_field.ffi_field_name());
let access_field = quote! {#access_field};
let ty = BridgedType::new_with_type(&norm_field.ty, types).unwrap();
let converted_field = ty.convert_rust_expression_to_ffi_type(
&access_field,
swift_bridge_path,
types,
norm_field.ty.span(),
);

quote! {
let tokens = quote! {
#maybe_name_and_colon #converted_field
}
};
tokens
})
.collect();

Expand Down Expand Up @@ -75,16 +78,17 @@ impl EnumVariant {
.fields
.normalized_fields()
.iter()
.map(|norm_field| norm_field.to_enum_field(&quote! {value}))
.map(|norm_field| format_ident!("{}", norm_field.ffi_field_name()))
.map(|norm_field| quote! {#norm_field})
.collect();
let converted_fields: Vec<TokenStream> = self
.fields
.normalized_fields()
.iter()
.map(|norm_field| {
let maybe_name_and_colon = norm_field.maybe_name_and_colon();
let access_field = norm_field.to_enum_field(&quote!(value));

let access_field = format_ident!("{}", norm_field.ffi_field_name());
let access_field = quote! {#access_field};
let ty = BridgedType::new_with_type(&norm_field.ty, types).unwrap();
let converted_field = ty.convert_ffi_expression_to_rust_type(
&access_field,
Expand Down Expand Up @@ -123,17 +127,17 @@ impl EnumVariant {
.iter()
.map(|norm_field| {
let field_name = norm_field.ffi_field_name();

let ty = BridgedType::new_with_type(&norm_field.ty, types).unwrap();
ty.convert_ffi_value_to_swift_value(
let field = ty.convert_ffi_value_to_swift_value(
&format!(
"self.payload.{variant_name}.{field_name}",
variant_name = self.name,
field_name = field_name
),
TypePosition::SharedStructField,
types,
)
);
norm_field.struct_field_setter_string(field)
})
.collect();
let converted_fields = converted_fields.join(", ");
Expand Down Expand Up @@ -177,17 +181,13 @@ impl EnumVariant {
.normalized_fields()
.iter()
.map(|norm_field| {
let field_name = norm_field.ffi_field_name();
let ffi_field_name = norm_field.ffi_field_name();
let ty = BridgedType::new_with_type(&norm_field.ty, types).unwrap();
let enum_field = ty.convert_swift_expression_to_ffi_type(
&format!("value{field_name}", field_name = field_name),
let variant_field = ty.convert_swift_expression_to_ffi_type(
&format!("{}", ffi_field_name),
TypePosition::SharedStructField,
);
format!(
"{field_name}: {enum_field}",
field_name = field_name,
enum_field = enum_field
)
norm_field.struct_ffi_field_setter_string(variant_field)
})
.collect();
let converted_fields = converted_fields.join(", ");
Expand All @@ -197,8 +197,8 @@ impl EnumVariant {
.normalized_fields()
.iter()
.map(|norm_field| {
let ffi_field_name = norm_field.ffi_field_name();
format!("let value{ffi_field_name}", ffi_field_name = ffi_field_name)
let field_name = norm_field.ffi_field_name();
format!("let {field_name}", field_name = field_name)
})
.collect();
let associated_values = associated_values.join(", ");
Expand All @@ -215,7 +215,9 @@ impl EnumVariant {
fn wrap_fields(&self, fields: &[TokenStream]) -> TokenStream {
match &self.fields {
StructFields::Named(_) => {
todo!();
quote! {
{ #(#fields),* }
}
}
StructFields::Unnamed(_) => {
quote! {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote};
use quote::quote;
use std::str::FromStr;
use syn::Type;

Expand Down Expand Up @@ -38,6 +38,36 @@ impl NormalizedStructField {
}
}

/// Create a string for setting the value of a struct's field.
///
/// Example if named field -> "field_name: someValue".
/// Example if unnamed field -> "someValue".
pub fn struct_field_setter_string(&self, value: String) -> String {
match &self.accessor {
NormalizedStructFieldAccessor::Named(name) => {
format!("{}: {}", name.to_string(), value)
}
NormalizedStructFieldAccessor::Unnamed(_) => {
format!("{}", value)
}
}
}

/// Create a string for setting the value of a struct's ffi field.
///
/// Example if named field -> "field_name: someValue".
/// Example if unnamed field -> "_0: someValue".
pub fn struct_ffi_field_setter_string(&self, value: String) -> String {
match &self.accessor {
NormalizedStructFieldAccessor::Named(name) => {
format!("{}: {}", name.to_string(), value)
}
NormalizedStructFieldAccessor::Unnamed(idx) => {
format!("_{}: {}", idx, value)
}
}
}

/// Access a struct's field
///
/// // Example named field access
Expand All @@ -56,19 +86,6 @@ impl NormalizedStructField {
}
}

pub fn to_enum_field(&self, expression: &TokenStream) -> TokenStream {
match &self.accessor {
NormalizedStructFieldAccessor::Named(_) => {
todo!()
}
NormalizedStructFieldAccessor::Unnamed(idx) => {
let idx = TokenStream::from_str(&idx.to_string()).unwrap();
let expression = format_ident!("{}_{}", expression.to_string(), idx.to_string());
quote! { #expression }
}
}
}

pub fn ffi_field_name(&self) -> String {
match &self.accessor {
NormalizedStructFieldAccessor::Named(name) => name.to_string(),
Expand Down
Loading