Skip to content

Commit ddd0abc

Browse files
author
Alex Kordys
committed
Merge remote-tracking branch 'origin/debugging' into master2
2 parents 79ccef3 + 1643c8e commit ddd0abc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+896
-1034
lines changed

cjdns-admin/src/bin/cjdnsadmin.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{env, path};
55
use anyhow::Error;
66
use regex::Regex;
77

8-
use cjdns_admin::{ArgValue, ArgValues, Func, msgs::GenericResponsePayload};
8+
use cjdns_admin::{msgs::GenericResponsePayload, ArgValue, ArgValues, Func};
99

1010
#[tokio::main]
1111
async fn main() {
@@ -104,8 +104,14 @@ fn test_parse_remote_fn_args() -> Result<(), ()> {
104104
assert_eq!(parse_remote_fn_args(r#" 42"#)?, vec![ArgValue::Int(42)]);
105105
assert_eq!(parse_remote_fn_args(r#"42 "#)?, vec![ArgValue::Int(42)]);
106106
assert_eq!(parse_remote_fn_args(r#""foo""#)?, vec![ArgValue::String("foo".to_string())]);
107-
assert_eq!(parse_remote_fn_args(r#"42,"foo",-42"#)?, vec![ArgValue::Int(42), ArgValue::String("foo".to_string()), ArgValue::Int(-42)]);
108-
assert_eq!(parse_remote_fn_args(r#"42, "foo", -42"#)?, vec![ArgValue::Int(42), ArgValue::String("foo".to_string()), ArgValue::Int(-42)]);
107+
assert_eq!(
108+
parse_remote_fn_args(r#"42,"foo",-42"#)?,
109+
vec![ArgValue::Int(42), ArgValue::String("foo".to_string()), ArgValue::Int(-42)]
110+
);
111+
assert_eq!(
112+
parse_remote_fn_args(r#"42, "foo", -42"#)?,
113+
vec![ArgValue::Int(42), ArgValue::String("foo".to_string()), ArgValue::Int(-42)]
114+
);
109115

110116
Ok(())
111117
}
@@ -118,4 +124,4 @@ fn make_args(func: &Func, arg_values: Vec<ArgValue>) -> ArgValues {
118124
args.add(arg.name.clone(), arg_value);
119125
}
120126
args
121-
}
127+
}

cjdns-admin/src/config.rs

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use std::path::{Path, PathBuf};
55
use serde::Deserialize;
66
use tokio::{fs, io};
77

8-
use crate::ConnectionOptions;
98
use crate::errors::Error;
9+
use crate::ConnectionOptions;
1010

1111
const DEFAULT_ADDR: &'static str = "127.0.0.1";
1212
const DEFAULT_PORT: u16 = 11234;
@@ -64,7 +64,11 @@ impl Opts {
6464
ConnectionOptions {
6565
addr: self.addr.as_ref().map_or(DEFAULT_ADDR, |s| &s).to_string(),
6666
port: self.port.unwrap_or(DEFAULT_PORT),
67-
password: self.password.as_ref().map_or_else(|| if self.anon { "" } else { DEFAULT_PASSWORD }, |s| &s).to_string(),
67+
password: self
68+
.password
69+
.as_ref()
70+
.map_or_else(|| if self.anon { "" } else { DEFAULT_PASSWORD }, |s| &s)
71+
.to_string(),
6872
used_config_file: conf_file.map(|path| path.to_string_lossy().into_owned()),
6973
}
7074
}
@@ -126,7 +130,11 @@ fn test_build_connection_options() {
126130
);
127131

128132
assert_eq!(
129-
Opts { addr: ss("192.168.1.1"), ..Opts::default() }.build_connection_options(None),
133+
Opts {
134+
addr: ss("192.168.1.1"),
135+
..Opts::default()
136+
}
137+
.build_connection_options(None),
130138
ConnectionOptions {
131139
addr: s("192.168.1.1"),
132140
port: 11234,
@@ -136,7 +144,11 @@ fn test_build_connection_options() {
136144
);
137145

138146
assert_eq!(
139-
Opts { port: Some(1234), ..Opts::default() }.build_connection_options(None),
147+
Opts {
148+
port: Some(1234),
149+
..Opts::default()
150+
}
151+
.build_connection_options(None),
140152
ConnectionOptions {
141153
addr: s("127.0.0.1"),
142154
port: 1234,
@@ -146,7 +158,11 @@ fn test_build_connection_options() {
146158
);
147159

148160
assert_eq!(
149-
Opts { password: ss("secret"), ..Opts::default() }.build_connection_options(None),
161+
Opts {
162+
password: ss("secret"),
163+
..Opts::default()
164+
}
165+
.build_connection_options(None),
150166
ConnectionOptions {
151167
addr: s("127.0.0.1"),
152168
port: 11234,
@@ -165,12 +181,35 @@ fn test_parse_config() {
165181

166182
assert_eq!(c(r#"{ "unknown": "foo" }"#), Opts::default());
167183

168-
assert_eq!(c(r#"{ "addr": "192.168.1.1" }"#), Opts { addr: s("192.168.1.1"), ..Opts::default() });
169-
assert_eq!(c(r#"{ "port": 1234 }"#), Opts { port: Some(1234), ..Opts::default() });
170-
assert_eq!(c(r#"{ "password": "secret" }"#), Opts { password: s("secret"), ..Opts::default() });
184+
assert_eq!(
185+
c(r#"{ "addr": "192.168.1.1" }"#),
186+
Opts {
187+
addr: s("192.168.1.1"),
188+
..Opts::default()
189+
}
190+
);
191+
assert_eq!(
192+
c(r#"{ "port": 1234 }"#),
193+
Opts {
194+
port: Some(1234),
195+
..Opts::default()
196+
}
197+
);
198+
assert_eq!(
199+
c(r#"{ "password": "secret" }"#),
200+
Opts {
201+
password: s("secret"),
202+
..Opts::default()
203+
}
204+
);
171205

172206
assert_eq!(
173207
c(r#"{ "addr": "192.168.1.1", "port": 1234, "password": "secret" }"#),
174-
Opts { addr: s("192.168.1.1"), port: Some(1234), password: s("secret"), ..Opts::default() }
208+
Opts {
209+
addr: s("192.168.1.1"),
210+
port: Some(1234),
211+
password: s("secret"),
212+
..Opts::default()
213+
}
175214
);
176215
}

cjdns-admin/src/conn.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
//! UDP connection to the CJDNS Router.
22
33
use std::net::{IpAddr, SocketAddr};
4-
use std::time::Duration;
54
use std::sync::Arc;
5+
use std::time::Duration;
66

77
use sodiumoxide::crypto::hash::sha256::hash;
88
use tokio::net::UdpSocket;
9-
use tokio::time;
109
use tokio::sync::Mutex;
10+
use tokio::time;
1111

12-
use crate::ConnectionOptions;
1312
use crate::errors::{ConnOptions, Error};
1413
use crate::func_list::Funcs;
1514
use crate::msgs::{self, Empty, Request};
1615
use crate::txid::Counter;
16+
use crate::ConnectionOptions;
1717

1818
const PING_TIMEOUT: Duration = Duration::from_millis(1_000);
1919
const DEFAULT_TIMEOUT: Duration = Duration::from_millis(10_000);
@@ -48,10 +48,14 @@ impl Connection {
4848
}
4949

5050
async fn probe_connection(&mut self, opts: ConnectionOptions) -> Result<(), Error> {
51-
self.call_func::<(), Empty>("ping", (), true, PING_TIMEOUT).await.map_err(|_| Error::ConnectError(ConnOptions::wrap(&opts)))?;
51+
self.call_func::<(), Empty>("ping", (), true, PING_TIMEOUT)
52+
.await
53+
.map_err(|_| Error::ConnectError(ConnOptions::wrap(&opts)))?;
5254

5355
if !self.password.is_empty() {
54-
self.call_func::<(), Empty>("AuthorizedPasswords_list", (), false, DEFAULT_TIMEOUT).await.map_err(|_| Error::AuthError(ConnOptions::wrap(&opts)))?;
56+
self.call_func::<(), Empty>("AuthorizedPasswords_list", (), false, DEFAULT_TIMEOUT)
57+
.await
58+
.map_err(|_| Error::AuthError(ConnOptions::wrap(&opts)))?;
5559
}
5660

5761
Ok(())
@@ -61,7 +65,9 @@ impl Connection {
6165
let mut res = Funcs::new();
6266

6367
for i in 0.. {
64-
let ret: msgs::AvailableFnsResponsePayload = self.call_func("Admin_availableFunctions", msgs::AvailableFnsQueryArg { page: i }, false, DEFAULT_TIMEOUT).await?;
68+
let ret: msgs::AvailableFnsResponsePayload = self
69+
.call_func("Admin_availableFunctions", msgs::AvailableFnsQueryArg { page: i }, false, DEFAULT_TIMEOUT)
70+
.await?;
6571
let funcs = ret.available_fns;
6672

6773
if funcs.is_empty() {
@@ -163,8 +169,9 @@ impl Connection {
163169
}
164170

165171
async fn send_msg<RQ, RS>(&mut self, req: &RQ) -> Result<RS, Error>
166-
where RQ: msgs::Request,
167-
RS: msgs::Response
172+
where
173+
RQ: msgs::Request,
174+
RS: msgs::Response,
168175
{
169176
// Send encoded request
170177
let msg = req.to_bencode()?;
@@ -201,7 +208,10 @@ fn check_txid(sent_txid: &String, received_txid: &String) -> Result<(), Error> {
201208
if sent_txid == received_txid {
202209
Ok(())
203210
} else {
204-
Err(Error::BrokenTx { sent_txid: sent_txid.clone(), received_txid: received_txid.clone() })
211+
Err(Error::BrokenTx {
212+
sent_txid: sent_txid.clone(),
213+
received_txid: received_txid.clone(),
214+
})
205215
}
206216
}
207217

@@ -212,4 +222,4 @@ fn check_remote_error(remote_error_msg: &str) -> Result<(), Error> {
212222
} else {
213223
Err(Error::RemoteError(remote_error_msg.to_string()))
214224
}
215-
}
225+
}

cjdns-admin/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,4 @@ pub enum Error {
7373
/// Network timeout error.
7474
#[error("Timeout occured: {0:?}")]
7575
TimeOut(Duration),
76-
}
76+
}

cjdns-admin/src/func_args.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
use std::collections::BTreeMap;
44

5-
use serde::{Serialize, Serializer};
65
use serde::ser::SerializeMap;
6+
use serde::{Serialize, Serializer};
77

88
/// Argument name (alias to `String`).
99
pub type ArgName = String;
@@ -87,11 +87,13 @@ impl From<&str> for ArgValue {
8787

8888
#[test]
8989
fn test_arg_value_conversion() {
90-
fn arg<T: Into<ArgValue>>(v: T) -> ArgValue { v.into() }
90+
fn arg<T: Into<ArgValue>>(v: T) -> ArgValue {
91+
v.into()
92+
}
9193

9294
assert_eq!(arg(42), ArgValue::Int(42));
9395
assert_eq!(arg(-42), ArgValue::Int(-42));
9496

9597
assert_eq!(arg("foo"), ArgValue::String("foo".to_string()));
9698
assert_eq!(arg("bar".to_string()), ArgValue::String("bar".to_string()));
97-
}
99+
}

cjdns-admin/src/func_list.rs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl Funcs {
7878

7979
/// Iterator over functions in this list returned in alphabetical order.
8080
#[inline]
81-
pub fn iter(&self) -> impl Iterator<Item=&Func> {
81+
pub fn iter(&self) -> impl Iterator<Item = &Func> {
8282
let Funcs(list) = self;
8383
list.iter()
8484
}
@@ -95,7 +95,7 @@ impl Args {
9595
/// Iterator over arguments in this list.
9696
/// Returns required args first in alphabetical order, then non-required in alphabetical order.
9797
#[inline]
98-
pub fn iter(&self) -> impl Iterator<Item=&Arg> {
98+
pub fn iter(&self) -> impl Iterator<Item = &Arg> {
9999
let Args(list) = self;
100100
list.iter()
101101
}
@@ -170,9 +170,7 @@ mod tests {
170170
("fn_a", mk_args(vec![])),
171171
("fn_c", mk_args(vec![("arg1", true, "Int"), ("arg2", true, "String")])),
172172
]);
173-
let fns2 = mk_funcs(vec![
174-
("fn_b", mk_args(vec![("c", false, "Int"), ("b", true, "Int"), ("a", false, "Int")])),
175-
]);
173+
let fns2 = mk_funcs(vec![("fn_b", mk_args(vec![("c", false, "Int"), ("b", true, "Int"), ("a", false, "Int")]))]);
176174

177175
let mut funcs = Funcs::new();
178176
funcs.add_funcs(fns1);
@@ -181,20 +179,27 @@ mod tests {
181179
funcs
182180
};
183181

184-
let a = |nm: &str, req: bool, typ: ArgType| {
185-
Arg {
186-
name: nm.to_string(),
187-
required: req,
188-
typ,
189-
}
182+
let a = |nm: &str, req: bool, typ: ArgType| Arg {
183+
name: nm.to_string(),
184+
required: req,
185+
typ,
190186
};
191187

192188
assert_eq!(
193189
funcs,
194190
Funcs(vec![
195-
Func { name: "fn_a".to_string(), args: Args(vec![]) },
196-
Func { name: "fn_b".to_string(), args: Args(vec![a("b", true, ArgType::Int), a("a", false, ArgType::Int), a("c", false, ArgType::Int)]) },
197-
Func { name: "fn_c".to_string(), args: Args(vec![a("arg1", true, ArgType::Int), a("arg2", true, ArgType::String)]) },
191+
Func {
192+
name: "fn_a".to_string(),
193+
args: Args(vec![])
194+
},
195+
Func {
196+
name: "fn_b".to_string(),
197+
args: Args(vec![a("b", true, ArgType::Int), a("a", false, ArgType::Int), a("c", false, ArgType::Int)])
198+
},
199+
Func {
200+
name: "fn_c".to_string(),
201+
args: Args(vec![a("arg1", true, ArgType::Int), a("arg2", true, ArgType::String)])
202+
},
198203
])
199204
);
200205
}
@@ -210,9 +215,12 @@ mod tests {
210215
fn mk_args(list: Vec<(&str, bool, &str)>) -> RemoteFnArgsDescr {
211216
let mut res = BTreeMap::new();
212217
for (name, req, typ) in list {
213-
let descr = RemoteFnArgDescr { required: if req { 1 } else { 0 }, typ: typ.to_string() };
218+
let descr = RemoteFnArgDescr {
219+
required: if req { 1 } else { 0 },
220+
typ: typ.to_string(),
221+
};
214222
res.insert(name.to_string(), descr);
215223
}
216224
res
217225
}
218-
}
226+
}

cjdns-admin/src/func_ret.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,23 @@ impl ReturnValue {
3535
/// Access stored List value, converting each list element.
3636
/// Returns a new `Vec` where each element is converted from another `ReturnValue` to the appropriate type.
3737
pub fn as_list<'rv, T, F>(&'rv self, mut item_convert: F) -> Result<Vec<T>, ()>
38-
where F: FnMut(&'rv ReturnValue) -> Result<T, ()>
38+
where
39+
F: FnMut(&'rv ReturnValue) -> Result<T, ()>,
3940
{
4041
match self {
41-
ReturnValue::List(list) => {
42-
list.iter().map(|v| item_convert(v)).collect()
43-
}
42+
ReturnValue::List(list) => list.iter().map(|v| item_convert(v)).collect(),
4443
_ => Err(()),
4544
}
4645
}
4746

4847
/// Access stored Map value, converting each entry value element.
4948
/// Returns a new `BTreeMap` where each key is `String` and each value is converted from another `ReturnValue` to the appropriate type.
5049
pub fn as_map<'rv, T, F>(&'rv self, mut value_convert: F) -> Result<BTreeMap<String, T>, ()>
51-
where F: FnMut(&'rv ReturnValue) -> Result<T, ()>
50+
where
51+
F: FnMut(&'rv ReturnValue) -> Result<T, ()>,
5252
{
5353
match self {
54-
ReturnValue::Map(map) => {
55-
map.iter().map(|(k, v)| value_convert(v).map(|v| (k.clone(), v))).collect()
56-
}
54+
ReturnValue::Map(map) => map.iter().map(|(k, v)| value_convert(v).map(|v| (k.clone(), v))).collect(),
5755
_ => Err(()),
5856
}
5957
}
@@ -120,8 +118,8 @@ mod deserialize {
120118
use std::collections::BTreeMap;
121119
use std::fmt;
122120

123-
use serde::{Deserialize, Deserializer};
124121
use serde::de::{Error, MapAccess, SeqAccess, Unexpected, Visitor};
122+
use serde::{Deserialize, Deserializer};
125123

126124
use super::ReturnValue;
127125

@@ -194,4 +192,4 @@ mod debug {
194192
}
195193
}
196194
}
197-
}
195+
}

0 commit comments

Comments
 (0)