mirror of
https://github.com/logos-messaging/logos-delivery.git
synced 2026-07-20 11:40:02 +00:00
nim-ffi 0.2.0 reshapes the authoring model: `.ffi.` procs take the library value plus typed params instead of threading (ctx, callback, userData) by hand, the macro validates the context itself, and payloads ride the wire as CBOR rather than ad-hoc JSON strings. The old idiom no longer compiles against it, so the whole surface moves at once. Proc names are camelCase chosen so the generated snake_case export matches the previous C symbol exactly (wakuRelayPublish -> waku_relay_publish), keeping the ABI names stable. Node lifecycle now uses the dedicated pragmas: `.ffiCtor.` for create_node (LogosDelivery.new already returns the Future[Result[...]] the contract wants) and `.ffiDtor.` for destroy. Contexts come from the macro-emitted FFIContextPool, which caps live contexts at 32. Events become typed `.ffiEvent.` procs over `.ffi.` payload objects. The payloads carry wire-friendly scalars rather than the domain types, which are not serialisable; byte fields stay base64. This is what makes the generated bindings emit typed listeners instead of leaving consumers to register by name and parse JSON themselves. The payload fields are deliberately unexported. genBindings copies field names verbatim, so an export marker leaks into the generated Rust as `pub payload*: String` and the file does not parse -- a nim-ffi bug (it strips the marker from type names but not fields, so its single-file examples never hit it). Construction therefore lives behind the emit* procs in declare_lib, which also keeps event emission in one place and collapses each listener body to a single call. `requireInitializedNode` is gone: the macro rejects a null/invalid ctx before the handler runs, so all 14 call sites were redundant. Relay and filter push handlers are declared `raises: [Defect]`, so the emit call is wrapped explicitly -- the dispatch path no longer guards the body for us. genBindings() emits the C/C++/Rust bindings and must stay last in the compilation root; it is a no-op without -d:ffiGenBindings. The Rust output is checked in so consumers can vendor it directly. Known gaps, tracked separately: the hand-written liblogosdelivery.h / _kernel.h still declare the pre-CBOR signatures and need generating or dropping, and nimble resolves cbor_serialization 0.4.0 while the lock and nim-ffi both pin 0.3.0. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
45 lines
1.5 KiB
Nim
45 lines
1.5 KiB
Nim
import std/[json, strutils]
|
|
import logos_delivery/waku/factory/waku_state_info
|
|
import tools/confutils/[cli_args, config_option_meta]
|
|
|
|
proc logosdeliveryGetAvailableNodeInfoIds*(
|
|
lib: LogosDelivery
|
|
): Future[Result[string, string]] {.ffi.} =
|
|
## Returns the list of all available node info item ids that
|
|
## can be queried with `get_node_info_item`.
|
|
return ok($lib.waku.stateInfo.getAllPossibleInfoItemIds())
|
|
|
|
proc logosdeliveryGetNodeInfo*(
|
|
lib: LogosDelivery, nodeInfoId: string
|
|
): Future[Result[string, string]] {.ffi.} =
|
|
## Returns the content of the node info item with the given id if it exists.
|
|
let infoItemIdEnum =
|
|
try:
|
|
parseEnum[NodeInfoId](nodeInfoId)
|
|
except ValueError:
|
|
return err("Invalid node info id: " & nodeInfoId)
|
|
|
|
return ok(lib.waku.stateInfo.getNodeInfoItem(infoItemIdEnum))
|
|
|
|
proc logosdeliveryGetAvailableConfigs*(
|
|
lib: LogosDelivery
|
|
): Future[Result[string, string]] {.ffi.} =
|
|
## Returns information about the accepted config items.
|
|
let optionMetas: seq[ConfigOptionMeta] = extractConfigOptionMeta(WakuNodeConf)
|
|
var configOptionDetails = newJArray()
|
|
|
|
# for confField, confValue in fieldPairs(conf):
|
|
# defaultConfig[confField] = $confValue
|
|
|
|
for meta in optionMetas:
|
|
configOptionDetails.add(
|
|
%*{
|
|
meta.fieldName: meta.typeName & "(" & meta.defaultValue & ")", "desc": meta.desc
|
|
}
|
|
)
|
|
|
|
var jsonNode = newJObject()
|
|
jsonNode["configOptions"] = configOptionDetails
|
|
let asString = pretty(jsonNode)
|
|
return ok(pretty(jsonNode))
|