2021-12-29 16:31:53 +00:00
|
|
|
//! Utility methods for `golgi`.
|
2021-12-02 13:12:52 +00:00
|
|
|
use async_std::io::Read;
|
2021-12-29 16:31:53 +00:00
|
|
|
use std::fmt::Debug;
|
2021-12-02 13:12:52 +00:00
|
|
|
|
|
|
|
use kuska_ssb::rpc::{RecvMsg, RequestNo, RpcReader};
|
2021-12-29 16:31:53 +00:00
|
|
|
use serde_json::Value;
|
2021-12-28 19:57:30 +00:00
|
|
|
|
2021-12-02 13:12:52 +00:00
|
|
|
use crate::error::GolgiError;
|
2021-12-29 15:59:05 +00:00
|
|
|
use crate::messages::{SsbKVT, SsbMessageValue};
|
2021-12-02 13:12:52 +00:00
|
|
|
|
2021-12-28 19:57:30 +00:00
|
|
|
/// Function to parse an array of bytes (returned by an rpc call) into a KVT.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `body` - An array of u8 to be parsed.
|
2021-12-29 15:59:05 +00:00
|
|
|
pub fn kvt_res_parse(body: &[u8]) -> Result<SsbKVT, GolgiError> {
|
2021-12-29 16:31:53 +00:00
|
|
|
let value: Value = serde_json::from_slice(body)?;
|
2021-12-29 15:59:05 +00:00
|
|
|
let kvt: SsbKVT = serde_json::from_value(value)?;
|
2021-12-28 19:57:30 +00:00
|
|
|
Ok(kvt)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Function to parse an array of bytes (returned by an rpc call) into a String.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `body` - An array of u8 to be parsed.
|
2021-12-27 21:43:44 +00:00
|
|
|
pub fn string_res_parse(body: &[u8]) -> Result<String, GolgiError> {
|
|
|
|
// TODO: cleanup with proper error handling etc.
|
|
|
|
Ok(std::str::from_utf8(body).unwrap().to_string())
|
|
|
|
}
|
|
|
|
|
2021-12-28 19:57:30 +00:00
|
|
|
/// Function to parse an array of bytes (returned by an rpc call) into a serde_json::Value.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `body` - An array of u8 to be parsed.
|
2021-12-28 15:24:35 +00:00
|
|
|
pub fn json_res_parse(body: &[u8]) -> Result<Value, GolgiError> {
|
|
|
|
let message: Value = serde_json::from_slice(body)?;
|
|
|
|
Ok(message)
|
|
|
|
}
|
|
|
|
|
2021-12-28 19:57:30 +00:00
|
|
|
/// Function to parse an array of bytes (returned by an rpc call) into an SsbMessageValue
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `body` - An array of u8 to be parsed.
|
|
|
|
pub fn ssb_message_res_parse(body: &[u8]) -> Result<SsbMessageValue, GolgiError> {
|
|
|
|
let message: SsbMessageValue = serde_json::from_slice(body)?;
|
|
|
|
Ok(message)
|
|
|
|
}
|
|
|
|
|
2021-12-27 21:43:44 +00:00
|
|
|
/// Takes in an rpc request number, and a handling function,
|
|
|
|
/// and waits for an rpc response which matches the request number,
|
|
|
|
/// and then calls the handling function on the response.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `rpc_reader` - A `RpcReader` which can return Messages in a loop
|
|
|
|
/// * `req_no` - A `RequestNo` of the response to listen for
|
|
|
|
/// * `f` - A function which takes in an array of u8 and returns a Result<T, GolgiError>.
|
|
|
|
/// This is a function which parses the response from the RpcReader. T is a generic type,
|
|
|
|
/// so this parse function can return multiple possible types (String, json, custom struct etc.)
|
2021-12-02 13:12:52 +00:00
|
|
|
pub async fn get_async<'a, R, T, F>(
|
|
|
|
rpc_reader: &mut RpcReader<R>,
|
|
|
|
req_no: RequestNo,
|
|
|
|
f: F,
|
|
|
|
) -> Result<T, GolgiError>
|
|
|
|
where
|
|
|
|
R: Read + Unpin,
|
|
|
|
F: Fn(&[u8]) -> Result<T, GolgiError>,
|
|
|
|
T: Debug,
|
|
|
|
{
|
|
|
|
loop {
|
|
|
|
let (id, msg) = rpc_reader.recv().await?;
|
|
|
|
if id == req_no {
|
|
|
|
match msg {
|
|
|
|
RecvMsg::RpcResponse(_type, body) => {
|
|
|
|
return f(&body).map_err(|err| err);
|
2021-12-29 16:31:53 +00:00
|
|
|
}
|
2021-12-27 21:43:44 +00:00
|
|
|
RecvMsg::ErrorResponse(message) => {
|
|
|
|
return Err(GolgiError::Sbot(message));
|
2021-12-29 16:31:53 +00:00
|
|
|
}
|
2021-12-27 21:43:44 +00:00
|
|
|
RecvMsg::CancelStreamRespose() => {
|
2021-12-29 16:31:53 +00:00
|
|
|
return Err(GolgiError::Sbot(
|
|
|
|
"sbot returned CancelStreamResponse before any content".to_string(),
|
|
|
|
));
|
|
|
|
}
|
2021-12-28 19:57:30 +00:00
|
|
|
_ => {}
|
2021-12-27 21:43:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Takes in an rpc request number, and a handling function,
|
|
|
|
/// and calls the handling function on all RPC responses which match the request number,
|
|
|
|
/// appending the result of each parsed message to a vector,
|
|
|
|
/// until a CancelStreamResponse is found, marking the end of the stream,
|
|
|
|
/// and then finally a result is returned.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `rpc_reader` - A `RpcReader` which can return Messages in a loop
|
|
|
|
/// * `req_no` - A `RequestNo` of the response to listen for
|
|
|
|
/// * `f` - A function which takes in an array of u8 and returns a Result<T, GolgiError>.
|
|
|
|
/// This is a function which parses the response from the RpcReader. T is a generic type,
|
|
|
|
/// so this parse function can return multiple possible types (String, json, custom struct etc.)
|
|
|
|
pub async fn get_async_until_eof<'a, R, T, F>(
|
|
|
|
rpc_reader: &mut RpcReader<R>,
|
|
|
|
req_no: RequestNo,
|
|
|
|
f: F,
|
|
|
|
) -> Result<Vec<T>, GolgiError>
|
|
|
|
where
|
|
|
|
R: Read + Unpin,
|
|
|
|
F: Fn(&[u8]) -> Result<T, GolgiError>,
|
|
|
|
T: Debug,
|
|
|
|
{
|
2021-12-29 16:31:53 +00:00
|
|
|
let mut messages: Vec<T> = Vec::new();
|
2021-12-27 21:43:44 +00:00
|
|
|
loop {
|
|
|
|
let (id, msg) = rpc_reader.recv().await?;
|
|
|
|
if id == req_no {
|
|
|
|
match msg {
|
|
|
|
RecvMsg::RpcResponse(_type, body) => {
|
|
|
|
let parsed_response: Result<T, GolgiError> = f(&body);
|
|
|
|
match parsed_response {
|
|
|
|
Ok(parsed_message) => {
|
|
|
|
messages.push(parsed_message);
|
2021-12-29 16:31:53 +00:00
|
|
|
}
|
2021-12-27 21:43:44 +00:00
|
|
|
Err(err) => {
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
}
|
2021-12-02 13:12:52 +00:00
|
|
|
}
|
|
|
|
RecvMsg::ErrorResponse(message) => {
|
|
|
|
return Err(GolgiError::Sbot(message));
|
|
|
|
}
|
2021-12-27 21:43:44 +00:00
|
|
|
RecvMsg::CancelStreamRespose() => break,
|
2021-12-02 13:12:52 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-27 21:43:44 +00:00
|
|
|
Ok(messages)
|
2021-12-02 13:12:52 +00:00
|
|
|
}
|
|
|
|
|
2021-12-28 19:57:30 +00:00
|
|
|
/// Takes in an rpc request number, and a handling function,
|
|
|
|
/// and calls the handling function on all responses which match the request number,
|
|
|
|
/// and prints out the result of the handling function.
|
|
|
|
///
|
|
|
|
/// This is a function useful for debugging, and only prints the output.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `rpc_reader` - A `RpcReader` which can return Messages in a loop
|
|
|
|
/// * `req_no` - A `RequestNo` of the response to listen for
|
|
|
|
/// * `f` - A function which takes in an array of u8 and returns a Result<T, GolgiError>.
|
|
|
|
/// This is a function which parses the response from the RpcReader. T is a generic type,
|
|
|
|
/// so this parse function can return multiple possible types (String, json, custom struct etc.)
|
2021-12-02 13:12:52 +00:00
|
|
|
pub async fn print_source_until_eof<'a, R, T, F>(
|
|
|
|
rpc_reader: &mut RpcReader<R>,
|
|
|
|
req_no: RequestNo,
|
|
|
|
f: F,
|
|
|
|
) -> Result<(), GolgiError>
|
|
|
|
where
|
|
|
|
R: Read + Unpin,
|
|
|
|
F: Fn(&[u8]) -> Result<T, GolgiError>,
|
|
|
|
T: Debug + serde::Deserialize<'a>,
|
|
|
|
{
|
|
|
|
loop {
|
|
|
|
let (id, msg) = rpc_reader.recv().await?;
|
|
|
|
if id == req_no {
|
|
|
|
match msg {
|
|
|
|
RecvMsg::RpcResponse(_type, body) => {
|
|
|
|
let display = f(&body)?;
|
2021-12-28 19:57:30 +00:00
|
|
|
println!("{:?}", display);
|
2021-12-02 13:12:52 +00:00
|
|
|
}
|
|
|
|
RecvMsg::ErrorResponse(message) => {
|
|
|
|
return Err(GolgiError::Sbot(message));
|
|
|
|
}
|
2021-12-29 16:31:53 +00:00
|
|
|
RecvMsg::CancelStreamRespose() => break,
|
2021-12-02 13:12:52 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|