1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//! CJDNS Admin lib

#![deny(missing_docs)]

extern crate cjdns_bencode as bencode;

pub use crate::config::Opts;
pub use crate::conn::Connection;
pub use crate::errors::Error;
pub use crate::func_args::{ArgName, ArgValue, ArgValues};
pub use crate::func_list::{Arg, ArgType, Args, Func, Funcs};
pub use crate::func_ret::ReturnValue;

mod config;
mod conn;
mod errors;
mod func_args;
mod func_list;
mod func_ret;
pub mod msgs;
mod txid;

#[derive(Clone, Default, PartialEq, Eq, Debug)]
struct ConnectionOptions {
    addr: String,
    port: u16,
    password: String,
    used_config_file: Option<String>,
}

/// Connect to the running cjdns router instance.
/// If `opts` is not provided, the default config file is read.
/// or only specified config file name,
/// the corresponding config file is read.
pub async fn connect(opts: Option<Opts>) -> Result<Connection, Error> {
    let opts = opts.unwrap_or_default().into_connection_options().await?;
    conn::Connection::new(opts).await
}

/// Helper macro to easily invoke remote function with arguments.
///
/// Examples:
/// ```no_run
/// # use cjdns_admin::cjdns_invoke;
/// # async fn test() -> Result<(), Box<dyn std::error::Error>> {
/// # let mut conn = cjdns_admin::connect(None).await?;
/// let res = cjdns_invoke!(conn, "FuncName").await?;
/// let res = cjdns_invoke!(conn, "FuncName", "arg1" = 42, "arg2" = "foobar").await?;
/// # Ok(())}
/// ```
#[macro_export]
macro_rules! cjdns_invoke {
    ($cjdns:expr, $fn_name:literal) => {
        $cjdns.invoke::<_, $crate::msgs::GenericResponsePayload>($fn_name, $crate::ArgValues::new())
    };
    ($cjdns:expr, $fn_name:literal, $( $arg_name:literal = $arg_value:expr ),*) => {
        $cjdns.invoke::<_, $crate::msgs::GenericResponsePayload>($fn_name, $crate::ArgValues::new() $( .add($arg_name, $arg_value) )*)
    };
}