mirror of
https://github.com/waku-org/waku-rust-bindings.git
synced 2025-02-19 18:08:34 +00:00
Properly decode a Vec<Multiaddr>
This commit is contained in:
parent
44ad6804ae
commit
223762d426
@ -11,6 +11,7 @@ use crate::general::Result;
|
||||
use crate::node::context::WakuNodeContext;
|
||||
use crate::utils::LibwakuResponse;
|
||||
use crate::utils::{get_trampoline, handle_json_response, handle_no_response, handle_response};
|
||||
use crate::utils::WakuDecode;
|
||||
|
||||
/// Instantiates a Waku node
|
||||
/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_newchar-jsonconfig)
|
||||
@ -101,6 +102,17 @@ pub fn waku_version(ctx: &WakuNodeContext) -> Result<String> {
|
||||
handle_response(code, result)
|
||||
}
|
||||
|
||||
// Implement WakuDecode for Vec<Multiaddr>
|
||||
impl WakuDecode for Vec<Multiaddr> {
|
||||
fn decode(input: &str) -> Result<Self> {
|
||||
input
|
||||
.split(',')
|
||||
.map(|s| s.trim().parse::<Multiaddr>().map_err(|err| err.to_string()))
|
||||
.collect::<Result<Vec<Multiaddr>>>() // Collect results into a Vec
|
||||
.map_err(|err| format!("could not parse Multiaddr: {}", err))
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the multiaddresses the Waku node is listening to
|
||||
/// as per [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_listen_addresses)
|
||||
pub fn waku_listen_addresses(ctx: &WakuNodeContext) -> Result<Vec<Multiaddr>> {
|
||||
|
@ -1,6 +1,5 @@
|
||||
use crate::general::Result;
|
||||
use core::str::FromStr;
|
||||
use serde::de::DeserializeOwned;
|
||||
use std::convert::TryFrom;
|
||||
use std::{slice, str};
|
||||
use waku_sys::WakuCallBack;
|
||||
@ -32,9 +31,13 @@ impl TryFrom<(u32, &str)> for LibwakuResponse {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decode<T: DeserializeOwned>(input: String) -> Result<T> {
|
||||
serde_json::from_str(input.as_str())
|
||||
.map_err(|err| format!("could not deserialize waku response: {}", err))
|
||||
// Define the WakuDecode trait
|
||||
pub trait WakuDecode: Sized {
|
||||
fn decode(input: &str) -> Result<Self>;
|
||||
}
|
||||
|
||||
pub fn decode<T: WakuDecode>(input: String) -> Result<T> {
|
||||
T::decode(input.as_str())
|
||||
}
|
||||
|
||||
unsafe extern "C" fn trampoline<F>(
|
||||
@ -84,7 +87,7 @@ pub fn handle_no_response(code: i32, result: LibwakuResponse) -> Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_json_response<F: DeserializeOwned>(code: i32, result: LibwakuResponse) -> Result<F> {
|
||||
pub fn handle_json_response<F: WakuDecode>(code: i32, result: LibwakuResponse) -> Result<F> {
|
||||
match result {
|
||||
LibwakuResponse::Success(v) => decode(v.unwrap_or_default()),
|
||||
LibwakuResponse::Failure(v) => Err(v),
|
||||
|
Loading…
x
Reference in New Issue
Block a user