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

feat: implement docstring handling #33

Closed
wants to merge 2 commits into from
Closed
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
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ camino = "1.0.8"
fs-err = "2.7.0"
paste = "1.0"
serde_json = "1.0.0"
textwrap = "0.16"
9 changes: 9 additions & 0 deletions bindgen/src/gen_go/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,15 @@ pub mod filters {
pub fn enum_variant_name(nm: &str) -> Result<String, askama::Error> {
Ok(oracle().enum_variant_name(nm))
}

/// Get the idiomatic go rendering of docstring
pub fn docstring(docstring: &str, tabs: &i32) -> Result<String, askama::Error> {
let middle = textwrap::indent(&textwrap::dedent(docstring), "// ");
let wrapped = format!("{middle}");

let tabs = usize::try_from(*tabs).unwrap_or_default();
Ok(textwrap::indent(&wrapped, &"\t".repeat(tabs)))
}
}

/// Renders Go helper code for all types
Expand Down
3 changes: 3 additions & 0 deletions bindgen/templates/CallbackInterfaceTemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
{{- self.add_import("sync") }}

// Declaration and FfiConverters for {{ type_name }} Callback Interface

{%- call go::docstring(cbi, 0) %}
type {{ type_name }} interface {
{% for meth in cbi.methods() -%}
{%- call go::docstring(meth, 4) %}
{{ meth.name()|fn_name }}({% call go::arg_list_decl(meth) %}) {% call go::return_type_decl_cb(meth) %}
{% endfor %}
}
Expand Down
5 changes: 4 additions & 1 deletion bindgen/templates/EnumTemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

{% let e = ci.get_enum_definition(name).expect("missing enum") -%}
{%- if e.is_flat() -%}

{%- call go::docstring(e, 0) %}
type {{ type_name }} uint

const (
{%- for variant in e.variants() %}
{%- call go::docstring(variant, 4) %}
{{ type_name }}{{ variant.name()|enum_variant_name }} {{ type_name }} = {{ loop.index }}
{%- endfor %}
)
Expand All @@ -19,8 +20,10 @@ type {{ type_name }} interface {
}

{%- for variant in e.variants() %}
{%- call go::docstring(variant, 0) %}
type {{ type_name }}{{ variant.name()|class_name }} struct {
{%- for field in variant.fields() %}
{%- call go::docstring(field, 4) %}
{{ field.name()|field_name }} {{ field|type_name}}
{%- endfor %}
}
Expand Down
3 changes: 3 additions & 0 deletions bindgen/templates/ErrorTemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* 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/. */#}

{%- call go::docstring(e, 0) %}
type {{ type_name|class_name }} struct {
err error
}
Expand All @@ -17,12 +18,14 @@ func (err {{ type_name|class_name }}) Unwrap() error {
// Err* are used for checking error type with `errors.Is`
{%- for variant in e.variants() %}
{%- let variant_class_name = (type_name.clone() + variant.name())|class_name %}
{%- call go::docstring(variant, 0) %}
var Err{{ variant_class_name }} = fmt.Errorf("{{ variant_class_name }}")
{%- endfor %}

// Variant structs
{%- for variant in e.variants() %}
{%- let variant_class_name = (type_name.clone() + variant.name())|class_name %}
{%- call go::docstring(variant, 0) %}
type {{ variant_class_name }} struct {
{%- if e.is_flat() %}
message string
Expand Down
4 changes: 4 additions & 0 deletions bindgen/templates/ObjectTemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,29 @@
{%- let obj_name = obj.name()|class_name %}
{%- if self.include_once_check("ObjectRuntime.go") %}{% include "ObjectRuntime.go" %}{% endif %}

{%- call go::docstring(obj, 0) %}
type {{ obj_name }} struct {
ffiObject FfiObject
}

{%- match obj.primary_constructor() %}
{%- when Some with (cons) %}
{%- call go::docstring(cons, 0) %}
func New{{ obj_name }}({% call go::arg_list_decl(cons) -%}) {% call go::return_type_decl(cons) %} {
{% call go::ffi_call_binding(func, "") %}
}
{%- when None %}
{%- endmatch %}

{% for cons in obj.alternate_constructors() -%}
{%- call go::docstring(cons, 0) %}
func {{ obj_name }}{{ cons.name()|fn_name }}({% call go::arg_list_decl(cons) %}) {% call go::return_type_decl(cons) %} {
{% call go::ffi_call_binding(func, "") %}
}
{% endfor %}

{% for func in obj.methods() -%}
{%- call go::docstring(func, 0) %}
func (_self {{ type_name }}){{ func.name()|fn_name }}({%- call go::arg_list_decl(func) -%}) {% call go::return_type_decl(func) %} {
_pointer := _self.ffiObject.incrementPointer("{{ type_name }}")
defer _self.ffiObject.decrementPointer()
Expand Down
2 changes: 2 additions & 0 deletions bindgen/templates/RecordTemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

{%- let rec = ci.get_record_definition(name).expect("missing record") %}

{%- call go::docstring(rec, 0) %}
type {{ type_name }} struct {
{%- for field in rec.fields() %}
{%- call go::docstring(field, 0) %}
{{ field.name()|field_name }} {{ field|type_name -}}
{%- endfor %}
}
Expand Down
8 changes: 8 additions & 0 deletions bindgen/templates/macros.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,11 @@ RustBufferFromExternal({{ arg|lower_fn }}({{ arg.name()|var_name }}))
{%- endmacro -%}



{%- macro docstring(defn, indent_spaces) %}
{%- match defn.docstring() %}
{%- when Some(docstring) %}
{{ docstring|docstring(indent_spaces) }}
{%- else %}
{%- endmatch %}
{%- endmacro %}
2 changes: 2 additions & 0 deletions bindgen/templates/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* 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/. */#}

{%- call go::docstring(ci.namespace_definition(), 0) %}

package {{ ci.namespace() }}

// #include <{{ config.header_filename() }}>
Expand Down
28 changes: 28 additions & 0 deletions binding_tests/docstring_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* 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/. */

package binding_tests

import (
"testing"

"github.com/NordSecurity/uniffi-bindgen-go/binding_tests/generated/uniffi_docstring"
)

func TestDocstring(_ *testing.T) {
_ = uniffi_docstring.EnumTestOne
_ = uniffi_docstring.AssociatedEnumTestTest{}
_ = uniffi_docstring.NewErrorTestOne()

_ = uniffi_docstring.NewObjectTest()
obj2 := uniffi_docstring.ObjectTestNewAlternate()
obj2.Test()

rec := uniffi_docstring.RecordTest { Test: 123 }
_ = rec.Test

// class CallbackImpls: CallbackTest {
// func test() {}
// }
}
1 change: 1 addition & 0 deletions fixtures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ uniffi-fixture-simple-iface = { path = "../3rd-party/uniffi-rs/fixtures/simple-i
uniffi-fixture-trait-methods = { path = "../3rd-party/uniffi-rs/fixtures/trait-methods" }
uniffi-fixture-type-limits = { path = "../3rd-party/uniffi-rs/fixtures/type-limits" }
uniffi-fixture-time = { path = "../3rd-party/uniffi-rs/fixtures/uniffi-fixture-time" }
uniffi-fixture-docstring = { path = "../3rd-party/uniffi-rs/fixtures/docstring" }

# Go specific
uniffi-go-fixture-destroy = { path = "destroy" }
Expand Down
2 changes: 2 additions & 0 deletions fixtures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ mod uniffi_fixtures {
uniffi_trait_methods::uniffi_reexport_scaffolding!();
uniffi_type_limits::uniffi_reexport_scaffolding!();

uniffi_fixture_docstring::uniffi_reexport_scaffolding!();

// Go specific
uniffi_go_destroy::uniffi_reexport_scaffolding!();
uniffi_go_errors::uniffi_reexport_scaffolding!();
Expand Down