crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.47k stars 1.62k forks source link

Improving crystal docs for C bindings #6721

Open Heaven31415 opened 6 years ago

Heaven31415 commented 6 years ago

Right now as far as I can see, crystal docs tool won't help you in any way if you are creating a binding to some C library. It isn't possible to document fun declarations with markdown comments, which I think might be beneficial.

Would you consider adding support for documentation comments for fun declarations and other things related to bindings like C structs and enums?

asterite commented 6 years ago

You shouldn't document those (at least from an API point of view) because you are not supposed to directly expose them. You should wrap them with Crystal constructs, and document those.

straight-shoota commented 6 years ago

@asterite But sometimes it makes sense to expose parts of the C bindings (for example structs) in the public API to ensure interoperability with other other C libraries. See Socket::Address for example.

Enums in lib definitions can already be documented, by the way. The documentation from the lib enum will be used when it is aliased as Crystal type.

Heaven31415 commented 6 years ago

I agree with @straight-shoota. I'm currently working on a GLFW wrapper, which happens to have support for creating Vulkan objects and wrapping them into anything doesn't make any sense, because those functions were designed to by directly used by Vulkan API. That's why I want to have some kind of documentation for lib functions.

RX14 commented 6 years ago

It's rare that a wrapped object will have no methods. In that case you have two options:

sol-vin commented 2 years ago

You shouldn't document those (at least from an API point of view) because you are not supposed to directly expose them. You should wrap them with Crystal constructs, and document those.

I would be fine with this if macros worked for lib but they don't. Why should someone have to write out potentially hundreds of functions to wrap them? lib methods need a special "LibMethod" type in Macros that has the Crystal method name, C function name, args, and return type.

asterite commented 2 years ago

That's already the case. The only issue is that because of some semantic intricacies, they are only accessible inside methods:

def foo
  {% p LibC.class.methods %}
end

foo
straight-shoota commented 2 years ago

@sol-vin That's a related topic, but a different issue. I created #12395 to track that.

sol-vin commented 1 year ago

I'd like to bring attention to this issue again if possible. I'd really love this as a feature. Maybe being a flag like crystal docs --output-lib. Potentially also an argument for what lib/libs should be output like crystal docs --output-lib Raylib.

nobodywasishere commented 1 year ago

I think this could work well if combined with #13184 if the C Lib stuff is put in a separate tab.