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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use crate::prelude::*;
pub trait IpAddress: Debug {
fn with_port(&self, port: u16) -> SocketAddr;
}
impl<A> IpAddress for A where A: Into<IpAddr> + Debug + Clone {
fn with_port(&self, port: u16) -> SocketAddr {
match (self.clone().into(), port)
.to_socket_addrs()
.map(|i| i.at_most_one()) {
Ok(Ok(Some(addr))) => addr,
x => panic!("{:?},{} gave {:?}", self, port, x),
}
}
}
#[derive(Debug,Copy,Clone,Eq,PartialEq,Ord,PartialOrd)]
pub struct TomlQuote<'s>(pub &'s str);
impl<'s> Display for TomlQuote<'s> {
#[throws(fmt::Error)]
fn fmt(&self, f: &mut fmt::Formatter) {
for c in self.0.chars() {
match c {
'"' | '\\'=> write!(f, "\\{}", c)?,
c if (c < ' ' && c != '\t') || c == '\x7f' => {
write!(f, r#"\u{:04x}"#, c as u32).unwrap();
continue;
}
c => write!(f, "{}", c)?,
}
}
}
}
#[test]
fn toml_quote_string_test(){
assert_eq!(TomlQuote(r#"w \ " ƒ."#).to_string(),
r#"w \\ \" \u0007\u007fƒ."#);
}
pub fn toml_merge<'u,
S: 'u + AsRef<str>,
KV: IntoIterator<Item=(&'u S, &'u toml::Value)>
>(
table: &mut toml::value::Table,
updates: KV,
) {
use toml::value::{Table, Value};
type TME<'e> = toml::map::Entry<'e>;
let mut kv = updates.into_iter().map(|(k, v)| (k.as_ref(), v));
inner(table, &mut kv);
fn inner<'u>(
table: &mut Table,
updates: &'u mut dyn Iterator<Item=(&'u str, &'u Value)>
) {
for (k, v) in updates {
let e = table.entry(k);
match e {
TME::Vacant(ve) => {
ve.insert(v.clone());
}
TME::Occupied(mut oe) => match (oe.get_mut(), v) {
(Value::Table(old), Value::Table(new)) => {
toml_merge(old, new);
}
(Value::Array(old), Value::Array(new)) => {
old.extend(new.iter().cloned());
}
(old, new) => {
*old = new.clone();
}
}
}
}
}
}
#[derive(Copy,Clone,Debug,Serialize,Deserialize,Eq,Ord,PartialEq,PartialOrd)]
#[serde(transparent)]
pub struct Timestamp(pub u64);
impl Timestamp {
pub fn now() -> Timestamp {
use std::time::SystemTime;
let now = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs();
Timestamp(now)
}
pub fn render(&self, tz: &Timezone) -> String {
tz.format(*self)
}
}
paste!{
#[cfg(debug_assertions)]
pub fn [<x x x>]<T>() -> T { panic!("todo item triggered") }
}
#[macro_export]
macro_rules! trace_dbg {
($msg:expr $(,$val:expr)*) => {
if log_enabled!(log::Level::Trace) {
#[allow(unused_mut)]
let mut buf = format!("{}", &$msg);
$( write!(&mut buf, " {}={:?}", stringify!($val), &$val).unwrap(); )*
trace!("{}", buf);
}
}
}
#[macro_export]
macro_rules! matches_doesnot_yn2bool {
(=) => (true);
(!) => (false);
}
#[macro_export]
macro_rules! matches_doesnot {
($v:expr,
$(
$yn:tt $p:pat
),* $(,)?
) => {
match $v {
$(
$p => $crate::matches_doesnot_yn2bool!($yn),
)*
}
}
}
#[test]
fn matches_doesnot_test() {
assert!(
matches_doesnot!(
Some(42),
= Some(_),
! None
)
);
assert!(
matches_doesnot!(
Some(42),
! None,
! Some(3),
= Some(_),
)
);
assert!(
matches_doesnot!(
Some(1),
= Some(1) | Some(2),
! Some(_) | None
)
);
assert!(
! matches_doesnot!(
Some(1),
! Some(1) | Some(2),
= Some(_) | None
)
);
}