Merge pull request #38 from TheBlueMatt/main
[ldk-c-bindings] / c-bindings-gen / src / blocks.rs
index 643a8be7c3c1fdb5052dde93872d3b14456c3e95..9c404411a2719d4b979e8b920d1f0ef516e7b3c8 100644 (file)
@@ -377,8 +377,34 @@ pub fn write_option_block<W: std::io::Write>(w: &mut W, mangled_container: &str,
        }
 }
 
+/// Prints the docs from a given attribute list unless its tagged no export
+pub fn writeln_fn_docs<'a, W: std::io::Write, I>(w: &mut W, attrs: &[syn::Attribute], prefix: &str, types: &mut TypeResolver, generics: Option<&GenericTypes>, args: I, ret: &syn::ReturnType) where I: Iterator<Item = &'a syn::FnArg> {
+       writeln_docs_impl(w, attrs, prefix, Some((types, generics,
+               args.filter_map(|arg| if let syn::FnArg::Typed(ty) = arg {
+                               if let syn::Pat::Ident(id) = &*ty.pat {
+                                       Some((id.ident.to_string(), &*ty.ty))
+                               } else { unimplemented!() }
+                       } else { None }),
+               if let syn::ReturnType::Type(_, ty) = ret { Some(&**ty) } else { None },
+               None
+       )));
+}
+
 /// Prints the docs from a given attribute list unless its tagged no export
 pub fn writeln_docs<W: std::io::Write>(w: &mut W, attrs: &[syn::Attribute], prefix: &str) {
+       writeln_docs_impl(w, attrs, prefix, None::<(_, _, std::vec::Drain<'_, (String, &syn::Type)>, _, _)>);
+}
+
+pub fn writeln_arg_docs<'a, W: std::io::Write, I>(w: &mut W, attrs: &[syn::Attribute], prefix: &str, types: &mut TypeResolver, generics: Option<&GenericTypes>, args: I, ret: Option<&syn::Type>) where I: Iterator<Item = (String, &'a syn::Type)> {
+       writeln_docs_impl(w, attrs, prefix, Some((types, generics, args, ret, None)))
+}
+
+pub fn writeln_field_docs<W: std::io::Write>(w: &mut W, attrs: &[syn::Attribute], prefix: &str, types: &mut TypeResolver, generics: Option<&GenericTypes>, field: &syn::Type) {
+       writeln_docs_impl(w, attrs, prefix, Some((types, generics, vec![].drain(..), None, Some(field))))
+}
+
+/// Prints the docs from a given attribute list unless its tagged no export
+fn writeln_docs_impl<'a, W: std::io::Write, I>(w: &mut W, attrs: &[syn::Attribute], prefix: &str, method_args_ret: Option<(&mut TypeResolver, Option<&GenericTypes>, I, Option<&syn::Type>, Option<&syn::Type>)>) where I: Iterator<Item = (String, &'a syn::Type)> {
        for attr in attrs.iter() {
                let tokens_clone = attr.tokens.clone();
                let mut token_iter = tokens_clone.into_iter();
@@ -414,6 +440,51 @@ pub fn writeln_docs<W: std::io::Write>(w: &mut W, attrs: &[syn::Attribute], pref
                        },
                }
        }
+       if let Some((types, generics, inp, outp, field)) = method_args_ret {
+               let mut nullable_found = false;
+               for (name, inp) in inp {
+                       if types.skip_arg(inp, generics) { continue; }
+                       if if let syn::Type::Reference(syn::TypeReference { elem, .. }) = inp {
+                               if let syn::Type::Path(syn::TypePath { ref path, .. }) = &**elem {
+                                       types.is_path_transparent_container(path, generics, true)
+                               } else { false }
+                       } else if let syn::Type::Path(syn::TypePath { ref path, .. }) = inp {
+                               types.is_path_transparent_container(path, generics, true)
+                       } else { false } {
+                               // Note downstream clients match this text exactly so any changes may require
+                               // changes in the Java and Swift bindings, at least.
+                               if !nullable_found { writeln!(w, "{}///", prefix).unwrap(); }
+                               nullable_found = true;
+                               writeln!(w, "{}/// Note that {} (or a relevant inner pointer) may be NULL or all-0s to represent None", prefix, name).unwrap();
+                       }
+               }
+               if if let Some(syn::Type::Reference(syn::TypeReference { elem, .. })) = outp {
+                       if let syn::Type::Path(syn::TypePath { ref path, .. }) = &**elem {
+                               types.is_path_transparent_container(path, generics, true)
+                       } else { false }
+               } else if let Some(syn::Type::Path(syn::TypePath { ref path, .. })) = outp {
+                       types.is_path_transparent_container(path, generics, true)
+               } else { false } {
+                       // Note downstream clients match this text exactly so any changes may require
+                       // changes in the Java and Swift bindings, at least.
+                       if !nullable_found { writeln!(w, "{}///", prefix).unwrap(); }
+                       nullable_found = true;
+                       writeln!(w, "{}/// Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None", prefix).unwrap();
+               }
+               if if let Some(syn::Type::Reference(syn::TypeReference { elem, .. })) = field {
+                       if let syn::Type::Path(syn::TypePath { ref path, .. }) = &**elem {
+                               types.is_path_transparent_container(path, generics, true)
+                       } else { false }
+               } else if let Some(syn::Type::Path(syn::TypePath { ref path, .. })) = field {
+                       types.is_path_transparent_container(path, generics, true)
+               } else { false } {
+                       // Note downstream clients match this text exactly so any changes may require
+                       // changes in the Java and Swift bindings, at least.
+                       if !nullable_found { writeln!(w, "{}///", prefix).unwrap(); }
+                       writeln!(w, "{}/// Note that this (or a relevant inner pointer) may be NULL or all-0s to represent None", prefix).unwrap();
+               }
+       }
+
 }
 
 /// Print the parameters in a method declaration, starting after the open parenthesis, through and