Skip to content

Commit

Permalink
Improve the auto-generated bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesWrigley committed Jan 5, 2024
1 parent 98d24b3 commit 98d9b43
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
19 changes: 14 additions & 5 deletions gen/gen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function rewrite!(ctx)
if name in string_functions
wrapper = quote
if ret == C_NULL
return nothing
throw(LibSSHException($("Error from $name, no string found (returned C_NULL)")))
else
return unsafe_string(Ptr{UInt8}(ret))
end
Expand All @@ -119,10 +119,19 @@ function rewrite!(ctx)

if !isnothing(wrapper)
wrapper_name = Symbol(chopprefix(string(name), "ssh_"))
new_expr = quote
function $wrapper_name($(args...))::$ret_type
ret = $name($(args...))
$wrapper
new_expr = if isnothing(ret_type)
quote
function $wrapper_name($(args...))
ret = $name($(args...))
$wrapper
end
end
else
quote
function $wrapper_name($(args...))::$ret_type
ret = $name($(args...))
$wrapper
end
end
end

Expand Down
4 changes: 4 additions & 0 deletions gen/prologue.jl
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
using libssh_jll
using DocStringExtensions

struct LibSSHException <: Exception
msg::String
end
6 changes: 1 addition & 5 deletions src/LibSSH.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@ using DocStringExtensions

include("bindings.jl")
using .lib
import .lib: ssh_options_get, ssh_options_set, SSH_OK, SSH_ERROR, SSH_AGAIN, SSH_EOF
import .lib: LibSSHException, ssh_options_get, ssh_options_set, SSH_OK, SSH_ERROR, SSH_AGAIN, SSH_EOF


struct LibSSHException <: Exception
msg::String
end

@enum AuthMethod begin
AuthMethod_Unknown = SSH_AUTH_METHOD_UNKNOWN
AuthMethod_None = SSH_AUTH_METHOD_NONE
Expand Down
8 changes: 6 additions & 2 deletions src/bindings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ using CEnum
using libssh_jll
using DocStringExtensions

struct LibSSHException <: Exception
msg::String
end


const __uid_t = Cuint

Expand Down Expand Up @@ -4902,7 +4906,7 @@ Auto-generated wrapper around [`ssh_message_auth_user`](@ref).
function message_auth_user(msg)::String
ret = ssh_message_auth_user(msg)
if ret == C_NULL
return nothing
throw(LibSSHException("Error from ssh_message_auth_user, no string found (returned C_NULL)"))
else
return unsafe_string(Ptr{UInt8}(ret))
end
Expand All @@ -4916,7 +4920,7 @@ Auto-generated wrapper around [`ssh_message_auth_password`](@ref).
function message_auth_password(msg)::String
ret = ssh_message_auth_password(msg)
if ret == C_NULL
return nothing
throw(LibSSHException("Error from ssh_message_auth_password, no string found (returned C_NULL)"))
else
return unsafe_string(Ptr{UInt8}(ret))
end
Expand Down

0 comments on commit 98d9b43

Please sign in to comment.