Skip to content

Commit 24e847b

Browse files
committed
Update formatting
1 parent 1da62e3 commit 24e847b

File tree

6 files changed

+62
-123
lines changed

6 files changed

+62
-123
lines changed

rustfmt.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
edition = "2021"
2+
max_width = 132
3+
use_field_init_shorthand = true
4+
use_try_shorthand = true
5+
6+
#unstable_features = true
7+
#fn_single_line = true
8+
#format_code_in_doc_comments = true
9+
#format_macro_matchers = true
10+
#format_strings = true
11+
#group_imports = "StdExternalCrate"
12+
#hex_literal_case = "Upper"
13+
#imports_layout = "HorizontalVertical"
14+
#imports_granularity = "Module"
15+
#reorder_impl_items = true
16+
#version = "Two"
17+
#wrap_comments = true

src/configuration.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,15 @@ where
8585
};
8686

8787
let connection_string = if let Some(cs_name) = connection_string.strip_prefix('@') {
88-
let content = std::fs::read(cs_name)
89-
.with_context(|| format!("Failed to read connection string from file {}", cs_name))?;
90-
let content = std::str::from_utf8(&content)
91-
.with_context(|| format!("Broken connection string encoding in file {}", cs_name))?;
92-
content
93-
.strip_prefix('\u{FEFF}')
94-
.unwrap_or(content)
95-
.trim()
96-
.to_owned()
88+
let content = std::fs::read(cs_name).with_context(|| format!("Failed to read connection string from file {}", cs_name))?;
89+
let content =
90+
std::str::from_utf8(&content).with_context(|| format!("Broken connection string encoding in file {}", cs_name))?;
91+
content.strip_prefix('\u{FEFF}').unwrap_or(content).trim().to_owned()
9792
} else {
9893
connection_string
9994
};
10095

101-
let options = mongodb::options::ClientOptions::parse(&connection_string)
102-
.context("Failed to parse connection string")?;
96+
let options = mongodb::options::ClientOptions::parse(&connection_string).context("Failed to parse connection string")?;
10397

10498
let db_name = connection_string
10599
.find("://")

src/data_model.rs

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ impl TryFrom<&str> for Record {
7070
fn try_from(value: &str) -> Result<Self, Self::Error> {
7171
const FILTER_REGEX_PATTERN: &str = r"^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z) (\S+?):(\d{1,5}) (\S+?):(\d{1,5}) (\S*) ([0-9a-fA-F]{1,4}) ([0-9a-fA-F]{64}) ([0-9a-fA-F]{64}) ([0-9a-fA-F]{16,})$";
7272
lazy_static! {
73-
static ref FILTER_REGEX: Regex =
74-
Regex::new(FILTER_REGEX_PATTERN).expect("Failed to parse filter regex");
73+
static ref FILTER_REGEX: Regex = Regex::new(FILTER_REGEX_PATTERN).expect("Failed to parse filter regex");
7574
}
7675

7776
let captures = FILTER_REGEX
@@ -82,33 +81,24 @@ impl TryFrom<&str> for Record {
8281
.with_context(|| format!("Invalid timestamp {}", timestamp))?
8382
.with_timezone(&Utc);
8483
let client_ip = &captures[2];
85-
let client_ip = IpAddr::from_str(client_ip)
86-
.with_context(|| format!("Invalid client IP address {}", client_ip))?;
84+
let client_ip = IpAddr::from_str(client_ip).with_context(|| format!("Invalid client IP address {}", client_ip))?;
8785
let client_port = &captures[3];
88-
let client_port = u16::from_str(client_port)
89-
.with_context(|| format!("Invalid client port {}", client_port))?;
86+
let client_port = u16::from_str(client_port).with_context(|| format!("Invalid client port {}", client_port))?;
9087
let server_ip = &captures[4];
91-
let server_ip = IpAddr::from_str(server_ip)
92-
.with_context(|| format!("Invalid server IP address {}", server_ip))?;
88+
let server_ip = IpAddr::from_str(server_ip).with_context(|| format!("Invalid server IP address {}", server_ip))?;
9389
let server_port = &captures[5];
94-
let server_port = u16::from_str(server_port)
95-
.with_context(|| format!("Invalid server port {}", server_port))?;
90+
let server_port = u16::from_str(server_port).with_context(|| format!("Invalid server port {}", server_port))?;
9691
let sni = &captures[6];
9792
let cipher_id = &captures[7];
98-
let cipher_id = u16::from_str_radix(cipher_id, 16)
99-
.with_context(|| format!("Invalid cipher id {}", cipher_id))?;
93+
let cipher_id = u16::from_str_radix(cipher_id, 16).with_context(|| format!("Invalid cipher id {}", cipher_id))?;
10094
let server_random = &captures[8];
101-
let server_random = hex::decode(server_random)
102-
.with_context(|| format!("Invalid server random {}", server_random))?;
95+
let server_random = hex::decode(server_random).with_context(|| format!("Invalid server random {}", server_random))?;
10396
let client_random = &captures[9];
104-
let client_random = hex::decode(client_random)
105-
.with_context(|| format!("Invalid client random {}", client_random))?;
97+
let client_random = hex::decode(client_random).with_context(|| format!("Invalid client random {}", client_random))?;
10698
let premaster = &captures[10];
107-
let premaster = hex::decode(premaster)
108-
.with_context(|| format!("Invalid premaster secret {}", premaster))?;
99+
let premaster = hex::decode(premaster).with_context(|| format!("Invalid premaster secret {}", premaster))?;
109100

110-
let sni = parse_sni(sni, server_ip, server_port)
111-
.with_context(|| format!("Invalid SNI {} in line {}", sni, value));
101+
let sni = parse_sni(sni, server_ip, server_port).with_context(|| format!("Invalid SNI {} in line {}", sni, value));
112102
let sni = match sni {
113103
Ok(v) => v,
114104
Err(e) => {
@@ -159,21 +149,11 @@ fn parse_sni(sni: &str, server_ip: IpAddr, server_port: u16) -> Result<String> {
159149

160150
Ok(match url.host() {
161151
Some(Host::Ipv4(a)) => {
162-
ensure!(
163-
a == server_ip,
164-
"Mismatching IPv4 address, expected {}, got {}",
165-
server_ip,
166-
a
167-
);
152+
ensure!(a == server_ip, "Mismatching IPv4 address, expected {}, got {}", server_ip, a);
168153
""
169154
}
170155
Some(Host::Ipv6(a)) => {
171-
ensure!(
172-
a == server_ip,
173-
"Mismatching IPv6 address, expected {}, got {}",
174-
server_ip,
175-
a
176-
);
156+
ensure!(a == server_ip, "Mismatching IPv6 address, expected {}, got {}", server_ip, a);
177157
""
178158
}
179159
Some(Host::Domain(d)) => d.trim_end_matches('.'),
@@ -219,9 +199,6 @@ mod test {
219199

220200
#[test]
221201
fn parse_sni_doesnt_fail_on_invalid_implicit_port() {
222-
assert_eq!(
223-
"just-a-host.com",
224-
parse_sni_test("just-a-host.com", "127.0.0.1", 80,)
225-
);
202+
assert_eq!("just-a-host.com", parse_sni_test("just-a-host.com", "127.0.0.1", 80,));
226203
}
227204
}

src/process.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,16 @@ use anyhow::{Context, Result};
44

55
use crate::{configuration, geolocator, processor, storage};
66

7-
pub(crate) fn process(
8-
args: &configuration::Configuration,
9-
term_token: &Arc<AtomicBool>,
10-
) -> Result<()> {
7+
pub(crate) fn process(args: &configuration::Configuration, term_token: &Arc<AtomicBool>) -> Result<()> {
118
let db = mongodb::sync::Client::with_options(args.options.clone())?.database(&args.db_name);
129
let mut store = storage::Store::new(&db);
1310
let geolocator = args
1411
.geodb_path
1512
.as_ref()
1613
.map(|path| {
17-
geolocator::Geolocator::new(path).with_context(|| {
18-
format!("Failed to create geolocator with database path {:?}", path)
19-
})
14+
geolocator::Geolocator::new(path).with_context(|| format!("Failed to create geolocator with database path {:?}", path))
2015
})
2116
.transpose()?;
22-
let mut context = processor::Processor::new(
23-
args.sni_filter.as_ref(),
24-
term_token,
25-
&mut store,
26-
geolocator.as_ref(),
27-
);
17+
let mut context = processor::Processor::new(args.sni_filter.as_ref(), term_token, &mut store, geolocator.as_ref());
2818
context.process(&args.files)
2919
}

src/processor.rs

Lines changed: 18 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ impl<'a> Processor<'a> {
6666
}
6767
}
6868

69-
failure
70-
.map(|f| bail!(f.context("Failed to process files")))
71-
.unwrap_or(Ok(()))
69+
failure.map(|f| bail!(f.context("Failed to process files"))).unwrap_or(Ok(()))
7270
}
7371

7472
fn process_entry(&mut self, path: &std::path::Path) -> Result<()> {
@@ -88,23 +86,17 @@ impl<'a> Processor<'a> {
8886
let file_name = &path.display();
8987

9088
println!("{}: open", file_name);
91-
let file = std::fs::File::open(path)
92-
.with_context(|| format!("Failed to open file {}", file_name))?;
89+
let file = std::fs::File::open(path).with_context(|| format!("Failed to open file {}", file_name))?;
9390
let lines = std::io::BufReader::new(file).lines();
9491
self.process_lines(lines, file_name)?;
9592

96-
std::fs::remove_file(&path)
97-
.with_context(|| format!("Failed to remove file {}", path.display()))?;
93+
std::fs::remove_file(&path).with_context(|| format!("Failed to remove file {}", path.display()))?;
9894

9995
println!("{}: done", file_name);
10096
Ok(())
10197
}
10298

103-
fn process_lines<Lines, Line, Error>(
104-
&mut self,
105-
lines: Lines,
106-
file_name: &impl std::fmt::Display,
107-
) -> Result<()>
99+
fn process_lines<Lines, Line, Error>(&mut self, lines: Lines, file_name: &impl std::fmt::Display) -> Result<()>
108100
where
109101
Lines: IntoIterator<Item = Result<Line, Error>>,
110102
Line: AsRef<str>,
@@ -115,16 +107,10 @@ impl<'a> Processor<'a> {
115107
let mut failure = None;
116108
for line in lines {
117109
line_num += 1;
118-
let location = FileLocation {
119-
file_name,
120-
line_num,
121-
};
110+
let location = FileLocation { file_name, line_num };
122111

123112
if self.term_token.load(Ordering::Relaxed) {
124-
bail!(errors::TerminatedError::new(format!(
125-
"processing {}",
126-
location
127-
)));
113+
bail!(errors::TerminatedError::new(format!("processing {}", location)));
128114
}
129115

130116
if let Err(f) = self.process_line(&location, line, &mut batch_map) {
@@ -137,20 +123,14 @@ impl<'a> Processor<'a> {
137123

138124
for (collection_name, batch) in batch_map {
139125
if self.term_token.load(Ordering::Relaxed) {
140-
bail!(errors::TerminatedError::new(format!(
141-
"flushing for {}",
142-
file_name
143-
)));
126+
bail!(errors::TerminatedError::new(format!("flushing for {}", file_name)));
144127
}
145128

146129
let count = batch.len();
147130
println!("{}: flushing {} to {}", file_name, count, collection_name);
148-
self.store.write(&collection_name, batch).with_context(|| {
149-
format!(
150-
"Failed to flush {} to {} for {}",
151-
count, collection_name, file_name
152-
)
153-
})?;
131+
self.store
132+
.write(&collection_name, batch)
133+
.with_context(|| format!("Failed to flush {} to {} for {}", count, collection_name, file_name))?;
154134
}
155135

156136
failure
@@ -165,33 +145,22 @@ impl<'a> Processor<'a> {
165145
batch_map: &mut HashMap<String, Vec<bson::Document>>,
166146
) -> Result<()> {
167147
let line = line.with_context(|| format!("Failed to read line at {}", location))?;
168-
let record = Record::try_from(line.as_ref())
169-
.with_context(|| format!("Failed to parse at {}", location))?;
170-
if self
171-
.sni_filter
172-
.map(|f| !f.is_match(&record.sni))
173-
.unwrap_or(false)
174-
{
148+
let record = Record::try_from(line.as_ref()).with_context(|| format!("Failed to parse at {}", location))?;
149+
if self.sni_filter.map(|f| !f.is_match(&record.sni)).unwrap_or(false) {
175150
return Ok(());
176151
}
177152

178153
let geolocation = self
179154
.geolocator
180155
.map(|g| {
181-
g.locate(record.client_ip).with_context(|| {
182-
format!(
183-
"Failed to geolocate client {} at {}",
184-
record.client_ip, location
185-
)
186-
})
156+
g.locate(record.client_ip)
157+
.with_context(|| format!("Failed to geolocate client {} at {}", record.client_ip, location))
187158
})
188159
.transpose()?
189160
.flatten();
190161

191162
let collection_name = format!("{}@{}:{}", record.sni, record.server_ip, record.server_port);
192-
let batch = batch_map
193-
.entry(collection_name.clone())
194-
.or_insert_with(Vec::new);
163+
let batch = batch_map.entry(collection_name.clone()).or_insert_with(Vec::new);
195164

196165
let document = match geolocation {
197166
Some(geoname_id) => bson::Document::from(&EnrichedRecord {
@@ -207,12 +176,9 @@ impl<'a> Processor<'a> {
207176
if batch.len() >= BATCH_SIZE {
208177
println!("{}: writing to {}", location.file_name, collection_name);
209178
let batch = batch_map.remove(&collection_name).unwrap();
210-
self.store.write(&collection_name, batch).with_context(|| {
211-
format!(
212-
"Failed to write to {} for {}",
213-
collection_name, location.file_name
214-
)
215-
})?;
179+
self.store
180+
.write(&collection_name, batch)
181+
.with_context(|| format!("Failed to write to {} for {}", collection_name, location.file_name))?;
216182
}
217183

218184
Ok(())

src/storage.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,8 @@ impl<'a> Store<'a> {
2424
}
2525
}
2626

27-
pub fn write(
28-
&mut self,
29-
collection_name: &str,
30-
batch: impl IntoIterator<Item = bson::Document>,
31-
) -> Result<()> {
32-
let options = mongodb::options::InsertManyOptions::builder()
33-
.ordered(false)
34-
.build();
27+
pub fn write(&mut self, collection_name: &str, batch: impl IntoIterator<Item = bson::Document>) -> Result<()> {
28+
let options = mongodb::options::InsertManyOptions::builder().ordered(false).build();
3529

3630
let collection = match self.collections.entry(String::from(collection_name)) {
3731
Occupied(c) => &*c.into_mut(),
@@ -46,7 +40,9 @@ impl<'a> Store<'a> {
4640
e.kind.as_ref(),
4741
mongodb::error::ErrorKind::BulkWrite(f)
4842
if f.write_concern_error.is_none()
49-
&& f.write_errors.as_ref().map(|b| b.iter().all(|e| e.code == DUPLICATE_KEY_ERROR_CODE)).unwrap_or(false)
43+
&& f.write_errors.as_ref()
44+
.map(|b| b.iter().all(|e| e.code == DUPLICATE_KEY_ERROR_CODE))
45+
.unwrap_or(false)
5046
) =>
5147
{
5248
Ok(())
@@ -62,7 +58,6 @@ fn create_collection(db: &Database, name: &str) -> Result<Collection<bson::Docum
6258
"createIndexes": c.name(),
6359
"indexes": data_model::get_index_model(),
6460
};
65-
db.run_command(command, None)
66-
.context("Failed to create indexes")?;
61+
db.run_command(command, None).context("Failed to create indexes")?;
6762
Ok(c)
6863
}

0 commit comments

Comments
 (0)