Skip to content

Commit

Permalink
Add feature to transfer docblocks from Rust public functions to gener…
Browse files Browse the repository at this point in the history
…ated Dart functions
  • Loading branch information
jerel committed Sep 15, 2023
1 parent a43db90 commit 7596ca8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
4 changes: 4 additions & 0 deletions example/src/application/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ pub fn contacts() -> impl Stream<Item = Result<data::Contact, data::Error>> {
futures::stream::iter(vec![Ok(data::Contact::default())])
}

///
/// This is a docblock that was written in Rust and
/// will be added to the generated Dart code.
///
#[async_dart(namespace = "accounts")]
pub async fn contact(user_id: String) -> Result<data::Contact, data::Error> {
println!("async {:?}", thread::current().id());
Expand Down
2 changes: 2 additions & 0 deletions membrane/src/generators/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ trait Callable {
impl Callable for Ffi {
fn begin(&mut self) -> &mut Self {
self.output += &self.fun.begin();
self.output += self.fun.docblock;
self
}

Expand Down Expand Up @@ -434,6 +435,7 @@ impl Callable for Ffi {
impl Callable for Web {
fn begin(&mut self) -> &mut Self {
self.output += &self.fun.begin();
self.output += self.fun.docblock;
self
}

Expand Down
1 change: 1 addition & 0 deletions membrane/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ pub struct Function {
pub dart_transforms: &'static str,
pub dart_inner_args: &'static str,
pub location: SourceCodeLocation,
pub docblock: &'static str,
}

#[doc(hidden)]
Expand Down
18 changes: 16 additions & 2 deletions membrane/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,25 @@ mod test {

let api = read_to_string(path.join("lib/src").join("accounts_ffi.dart")).unwrap();
assert!(api.contains("@immutable\nclass AccountsApi {"));
assert!(api.contains("Future<Contact> contact({required String userId}) async {"));
assert_contains_part(
&api,
"///
/// This is a docblock that was written in Rust and
/// will be added to the generated Dart code.
///
Future<Contact> contact({required String userId}) async {",
);

let web_api = read_to_string(path.join("lib/src").join("accounts_web.dart")).unwrap();
assert!(web_api.contains("@immutable\nclass AccountsApi {"));
assert!(web_api.contains("Future<Contact> contact({required String userId}) async {"));
assert_contains_part(
&web_api,
"///
/// This is a docblock that was written in Rust and
/// will be added to the generated Dart code.
///
Future<Contact> contact({required String userId}) async {",
);

let dart_type = read_to_string(
path
Expand Down
20 changes: 20 additions & 0 deletions membrane_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use membrane_types::c::CHeaderTypes;
use membrane_types::dart::{DartArgs, DartParams, DartTransforms};
use membrane_types::heck::ToLowerCamelCase;
use membrane_types::rust::{flatten_types, RustArgs, RustExternParams, RustTransforms};
use membrane_types::syn::Attribute;
use membrane_types::{proc_macro2, quote, syn, Input, OutputStyle};
use options::{extract_options, Options};
use proc_macro::TokenStream;
Expand All @@ -23,12 +24,28 @@ struct ReprDart {
output_style: OutputStyle,
output: syn::Type,
error: syn::Type,
docblock: String,
}

impl Parse for ReprDart {
fn parse(input: ParseStream) -> Result<Self> {
let arg_buffer;

let docblock = input
.call(Attribute::parse_outer)?
.iter()
.map(|x| Ok(x.meta.require_name_value()?))
.map(|x: Result<&MetaNameValue>| Ok(x?.value.clone()))
.filter_map(|x: Result<syn::Expr>| match x {
Ok(syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Str(comment),
..
})) => Some(Ok(format!("/// {}\n", &comment.value()))),
_ => None,
})
.collect::<Result<Vec<String>>>()?
.join("");

input.parse::<Token![pub]>()?;
if input.peek(Token![async]) {
input.parse::<Token![async]>()?;
Expand All @@ -50,6 +67,7 @@ impl Parse for ReprDart {
output_style,
output: ret_type,
error: err_type,
docblock,
})
}
}
Expand Down Expand Up @@ -144,6 +162,7 @@ fn to_token_stream(
output,
error,
inputs,
docblock,
..
} = repr_dart;

Expand Down Expand Up @@ -367,6 +386,7 @@ fn to_token_stream(
dart_inner_args: #dart_inner_args,
output: "",
location: #debug_location,
docblock: #docblock,
},
namespace: #namespace,
trace: |
Expand Down

0 comments on commit 7596ca8

Please sign in to comment.