diff --git a/uniffi_bindgen/src/bindings/kotlin/gen_kotlin/mod.rs b/uniffi_bindgen/src/bindings/kotlin/gen_kotlin/mod.rs index 7663c26fab..7e5ae84620 100644 --- a/uniffi_bindgen/src/bindings/kotlin/gen_kotlin/mod.rs +++ b/uniffi_bindgen/src/bindings/kotlin/gen_kotlin/mod.rs @@ -26,6 +26,7 @@ mod miscellany; mod object; mod primitives; mod record; +mod variant; // config options to customize the generated Kotlin. #[derive(Debug, Default, Clone, Serialize, Deserialize)] @@ -226,50 +227,8 @@ impl<'a> KotlinWrapper<'a> { pub struct KotlinCodeOracle; impl KotlinCodeOracle { - // Map `Type` instances to a `Box` for that type. - // - // There is a companion match in `templates/Types.kt` which performs a similar function for the - // template code. - // - // - When adding additional types here, make sure to also add a match arm to the `Types.kt` template. - // - To keep things manageable, let's try to limit ourselves to these 2 mega-matches - fn create_code_type(&self, type_: Type) -> Box { - match type_ { - Type::UInt8 => Box::new(primitives::UInt8CodeType), - Type::Int8 => Box::new(primitives::Int8CodeType), - Type::UInt16 => Box::new(primitives::UInt16CodeType), - Type::Int16 => Box::new(primitives::Int16CodeType), - Type::UInt32 => Box::new(primitives::UInt32CodeType), - Type::Int32 => Box::new(primitives::Int32CodeType), - Type::UInt64 => Box::new(primitives::UInt64CodeType), - Type::Int64 => Box::new(primitives::Int64CodeType), - Type::Float32 => Box::new(primitives::Float32CodeType), - Type::Float64 => Box::new(primitives::Float64CodeType), - Type::Boolean => Box::new(primitives::BooleanCodeType), - Type::String => Box::new(primitives::StringCodeType), - Type::Bytes => Box::new(primitives::BytesCodeType), - - Type::Timestamp => Box::new(miscellany::TimestampCodeType), - Type::Duration => Box::new(miscellany::DurationCodeType), - - Type::Enum(id) => Box::new(enum_::EnumCodeType::new(id)), - Type::Object { name, .. } => Box::new(object::ObjectCodeType::new(name)), - Type::Record(id) => Box::new(record::RecordCodeType::new(id)), - Type::Error(id) => Box::new(error::ErrorCodeType::new(id)), - Type::CallbackInterface(id) => { - Box::new(callback_interface::CallbackInterfaceCodeType::new(id)) - } - Type::ForeignExecutor => Box::new(executor::ForeignExecutorCodeType), - Type::Optional(inner) => Box::new(compounds::OptionalCodeType::new(*inner)), - Type::Sequence(inner) => Box::new(compounds::SequenceCodeType::new(*inner)), - Type::Map(key, value) => Box::new(compounds::MapCodeType::new(*key, *value)), - Type::External { name, .. } => Box::new(external::ExternalCodeType::new(name)), - Type::Custom { name, .. } => Box::new(custom::CustomCodeType::new(name)), - } - } - fn find(&self, type_: &Type) -> Box { - self.create_code_type(type_.clone()) + type_.clone().as_type().as_codetype() } /// Get the idiomatic Kotlin rendering of a class name (for enums, records, errors, etc). @@ -334,58 +293,96 @@ impl KotlinCodeOracle { } } -pub mod filters { - use super::*; +pub trait AsCodeType { + fn as_codetype(&self) -> Box; +} - fn oracle() -> &'static KotlinCodeOracle { - &KotlinCodeOracle +impl AsCodeType for T { + fn as_codetype(&self) -> Box { + // Map `Type` instances to a `Box` for that type. + // + // There is a companion match in `templates/Types.kt` which performs a similar function for the + // template code. + // + // - When adding additional types here, make sure to also add a match arm to the `Types.kt` template. + // - To keep things manageable, let's try to limit ourselves to these 2 mega-matches + match self.as_type() { + Type::UInt8 => Box::new(primitives::UInt8CodeType), + Type::Int8 => Box::new(primitives::Int8CodeType), + Type::UInt16 => Box::new(primitives::UInt16CodeType), + Type::Int16 => Box::new(primitives::Int16CodeType), + Type::UInt32 => Box::new(primitives::UInt32CodeType), + Type::Int32 => Box::new(primitives::Int32CodeType), + Type::UInt64 => Box::new(primitives::UInt64CodeType), + Type::Int64 => Box::new(primitives::Int64CodeType), + Type::Float32 => Box::new(primitives::Float32CodeType), + Type::Float64 => Box::new(primitives::Float64CodeType), + Type::Boolean => Box::new(primitives::BooleanCodeType), + Type::String => Box::new(primitives::StringCodeType), + Type::Bytes => Box::new(primitives::BytesCodeType), + + Type::Timestamp => Box::new(miscellany::TimestampCodeType), + Type::Duration => Box::new(miscellany::DurationCodeType), + + Type::Enum(id) => Box::new(enum_::EnumCodeType::new(id)), + Type::Object { name, .. } => Box::new(object::ObjectCodeType::new(name)), + Type::Record(id) => Box::new(record::RecordCodeType::new(id)), + Type::Error(id) => Box::new(error::ErrorCodeType::new(id)), + Type::CallbackInterface(id) => { + Box::new(callback_interface::CallbackInterfaceCodeType::new(id)) + } + Type::ForeignExecutor => Box::new(executor::ForeignExecutorCodeType), + Type::Optional(inner) => Box::new(compounds::OptionalCodeType::new(*inner)), + Type::Sequence(inner) => Box::new(compounds::SequenceCodeType::new(*inner)), + Type::Map(key, value) => Box::new(compounds::MapCodeType::new(*key, *value)), + Type::External { name, .. } => Box::new(external::ExternalCodeType::new(name)), + Type::Custom { name, .. } => Box::new(custom::CustomCodeType::new(name)), + } } +} + +pub mod filters { + use super::*; - pub fn type_name(as_type: &impl AsType) -> Result { - Ok(oracle().find(&as_type.as_type()).type_label()) + pub fn type_name(as_ct: &impl AsCodeType) -> Result { + Ok(as_ct.as_codetype().type_label()) } - pub fn canonical_name(as_type: &impl AsType) -> Result { - Ok(oracle().find(&as_type.as_type()).canonical_name()) + pub fn canonical_name(as_ct: &impl AsCodeType) -> Result { + Ok(as_ct.as_codetype().canonical_name()) } - pub fn ffi_converter_name(as_type: &impl AsType) -> Result { - Ok(oracle().find(&as_type.as_type()).ffi_converter_name()) + pub fn ffi_converter_name(as_ct: &impl AsCodeType) -> Result { + Ok(as_ct.as_codetype().ffi_converter_name()) } - pub fn lower_fn(as_type: &impl AsType) -> Result { + pub fn lower_fn(as_ct: &impl AsCodeType) -> Result { Ok(format!( "{}.lower", - oracle().find(&as_type.as_type()).ffi_converter_name() + as_ct.as_codetype().ffi_converter_name() )) } - pub fn allocation_size_fn(as_type: &impl AsType) -> Result { + pub fn allocation_size_fn(as_ct: &impl AsCodeType) -> Result { Ok(format!( "{}.allocationSize", - oracle().find(&as_type.as_type()).ffi_converter_name() + as_ct.as_codetype().ffi_converter_name() )) } - pub fn write_fn(as_type: &impl AsType) -> Result { + pub fn write_fn(as_ct: &impl AsCodeType) -> Result { Ok(format!( "{}.write", - oracle().find(&as_type.as_type()).ffi_converter_name() + as_ct.as_codetype().ffi_converter_name() )) } - pub fn lift_fn(as_type: &impl AsType) -> Result { - Ok(format!( - "{}.lift", - oracle().find(&as_type.as_type()).ffi_converter_name() - )) + pub fn lift_fn(as_ct: &impl AsCodeType) -> Result { + Ok(format!("{}.lift", as_ct.as_codetype().ffi_converter_name())) } - pub fn read_fn(as_type: &impl AsType) -> Result { - Ok(format!( - "{}.read", - oracle().find(&as_type.as_type()).ffi_converter_name() - )) + pub fn read_fn(as_ct: &impl AsCodeType) -> Result { + Ok(format!("{}.read", as_ct.as_codetype().ffi_converter_name())) } pub fn error_handler(result_type: &ResultType) -> Result { @@ -419,40 +416,39 @@ pub mod filters { pub fn render_literal( literal: &Literal, - as_type: &impl AsType, + as_ct: &impl AsCodeType, ) -> Result { - Ok(oracle().find(&as_type.as_type()).literal(literal)) + Ok(as_ct.as_codetype().literal(literal)) } /// Get the Kotlin syntax for representing a given low-level `FfiType`. pub fn ffi_type_name(type_: &FfiType) -> Result { - Ok(oracle().ffi_type_label(type_)) - } - - /// Get the idiomatic Kotlin rendering of a class name (for enums, records, errors, etc). - pub fn class_name(nm: &str) -> Result { - Ok(oracle().class_name(nm)) + Ok(KotlinCodeOracle.ffi_type_label(type_)) } /// Get the idiomatic Kotlin rendering of a function name. pub fn fn_name(nm: &str) -> Result { - Ok(oracle().fn_name(nm)) + Ok(KotlinCodeOracle.fn_name(nm)) } /// Get the idiomatic Kotlin rendering of a variable name. pub fn var_name(nm: &str) -> Result { - Ok(oracle().var_name(nm)) + Ok(KotlinCodeOracle.var_name(nm)) } - /// Get the idiomatic Kotlin rendering of an individual enum variant. - pub fn enum_variant(nm: &str) -> Result { - Ok(oracle().enum_variant_name(nm)) + pub fn variant_name(v: &Variant) -> Result { + Ok(KotlinCodeOracle.enum_variant_name(v.name())) + } + + /// Get a codetype for idiomatic Kotlin rendering of an individual enum variant. + pub fn enum_variant(v: &Variant) -> Result { + Ok(v.clone()) } - /// Get the idiomatic Kotlin rendering of an exception name, replacing - /// `Error` with `Exception`. - pub fn exception_name(nm: &str) -> Result { - Ok(oracle().error_name(nm)) + /// Get a codetype for idiomatic Kotlin rendering of an individual enum variant + /// when used in an error. + pub fn error_variant(v: &Variant) -> Result { + Ok(variant::ErrorVariantCodeTypeProvider { v: v.clone() }) } /// Remove the "`" chars we put around function/variable names diff --git a/uniffi_bindgen/src/bindings/kotlin/gen_kotlin/variant.rs b/uniffi_bindgen/src/bindings/kotlin/gen_kotlin/variant.rs new file mode 100644 index 0000000000..624950949a --- /dev/null +++ b/uniffi_bindgen/src/bindings/kotlin/gen_kotlin/variant.rs @@ -0,0 +1,45 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use super::{AsCodeType, KotlinCodeOracle}; +use crate::backend::CodeType; +use crate::interface::Variant; + +#[derive(Debug)] +pub(super) struct VariantCodeType { + pub v: Variant, +} + +impl CodeType for VariantCodeType { + fn type_label(&self) -> String { + KotlinCodeOracle.class_name(self.v.name()) + } +} + +impl AsCodeType for Variant { + fn as_codetype(&self) -> Box { + Box::new(VariantCodeType { v: self.clone() }) + } +} + +#[derive(Debug)] +pub(super) struct ErrorVariantCodeType { + pub v: Variant, +} + +impl CodeType for ErrorVariantCodeType { + fn type_label(&self) -> String { + KotlinCodeOracle.error_name(self.v.name()) + } +} + +pub(super) struct ErrorVariantCodeTypeProvider { + pub v: Variant, +} + +impl AsCodeType for ErrorVariantCodeTypeProvider { + fn as_codetype(&self) -> Box { + Box::new(ErrorVariantCodeType { v: self.v.clone() }) + } +} diff --git a/uniffi_bindgen/src/bindings/kotlin/templates/EnumTemplate.kt b/uniffi_bindgen/src/bindings/kotlin/templates/EnumTemplate.kt index be198ac7b9..a4c0699ff9 100644 --- a/uniffi_bindgen/src/bindings/kotlin/templates/EnumTemplate.kt +++ b/uniffi_bindgen/src/bindings/kotlin/templates/EnumTemplate.kt @@ -10,7 +10,7 @@ enum class {{ type_name }} { {% for variant in e.variants() -%} - {{ variant.name()|enum_variant }}{% if loop.last %};{% else %},{% endif %} + {{ variant|variant_name }}{% if loop.last %};{% else %},{% endif %} {%- endfor %} } @@ -33,9 +33,9 @@ public object {{ e|ffi_converter_name }}: FfiConverterRustBuffer<{{ type_name }} sealed class {{ type_name }}{% if contains_object_references %}: Disposable {% endif %} { {% for variant in e.variants() -%} {% if !variant.has_fields() -%} - object {{ variant.name()|class_name }} : {{ type_name }}() + object {{ variant|enum_variant|type_name }} : {{ type_name }}() {% else -%} - data class {{ variant.name()|class_name }}( + data class {{ variant|enum_variant|type_name }}( {% for field in variant.fields() -%} val {{ field.name()|var_name }}: {{ field|type_name}}{% if loop.last %}{% else %}, {% endif %} {% endfor -%} @@ -48,7 +48,7 @@ sealed class {{ type_name }}{% if contains_object_references %}: Disposable {% e override fun destroy() { when(this) { {%- for variant in e.variants() %} - is {{ type_name }}.{{ variant.name()|class_name }} -> { + is {{ type_name }}.{{ variant|enum_variant|type_name }} -> { {%- if variant.has_fields() %} {% call kt::destroy_fields(variant) %} {% else -%} @@ -65,7 +65,7 @@ public object {{ e|ffi_converter_name }} : FfiConverterRustBuffer<{{ type_name } override fun read(buf: ByteBuffer): {{ type_name }} { return when(buf.getInt()) { {%- for variant in e.variants() %} - {{ loop.index }} -> {{ type_name }}.{{ variant.name()|class_name }}{% if variant.has_fields() %}( + {{ loop.index }} -> {{ type_name }}.{{ variant|enum_variant|type_name }}{% if variant.has_fields() %}( {% for field in variant.fields() -%} {{ field|read_fn }}(buf), {% endfor -%} @@ -77,7 +77,7 @@ public object {{ e|ffi_converter_name }} : FfiConverterRustBuffer<{{ type_name } override fun allocationSize(value: {{ type_name }}) = when(value) { {%- for variant in e.variants() %} - is {{ type_name }}.{{ variant.name()|class_name }} -> { + is {{ type_name }}.{{ variant|enum_variant|type_name }} -> { // Add the size for the Int that specifies the variant plus the size needed for all fields ( 4 @@ -92,7 +92,7 @@ public object {{ e|ffi_converter_name }} : FfiConverterRustBuffer<{{ type_name } override fun write(value: {{ type_name }}, buf: ByteBuffer) { when(value) { {%- for variant in e.variants() %} - is {{ type_name }}.{{ variant.name()|class_name }} -> { + is {{ type_name }}.{{ variant|enum_variant|type_name }} -> { buf.putInt({{ loop.index }}) {%- for field in variant.fields() %} {{ field|write_fn }}(value.{{ field.name()|var_name }}, buf) diff --git a/uniffi_bindgen/src/bindings/kotlin/templates/ErrorTemplate.kt b/uniffi_bindgen/src/bindings/kotlin/templates/ErrorTemplate.kt index 715cb19d75..0ddfe7183f 100644 --- a/uniffi_bindgen/src/bindings/kotlin/templates/ErrorTemplate.kt +++ b/uniffi_bindgen/src/bindings/kotlin/templates/ErrorTemplate.kt @@ -5,7 +5,7 @@ sealed class {{ type_name }}(message: String): Exception(message){% if contains_ // Each variant is a nested class // Flat enums carries a string error message, so no special implementation is necessary. {% for variant in e.variants() -%} - class {{ variant.name()|exception_name }}(message: String) : {{ type_name }}(message) + class {{ variant|error_variant|type_name }}(message: String) : {{ type_name }}(message) {% endfor %} companion object ErrorHandler : CallStatusErrorHandler<{{ type_name }}> { @@ -16,7 +16,7 @@ sealed class {{ type_name }}(message: String): Exception(message){% if contains_ sealed class {{ type_name }}: Exception(){% if contains_object_references %}, Disposable {% endif %} { // Each variant is a nested class {% for variant in e.variants() -%} - {%- let variant_name = variant.name()|exception_name %} + {%- let variant_name = variant|error_variant|type_name %} class {{ variant_name }}( {% for field in variant.fields() -%} val {{ field.name()|var_name }}: {{ field|type_name}}{% if loop.last %}{% else %}, {% endif %} @@ -36,7 +36,7 @@ sealed class {{ type_name }}: Exception(){% if contains_object_references %}, Di override fun destroy() { when(this) { {%- for variant in e.variants() %} - is {{ type_name }}.{{ variant.name()|exception_name }} -> { + is {{ type_name }}.{{ variant|error_variant|type_name }} -> { {%- if variant.has_fields() %} {% call kt::destroy_fields(variant) %} {% else -%} @@ -55,7 +55,7 @@ public object {{ e|ffi_converter_name }} : FfiConverterRustBuffer<{{ type_name } {% if e.is_flat() %} return when(buf.getInt()) { {%- for variant in e.variants() %} - {{ loop.index }} -> {{ type_name }}.{{ variant.name()|exception_name }}({{ Type::String.borrow()|read_fn }}(buf)) + {{ loop.index }} -> {{ type_name }}.{{ variant|error_variant|type_name }}({{ Type::String.borrow()|read_fn }}(buf)) {%- endfor %} else -> throw RuntimeException("invalid error enum value, something is very wrong!!") } @@ -63,7 +63,7 @@ public object {{ e|ffi_converter_name }} : FfiConverterRustBuffer<{{ type_name } return when(buf.getInt()) { {%- for variant in e.variants() %} - {{ loop.index }} -> {{ type_name }}.{{ variant.name()|exception_name }}({% if variant.has_fields() %} + {{ loop.index }} -> {{ type_name }}.{{ variant|error_variant|type_name }}({% if variant.has_fields() %} {% for field in variant.fields() -%} {{ field|read_fn }}(buf), {% endfor -%} @@ -80,7 +80,7 @@ public object {{ e|ffi_converter_name }} : FfiConverterRustBuffer<{{ type_name } {%- else %} return when(value) { {%- for variant in e.variants() %} - is {{ type_name }}.{{ variant.name()|exception_name }} -> ( + is {{ type_name }}.{{ variant|error_variant|type_name }} -> ( // Add the size for the Int that specifies the variant plus the size needed for all fields 4 {%- for field in variant.fields() %} @@ -95,7 +95,7 @@ public object {{ e|ffi_converter_name }} : FfiConverterRustBuffer<{{ type_name } override fun write(value: {{ type_name }}, buf: ByteBuffer) { when(value) { {%- for variant in e.variants() %} - is {{ type_name }}.{{ variant.name()|exception_name }} -> { + is {{ type_name }}.{{ variant|error_variant|type_name }} -> { buf.putInt({{ loop.index }}) {%- for field in variant.fields() %} {{ field|write_fn }}(value.{{ field.name()|var_name }}, buf) diff --git a/uniffi_bindgen/src/bindings/python/gen_python/mod.rs b/uniffi_bindgen/src/bindings/python/gen_python/mod.rs index 95f53229ff..cccbc8b5dd 100644 --- a/uniffi_bindgen/src/bindings/python/gen_python/mod.rs +++ b/uniffi_bindgen/src/bindings/python/gen_python/mod.rs @@ -266,50 +266,8 @@ fn fixup_keyword(name: String) -> String { pub struct PythonCodeOracle; impl PythonCodeOracle { - // Map `Type` instances to a `Box` for that type. - // - // There is a companion match in `templates/Types.py` which performs a similar function for the - // template code. - // - // - When adding additional types here, make sure to also add a match arm to the `Types.py` template. - // - To keep things manageable, let's try to limit ourselves to these 2 mega-matches - fn create_code_type(&self, type_: Type) -> Box { - match type_ { - Type::UInt8 => Box::new(primitives::UInt8CodeType), - Type::Int8 => Box::new(primitives::Int8CodeType), - Type::UInt16 => Box::new(primitives::UInt16CodeType), - Type::Int16 => Box::new(primitives::Int16CodeType), - Type::UInt32 => Box::new(primitives::UInt32CodeType), - Type::Int32 => Box::new(primitives::Int32CodeType), - Type::UInt64 => Box::new(primitives::UInt64CodeType), - Type::Int64 => Box::new(primitives::Int64CodeType), - Type::Float32 => Box::new(primitives::Float32CodeType), - Type::Float64 => Box::new(primitives::Float64CodeType), - Type::Boolean => Box::new(primitives::BooleanCodeType), - Type::String => Box::new(primitives::StringCodeType), - Type::Bytes => Box::new(primitives::BytesCodeType), - - Type::Timestamp => Box::new(miscellany::TimestampCodeType), - Type::Duration => Box::new(miscellany::DurationCodeType), - - Type::Enum(id) => Box::new(enum_::EnumCodeType::new(id)), - Type::Object { name, .. } => Box::new(object::ObjectCodeType::new(name)), - Type::Record(id) => Box::new(record::RecordCodeType::new(id)), - Type::Error(id) => Box::new(error::ErrorCodeType::new(id)), - Type::CallbackInterface(id) => { - Box::new(callback_interface::CallbackInterfaceCodeType::new(id)) - } - Type::ForeignExecutor => Box::new(executor::ForeignExecutorCodeType), - Type::Optional(inner) => Box::new(compounds::OptionalCodeType::new(*inner)), - Type::Sequence(inner) => Box::new(compounds::SequenceCodeType::new(*inner)), - Type::Map(key, value) => Box::new(compounds::MapCodeType::new(*key, *value)), - Type::External { name, .. } => Box::new(external::ExternalCodeType::new(name)), - Type::Custom { name, .. } => Box::new(custom::CustomCodeType::new(name)), - } - } - fn find(&self, type_: &Type) -> Box { - self.create_code_type(type_.clone()) + type_.clone().as_type().as_codetype() } /// Get the idiomatic Python rendering of a class name (for enums, records, errors, etc). @@ -365,39 +323,83 @@ impl PythonCodeOracle { } } -pub mod filters { - use super::*; +pub trait AsCodeType { + fn as_codetype(&self) -> Box; +} + +impl AsCodeType for T { + fn as_codetype(&self) -> Box { + // Map `Type` instances to a `Box` for that type. + // + // There is a companion match in `templates/Types.py` which performs a similar function for the + // template code. + // + // - When adding additional types here, make sure to also add a match arm to the `Types.py` template. + // - To keep things manageable, let's try to limit ourselves to these 2 mega-matches + match self.as_type() { + Type::UInt8 => Box::new(primitives::UInt8CodeType), + Type::Int8 => Box::new(primitives::Int8CodeType), + Type::UInt16 => Box::new(primitives::UInt16CodeType), + Type::Int16 => Box::new(primitives::Int16CodeType), + Type::UInt32 => Box::new(primitives::UInt32CodeType), + Type::Int32 => Box::new(primitives::Int32CodeType), + Type::UInt64 => Box::new(primitives::UInt64CodeType), + Type::Int64 => Box::new(primitives::Int64CodeType), + Type::Float32 => Box::new(primitives::Float32CodeType), + Type::Float64 => Box::new(primitives::Float64CodeType), + Type::Boolean => Box::new(primitives::BooleanCodeType), + Type::String => Box::new(primitives::StringCodeType), + Type::Bytes => Box::new(primitives::BytesCodeType), + + Type::Timestamp => Box::new(miscellany::TimestampCodeType), + Type::Duration => Box::new(miscellany::DurationCodeType), - fn oracle() -> &'static PythonCodeOracle { - &PythonCodeOracle + Type::Enum(id) => Box::new(enum_::EnumCodeType::new(id)), + Type::Object { name, .. } => Box::new(object::ObjectCodeType::new(name)), + Type::Record(id) => Box::new(record::RecordCodeType::new(id)), + Type::Error(id) => Box::new(error::ErrorCodeType::new(id)), + Type::CallbackInterface(id) => { + Box::new(callback_interface::CallbackInterfaceCodeType::new(id)) + } + Type::ForeignExecutor => Box::new(executor::ForeignExecutorCodeType), + Type::Optional(inner) => Box::new(compounds::OptionalCodeType::new(*inner)), + Type::Sequence(inner) => Box::new(compounds::SequenceCodeType::new(*inner)), + Type::Map(key, value) => Box::new(compounds::MapCodeType::new(*key, *value)), + Type::External { name, .. } => Box::new(external::ExternalCodeType::new(name)), + Type::Custom { name, .. } => Box::new(custom::CustomCodeType::new(name)), + } } +} + +pub mod filters { + use super::*; - pub fn type_name(as_type: &impl AsType) -> Result { - Ok(oracle().find(&as_type.as_type()).type_label()) + pub fn type_name(as_ct: &impl AsCodeType) -> Result { + Ok(as_ct.as_codetype().type_label()) } - pub fn ffi_converter_name(as_type: &impl AsType) -> Result { - Ok(oracle().find(&as_type.as_type()).ffi_converter_name()) + pub fn ffi_converter_name(as_ct: &impl AsCodeType) -> Result { + Ok(as_ct.as_codetype().ffi_converter_name()) } - pub fn canonical_name(as_type: &impl AsType) -> Result { - Ok(oracle().find(&as_type.as_type()).canonical_name()) + pub fn canonical_name(as_ct: &impl AsCodeType) -> Result { + Ok(as_ct.as_codetype().canonical_name()) } - pub fn lift_fn(as_type: &impl AsType) -> Result { - Ok(format!("{}.lift", ffi_converter_name(as_type)?)) + pub fn lift_fn(as_ct: &impl AsCodeType) -> Result { + Ok(format!("{}.lift", ffi_converter_name(as_ct)?)) } - pub fn lower_fn(as_type: &impl AsType) -> Result { - Ok(format!("{}.lower", ffi_converter_name(as_type)?)) + pub fn lower_fn(as_ct: &impl AsCodeType) -> Result { + Ok(format!("{}.lower", ffi_converter_name(as_ct)?)) } - pub fn read_fn(as_type: &impl AsType) -> Result { - Ok(format!("{}.read", ffi_converter_name(as_type)?)) + pub fn read_fn(as_ct: &impl AsCodeType) -> Result { + Ok(format!("{}.read", ffi_converter_name(as_ct)?)) } - pub fn write_fn(as_type: &impl AsType) -> Result { - Ok(format!("{}.write", ffi_converter_name(as_type)?)) + pub fn write_fn(as_ct: &impl AsCodeType) -> Result { + Ok(format!("{}.write", ffi_converter_name(as_ct)?)) } // Name of the callback function we pass to Rust to complete an async call @@ -415,8 +417,8 @@ pub mod filters { )) } - pub fn literal_py(literal: &Literal, as_type: &impl AsType) -> Result { - Ok(oracle().find(&as_type.as_type()).literal(literal)) + pub fn literal_py(literal: &Literal, as_ct: &impl AsCodeType) -> Result { + Ok(as_ct.as_codetype().literal(literal)) } /// Get the Python syntax for representing a given low-level `FfiType`. @@ -426,21 +428,21 @@ pub mod filters { /// Get the idiomatic Python rendering of a class name (for enums, records, errors, etc). pub fn class_name(nm: &str) -> Result { - Ok(oracle().class_name(nm)) + Ok(PythonCodeOracle.class_name(nm)) } /// Get the idiomatic Python rendering of a function name. pub fn fn_name(nm: &str) -> Result { - Ok(oracle().fn_name(nm)) + Ok(PythonCodeOracle.fn_name(nm)) } /// Get the idiomatic Python rendering of a variable name. pub fn var_name(nm: &str) -> Result { - Ok(oracle().var_name(nm)) + Ok(PythonCodeOracle.var_name(nm)) } /// Get the idiomatic Python rendering of an individual enum variant. pub fn enum_variant_py(nm: &str) -> Result { - Ok(oracle().enum_variant_name(nm)) + Ok(PythonCodeOracle.enum_variant_name(nm)) } } diff --git a/uniffi_bindgen/src/interface/mod.rs b/uniffi_bindgen/src/interface/mod.rs index b5085d6863..7ab90f8eeb 100644 --- a/uniffi_bindgen/src/interface/mod.rs +++ b/uniffi_bindgen/src/interface/mod.rs @@ -60,7 +60,7 @@ mod attributes; mod callbacks; pub use callbacks::CallbackInterface; mod enum_; -pub use enum_::Enum; +pub use enum_::{Enum, Variant}; mod error; pub use error::Error; mod function;