Skip to content

Commit ac0104c

Browse files
author
Ivo Georgiev
authored
Merge branch 'dev' into adapter-safety
2 parents af5e3e5 + 29aa2ca commit ac0104c

File tree

7 files changed

+187
-188
lines changed

7 files changed

+187
-188
lines changed

primitives/src/channel.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ pub struct ChannelSpec {
7070
}
7171

7272
#[derive(Serialize, Deserialize, Debug, Clone)]
73-
#[serde(transparent)]
74-
pub struct SpecValidators([ValidatorDesc; 2]);
73+
/// A (leader, follower) tuple
74+
pub struct SpecValidators(ValidatorDesc, ValidatorDesc);
7575

7676
pub enum SpecValidator<'a> {
7777
Leader(&'a ValidatorDesc),
@@ -94,15 +94,15 @@ impl<'a> SpecValidator<'a> {
9494

9595
impl SpecValidators {
9696
pub fn new(leader: ValidatorDesc, follower: ValidatorDesc) -> Self {
97-
Self([leader, follower])
97+
Self(leader, follower)
9898
}
9999

100100
pub fn leader(&self) -> &ValidatorDesc {
101-
&self.0[0]
101+
&self.0
102102
}
103103

104104
pub fn follower(&self) -> &ValidatorDesc {
105-
&self.0[1]
105+
&self.1
106106
}
107107

108108
pub fn find(&self, validator_id: &ValidatorId) -> SpecValidator<'_> {
@@ -116,12 +116,13 @@ impl SpecValidators {
116116
}
117117
}
118118

119-
impl From<[ValidatorDesc; 2]> for SpecValidators {
120-
fn from(slice: [ValidatorDesc; 2]) -> Self {
121-
Self(slice)
119+
impl From<(ValidatorDesc, ValidatorDesc)> for SpecValidators {
120+
fn from((leader, follower): (ValidatorDesc, ValidatorDesc)) -> Self {
121+
Self(leader, follower)
122122
}
123123
}
124124

125+
/// Fixed size iterator of 2, as we need an iterator in couple of occasions
125126
impl<'a> IntoIterator for &'a SpecValidators {
126127
type Item = &'a ValidatorDesc;
127128
type IntoIter = ::std::vec::IntoIter<Self::Item>;

primitives/src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@ pub mod market_channel;
1515
pub mod merkle_tree;
1616
pub mod sentry;
1717
pub mod targeting_tag;
18-
pub mod util;
18+
pub mod util {
19+
pub mod tests {
20+
pub mod prep_db;
21+
pub mod time;
22+
}
23+
24+
pub mod logging;
25+
pub mod serde;
26+
}
1927
pub mod validator;
2028

2129
pub use self::ad_unit::AdUnit;

primitives/src/util.rs

Lines changed: 0 additions & 174 deletions
This file was deleted.

primitives/src/util/logging.rs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
use slog::{Drain, OwnedKVList, Record, KV};
2+
use slog_term::{
3+
timestamp_local, CompactFormatSerializer, CountingWriter, Decorator, RecordDecorator,
4+
Serializer, ThreadSafeTimestampFn,
5+
};
6+
use std::cell::RefCell;
7+
use std::{io, io::Write};
8+
9+
pub use slog_async::Async;
10+
pub use slog_term::TermDecorator;
11+
12+
pub struct PrefixedCompactFormat<D>
13+
where
14+
D: Decorator,
15+
{
16+
decorator: D,
17+
history: RefCell<Vec<(Vec<u8>, Vec<u8>)>>,
18+
fn_timestamp: Box<dyn ThreadSafeTimestampFn<Output = io::Result<()>>>,
19+
prefix: String,
20+
}
21+
22+
impl<D> Drain for PrefixedCompactFormat<D>
23+
where
24+
D: Decorator,
25+
{
26+
type Ok = ();
27+
type Err = io::Error;
28+
29+
fn log(&self, record: &Record<'_>, values: &OwnedKVList) -> Result<Self::Ok, Self::Err> {
30+
self.format_compact(record, values)
31+
}
32+
}
33+
34+
impl<D> PrefixedCompactFormat<D>
35+
where
36+
D: Decorator,
37+
{
38+
pub fn new(prefix: &str, d: D) -> PrefixedCompactFormat<D> {
39+
Self {
40+
fn_timestamp: Box::new(timestamp_local),
41+
decorator: d,
42+
history: RefCell::new(vec![]),
43+
prefix: prefix.to_owned(),
44+
}
45+
}
46+
47+
fn format_compact(&self, record: &Record<'_>, values: &OwnedKVList) -> io::Result<()> {
48+
self.decorator.with_record(record, values, |decorator| {
49+
let indent = {
50+
let mut history_ref = self.history.borrow_mut();
51+
let mut serializer = CompactFormatSerializer::new(decorator, &mut *history_ref);
52+
53+
values.serialize(record, &mut serializer)?;
54+
55+
serializer.finish()?
56+
};
57+
58+
decorator.start_whitespace()?;
59+
60+
for _ in 0..indent {
61+
write!(decorator, " ")?;
62+
}
63+
64+
let comma_needed =
65+
print_msg_header(&self.prefix, &*self.fn_timestamp, decorator, record)?;
66+
{
67+
let mut serializer = Serializer::new(decorator, comma_needed, false);
68+
69+
record.kv().serialize(record, &mut serializer)?;
70+
71+
serializer.finish()?;
72+
}
73+
74+
decorator.start_whitespace()?;
75+
writeln!(decorator)?;
76+
77+
decorator.flush()?;
78+
79+
Ok(())
80+
})
81+
}
82+
}
83+
84+
pub fn print_msg_header(
85+
prefix: &str,
86+
fn_timestamp: &dyn ThreadSafeTimestampFn<Output = io::Result<()>>,
87+
mut rd: &mut dyn RecordDecorator,
88+
record: &Record<'_>,
89+
) -> io::Result<bool> {
90+
rd.start_timestamp()?;
91+
fn_timestamp(&mut rd)?;
92+
93+
rd.start_whitespace()?;
94+
write!(rd, " ")?;
95+
96+
rd.start_level()?;
97+
write!(rd, "{}", record.level().as_short_str())?;
98+
99+
rd.start_whitespace()?;
100+
write!(rd, " ")?;
101+
102+
rd.start_msg()?;
103+
write!(rd, "{}:", prefix)?;
104+
105+
rd.start_whitespace()?;
106+
write!(rd, " ")?;
107+
108+
rd.start_msg()?;
109+
let mut count_rd = CountingWriter::new(&mut rd);
110+
write!(count_rd, "{}", record.msg())?;
111+
Ok(count_rd.count() != 0)
112+
}

0 commit comments

Comments
 (0)