From 0d39d41326d3175133318acfe90d5f7caca3a11b Mon Sep 17 00:00:00 2001 From: Luca Campobasso Date: Thu, 26 Oct 2023 12:59:47 +0200 Subject: [PATCH] fixes --- .../src/bindings/kotlin/gen_kotlin/mod.rs | 3 +++ .../bindings/kotlin/templates/Interface.kt | 1 + .../kotlin/templates/ObjectTemplate.kt | 1 + .../src/bindings/python/gen_python/mod.rs | 3 +++ .../src/bindings/ruby/gen_ruby/mod.rs | 4 ++++ .../src/bindings/swift/gen_swift/mod.rs | 4 ++++ .../swift/templates/ObjectTemplate.swift | 2 +- .../bindings/swift/templates/Protocol.swift | 19 ++++++++++--------- uniffi_bindgen/src/lib.rs | 19 ++++++++++++++----- 9 files changed, 41 insertions(+), 15 deletions(-) diff --git a/uniffi_bindgen/src/bindings/kotlin/gen_kotlin/mod.rs b/uniffi_bindgen/src/bindings/kotlin/gen_kotlin/mod.rs index ef2cccfebd..a5707b3b5a 100644 --- a/uniffi_bindgen/src/bindings/kotlin/gen_kotlin/mod.rs +++ b/uniffi_bindgen/src/bindings/kotlin/gen_kotlin/mod.rs @@ -64,8 +64,11 @@ impl Config { } } } +use crate::Utf8Path; impl BindingsConfig for Config { + fn update_documentation(&mut self, _ci: &mut ComponentInterface, _udl_file: &Utf8Path) -> Result<()> { Ok(()) } + fn update_from_ci(&mut self, ci: &ComponentInterface) { self.package_name .get_or_insert_with(|| format!("uniffi.{}", ci.namespace())); diff --git a/uniffi_bindgen/src/bindings/kotlin/templates/Interface.kt b/uniffi_bindgen/src/bindings/kotlin/templates/Interface.kt index 2de84c102a..f1c4209bce 100644 --- a/uniffi_bindgen/src/bindings/kotlin/templates/Interface.kt +++ b/uniffi_bindgen/src/bindings/kotlin/templates/Interface.kt @@ -1,5 +1,6 @@ public interface {{ interface_name }} { {% for meth in methods.iter() -%} + {%- let func = meth -%} {%- include "FunctionDocsTemplate.kt" -%} {% if meth.is_async() -%}suspend {% endif -%} fun {{ meth.name()|fn_name }}({% call kt::arg_list_decl(meth) %}) diff --git a/uniffi_bindgen/src/bindings/kotlin/templates/ObjectTemplate.kt b/uniffi_bindgen/src/bindings/kotlin/templates/ObjectTemplate.kt index ea1ca62ab7..0fd2e85a1e 100644 --- a/uniffi_bindgen/src/bindings/kotlin/templates/ObjectTemplate.kt +++ b/uniffi_bindgen/src/bindings/kotlin/templates/ObjectTemplate.kt @@ -89,6 +89,7 @@ class {{ impl_class_name }}( {% if !obj.alternate_constructors().is_empty() -%} companion object { {% for cons in obj.alternate_constructors() -%} + {%- let func = cons -%} {%- include "FunctionDocsTemplate.kt" %} fun {{ cons.name()|fn_name }}({% call kt::arg_list_decl(cons) %}): {{ impl_class_name }} = {{ impl_class_name }}({% call kt::to_ffi_call(cons) %}) diff --git a/uniffi_bindgen/src/bindings/python/gen_python/mod.rs b/uniffi_bindgen/src/bindings/python/gen_python/mod.rs index 9199bbefd0..b9bb472c35 100644 --- a/uniffi_bindgen/src/bindings/python/gen_python/mod.rs +++ b/uniffi_bindgen/src/bindings/python/gen_python/mod.rs @@ -107,8 +107,11 @@ impl Config { } } } +use crate::Utf8Path; impl BindingsConfig for Config { + fn update_documentation(&mut self, _ci: &mut ComponentInterface, _udl_file: &Utf8Path) -> Result<()> { Ok(()) } + fn update_from_ci(&mut self, ci: &ComponentInterface) { self.cdylib_name .get_or_insert_with(|| format!("uniffi_{}", ci.namespace())); diff --git a/uniffi_bindgen/src/bindings/ruby/gen_ruby/mod.rs b/uniffi_bindgen/src/bindings/ruby/gen_ruby/mod.rs index e6defe65cc..1dc33fb69d 100644 --- a/uniffi_bindgen/src/bindings/ruby/gen_ruby/mod.rs +++ b/uniffi_bindgen/src/bindings/ruby/gen_ruby/mod.rs @@ -103,7 +103,11 @@ impl Config { } } +use crate::Utf8Path; + impl BindingsConfig for Config { + fn update_documentation(&mut self, _ci: &mut ComponentInterface, _udl_file: &Utf8Path) -> Result<()> { Ok(()) } + fn update_from_ci(&mut self, ci: &ComponentInterface) { self.cdylib_name .get_or_insert_with(|| format!("uniffi_{}", ci.namespace())); diff --git a/uniffi_bindgen/src/bindings/swift/gen_swift/mod.rs b/uniffi_bindgen/src/bindings/swift/gen_swift/mod.rs index 0bab0cf52e..6b08fc23a7 100644 --- a/uniffi_bindgen/src/bindings/swift/gen_swift/mod.rs +++ b/uniffi_bindgen/src/bindings/swift/gen_swift/mod.rs @@ -203,7 +203,11 @@ impl Config { } } +use crate::Utf8Path; + impl BindingsConfig for Config { + fn update_documentation(&mut self, _ci: &mut ComponentInterface, _udl_file: &Utf8Path) -> Result<()> { Ok(()) } + fn update_from_ci(&mut self, ci: &ComponentInterface) { self.module_name .get_or_insert_with(|| ci.namespace().into()); diff --git a/uniffi_bindgen/src/bindings/swift/templates/ObjectTemplate.swift b/uniffi_bindgen/src/bindings/swift/templates/ObjectTemplate.swift index af28d6f142..5c4dcb5b0b 100644 --- a/uniffi_bindgen/src/bindings/swift/templates/ObjectTemplate.swift +++ b/uniffi_bindgen/src/bindings/swift/templates/ObjectTemplate.swift @@ -29,7 +29,7 @@ public class {{ impl_class_name }}: {{ protocol_name }} { } {% for cons in obj.alternate_constructors() %} - + {%- let func = cons -%} {%- include "FunctionDocsTemplate.swift" %} public static func {{ cons.name()|fn_name }}({% call swift::arg_list_decl(cons) %}) {% call swift::throws(cons) %} -> {{ impl_class_name }} { return {{ impl_class_name }}(unsafeFromRawPointer: {% call swift::to_ffi_call(cons) %}) diff --git a/uniffi_bindgen/src/bindings/swift/templates/Protocol.swift b/uniffi_bindgen/src/bindings/swift/templates/Protocol.swift index d2541c8674..7e1bac8f0f 100644 --- a/uniffi_bindgen/src/bindings/swift/templates/Protocol.swift +++ b/uniffi_bindgen/src/bindings/swift/templates/Protocol.swift @@ -1,10 +1,11 @@ -public protocol {{ protocol_name }}: AnyObject { - { % for meth in methods.iter() -% } - { %- include "FunctionDocsTemplate.swift" % } - func {{ meth.name() | fn_name }}({ % call swift:: arg_list_protocol(meth) % }) { % call swift:: async(meth) -% } { % call swift:: throws (meth) -% } - { %- match meth.return_type() -% } - { %- when Some with(return_type) % } -> {{ return_type | type_name - }} - { %- else -% } - { %- endmatch % } - { % endfor % } +public protocol {{ protocol_name }} : AnyObject { + {% for meth in methods.iter() -%} + {%- let func = meth -%} + {%- include "FunctionDocsTemplate.swift" %} + func {{ meth.name()|fn_name }}({% call swift::arg_list_protocol(meth) %}) {% call swift::async(meth) -%}{% call swift::throws(meth) -%} + {%- match meth.return_type() -%} + {%- when Some with (return_type) %} -> {{ return_type|type_name -}} + {%- else -%} + {%- endmatch %} + {% endfor %} } diff --git a/uniffi_bindgen/src/lib.rs b/uniffi_bindgen/src/lib.rs index a05d82e580..67b89e2104 100644 --- a/uniffi_bindgen/src/lib.rs +++ b/uniffi_bindgen/src/lib.rs @@ -116,6 +116,9 @@ use scaffolding::RustScaffolding; /// BindingsConfigs are initially loaded from `uniffi.toml` file. Then the trait methods are used /// to fill in missing values. pub trait BindingsConfig: DeserializeOwned { + /// attaches documentation if set to do so + fn update_documentation(&mut self, ci: &mut ComponentInterface, udl_file: &Utf8Path) -> Result<()>; + /// Update missing values using the `ComponentInterface` fn update_from_ci(&mut self, ci: &ComponentInterface); @@ -136,6 +139,7 @@ pub trait BindingsConfig: DeserializeOwned { pub struct EmptyBindingsConfig; impl BindingsConfig for EmptyBindingsConfig { + fn update_documentation(&mut self, ci: &mut ComponentInterface, udl_file: &Utf8Path) -> Result<()> { Ok(()) } fn update_from_ci(&mut self, _ci: &ComponentInterface) {} fn update_from_cdylib_name(&mut self, _cdylib_name: &str) {} fn update_from_dependency_configs(&mut self, _config_map: HashMap<&str, &Self>) {} @@ -241,11 +245,7 @@ pub fn generate_external_bindings( let mut config = load_initial_config::(crate_root, config_file_override)?; config.update_from_ci(&component); - if config.bindings.doc_comments.unwrap_or_default() { - let path = udl_file.with_file_name("lib.rs"); - let documentation = uniffi_docs::extract_documentation_from_path(path)?; - component.attach_documentation(documentation); - }; + let _ = config.update_documentation(&mut component, udl_file.as_ref())?; if let Some(ref library_file) = library_file { if let Some(cdylib_name) = crate::library_mode::calc_cdylib_name(library_file.as_ref()) @@ -449,6 +449,15 @@ pub struct Config { } impl BindingsConfig for Config { + fn update_documentation(&mut self, ci: &mut ComponentInterface, udl_file: &Utf8Path) -> Result<()> { + if self.bindings.doc_comments.unwrap_or_default() { + let path = udl_file.with_file_name("lib.rs"); + let documentation = uniffi_docs::extract_documentation_from_path(path)?; + ci.attach_documentation(documentation); + } + Ok(()) + } + fn update_from_ci(&mut self, ci: &ComponentInterface) { self.bindings.kotlin.update_from_ci(ci); self.bindings.swift.update_from_ci(ci);