From 4b0123a736fa23de7c7c3b591de5fcf6fab1bf63 Mon Sep 17 00:00:00 2001 From: Jan-Erik Rediger Date: Wed, 13 Dec 2023 13:57:47 +0100 Subject: [PATCH] Properly dedent doc strings after parsing in the proc-macro Fixes #1884 --- Cargo.lock | 1 + fixtures/docstring-proc-macro/src/lib.rs | 3 +++ .../docstring-proc-macro/tests/bindings/test_docstring.py | 8 +++++++- fixtures/docstring/src/docstring.udl | 3 +++ fixtures/docstring/tests/bindings/test_docstring.py | 8 +++++++- uniffi_macros/Cargo.toml | 1 + uniffi_macros/src/util.rs | 4 ++-- 7 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6c73b85452..2800a44444 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1872,6 +1872,7 @@ dependencies = [ "quote", "serde", "syn", + "textwrap", "toml", "uniffi_build", "uniffi_meta", diff --git a/fixtures/docstring-proc-macro/src/lib.rs b/fixtures/docstring-proc-macro/src/lib.rs index c8b45abc46..aafe8e5f0c 100644 --- a/fixtures/docstring-proc-macro/src/lib.rs +++ b/fixtures/docstring-proc-macro/src/lib.rs @@ -49,6 +49,9 @@ enum AssociatedErrorTest { } /// +/// +/// +/// #[derive(uniffi::Object)] pub struct ObjectTest {} diff --git a/fixtures/docstring-proc-macro/tests/bindings/test_docstring.py b/fixtures/docstring-proc-macro/tests/bindings/test_docstring.py index 9c8a1af1c3..7c2385bdbf 100644 --- a/fixtures/docstring-proc-macro/tests/bindings/test_docstring.py +++ b/fixtures/docstring-proc-macro/tests/bindings/test_docstring.py @@ -36,7 +36,13 @@ assert AssociatedErrorTest.Test2.__doc__ == "" # Test objects -assert ObjectTest.__doc__ == "" +expected_object_doc = """ + + + + + """ +assert ObjectTest.__doc__ == expected_object_doc assert ObjectTest.__init__.__doc__ == "" assert ObjectTest.new_alternate.__doc__ == "" assert ObjectTest.test.__doc__ == "" diff --git a/fixtures/docstring/src/docstring.udl b/fixtures/docstring/src/docstring.udl index 74963f9121..ffd9de2555 100644 --- a/fixtures/docstring/src/docstring.udl +++ b/fixtures/docstring/src/docstring.udl @@ -42,6 +42,9 @@ interface AssociatedErrorTest { }; /// +/// +/// +/// interface ObjectTest { /// constructor(); diff --git a/fixtures/docstring/tests/bindings/test_docstring.py b/fixtures/docstring/tests/bindings/test_docstring.py index 2a029e8a23..d08f8ca057 100644 --- a/fixtures/docstring/tests/bindings/test_docstring.py +++ b/fixtures/docstring/tests/bindings/test_docstring.py @@ -36,7 +36,13 @@ assert AssociatedErrorTest.Test2.__doc__ == "" # Test objects -assert ObjectTest.__doc__ == "" +expected_object_doc = """ + + + + + """ +assert ObjectTest.__doc__ == expected_object_doc assert ObjectTest.__init__.__doc__ == "" assert ObjectTest.new_alternate.__doc__ == "" assert ObjectTest.test.__doc__ == "" diff --git a/uniffi_macros/Cargo.toml b/uniffi_macros/Cargo.toml index 79e28300e2..557b05283d 100644 --- a/uniffi_macros/Cargo.toml +++ b/uniffi_macros/Cargo.toml @@ -23,6 +23,7 @@ proc-macro2 = "1.0" quote = "1.0" serde = "1.0.136" syn = { version = "2.0", features = ["full", "visit-mut"] } +textwrap = "0.16.0" toml = "0.5.9" uniffi_build = { path = "../uniffi_build", version = "=0.25.3" } uniffi_meta = { path = "../uniffi_meta", version = "=0.25.3" } diff --git a/uniffi_macros/src/util.rs b/uniffi_macros/src/util.rs index d419d80b70..f5d5567f40 100644 --- a/uniffi_macros/src/util.rs +++ b/uniffi_macros/src/util.rs @@ -285,11 +285,11 @@ pub(crate) fn extract_docstring(attrs: &[Attribute]) -> syn::Result { let name_value = attr.meta.require_name_value()?; if let Expr::Lit(expr) = &name_value.value { if let Lit::Str(lit_str) = &expr.lit { - return Ok(lit_str.value().trim().to_owned()); + return Ok(lit_str.value()); } } Err(syn::Error::new_spanned(attr, "Cannot parse doc attribute")) }) .collect::>>() - .map(|lines| lines.join("\n")); + .map(|lines| textwrap::dedent(&lines.join("\n"))); }