Skip to content

Commit de090f5

Browse files
authored
Merge pull request #13 from future-highway/consensus
Refactoring and cleanup
2 parents 60c42b9 + cf86db6 commit de090f5

15 files changed

+355
-426
lines changed

Cargo.toml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
[workspace]
2+
resolver = "2"
23

3-
members = [
4-
"cli",
5-
"database",
6-
"experiments/iocost", "html-test",
7-
]
4+
members = ["cli", "database", "experiments/iocost", "html-test"]

database/Cargo.toml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
clap = { version = "4.4.10", features = ["derive","string"]}
109
actix-web = "4.3.1"
11-
lazy_static = "1.4.0"
10+
anyhow = "1"
11+
clap = { version = "4.4.10", features = ["derive", "string"] }
1212
easy_reader = "0.5.1"
13+
env_logger = "0.11.1"
1314
fnv = "1.0.7"
14-
anyhow = "1"
15-
tempfile = "3.8.0"
15+
lazy_static = "1.4.0"
16+
log = "0.4"
1617
prost = "0.12"
17-
tonic = { version = "0.10.2" }
18-
tokio = { version = "1.26.0", features = ["full"] }
18+
quick-xml = { version = "0.31.0", features = ["serialize"] }
1919
rand = "0.8.5"
2020
rand_chacha = "0.3.1"
21-
quick-xml = { version= "0.31.0", features = ["serialize"]}
22-
serde_json = "1.0.70"
2321
serde = { version = "1.0.130", features = ["derive"] }
24-
log = "0.4"
25-
env_logger = "0.11.1"
22+
serde_json = "1.0.70"
23+
tempfile = "3.8.0"
24+
tokio = { version = "1.26.0", features = ["full"] }
25+
tonic = { version = "0.10.2" }
2626

2727

2828
[build-dependencies]

database/src/file/file_engine.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use quick_xml::de::from_str;
2-
use serde::{Deserialize, Serialize};
3-
4-
use prost::Message;
1+
use std::str::FromStr;
52

63
use super::proto;
74
use super::record::{HtmlRecord, JsonRecord, Record};
85
use crate::errors::NullDbReadError;
6+
use anyhow::anyhow;
7+
use prost::Message;
8+
use quick_xml::de::from_str;
99

1010
#[derive(Debug, Clone, Copy)]
1111
pub enum FileEngine {
@@ -14,18 +14,22 @@ pub enum FileEngine {
1414
Proto,
1515
}
1616

17-
/// FileEngine is an enum that represents the different file engines that can be used to store records.
18-
impl FileEngine {
17+
impl FromStr for FileEngine {
18+
type Err = anyhow::Error;
19+
1920
/// new creates a new FileEngine from a string, valid options are json, html and proto.
20-
pub fn new(engine: &str) -> Self {
21-
match engine {
22-
"json" => FileEngine::Json,
23-
"html" => FileEngine::Html,
24-
"proto" => FileEngine::Proto,
25-
_ => panic!("Invalid file engine"),
21+
fn from_str(s: &str) -> Result<Self, Self::Err> {
22+
match s {
23+
"json" => Ok(FileEngine::Json),
24+
"html" => Ok(FileEngine::Html),
25+
"proto" => Ok(FileEngine::Proto),
26+
_ => Err(anyhow!("Invalid file engine")),
2627
}
2728
}
29+
}
2830

31+
/// FileEngine is an enum that represents the different file engines that can be used to store records.
32+
impl FileEngine {
2933
/// deserialize a string into a Record.
3034
///
3135
/// # Errors
@@ -35,16 +39,16 @@ impl FileEngine {
3539
match self {
3640
FileEngine::Json => {
3741
let json: JsonRecord =
38-
serde_json::from_str(value).map_err(|e| NullDbReadError::Corrupted)?;
42+
serde_json::from_str(value).map_err(|_e| NullDbReadError::Corrupted)?;
3943
Ok(Record::Json(json))
4044
}
4145
FileEngine::Html => {
42-
let html: HtmlRecord = from_str(value).map_err(|e| NullDbReadError::Corrupted)?;
46+
let html: HtmlRecord = from_str(value).map_err(|_e| NullDbReadError::Corrupted)?;
4347
Ok(Record::Html(html))
4448
}
4549
FileEngine::Proto => {
4650
let proto: proto::ProtoRecord = proto::ProtoRecord::decode(value.as_bytes())
47-
.map_err(|e| NullDbReadError::Corrupted)?;
51+
.map_err(|_e| NullDbReadError::Corrupted)?;
4852
Ok(Record::Proto(proto))
4953
}
5054
}

database/src/file/record.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,15 @@ impl Record {
8383
}
8484
}
8585

86-
/// get_key returns the key of the record.
86+
pub fn as_key(&self) -> &str {
87+
match self {
88+
Record::Json(json) => &json.key,
89+
Record::Html(html) => &html.key,
90+
Record::Proto(proto) => &proto.key,
91+
}
92+
}
93+
94+
/// to_key returns a clone of the recored's key.
8795
pub fn get_key(&self) -> String {
8896
match self {
8997
Record::Json(json) => json.key.clone(),
@@ -107,19 +115,21 @@ impl Record {
107115
Record::Json(json) => json.tombstone,
108116
Record::Proto(proto) => proto.tombstone,
109117
Record::Html(html) => match &html.class {
110-
Some(class) => {
111-
if class == "tombstone" {
112-
Some(true)
113-
} else {
114-
None
115-
}
116-
}
117-
None => None,
118+
Some(class) if class == "tombstone" => Some(true),
119+
Some(_) | None => None,
118120
},
119121
}
120122
}
121123

122-
/// get_value returns the value of the record.
124+
pub fn as_value(&self) -> Option<&str> {
125+
match self {
126+
Record::Json(json) => json.value.as_deref(),
127+
Record::Html(html) => html.value.as_deref(),
128+
Record::Proto(proto) => proto.value.as_deref(),
129+
}
130+
}
131+
132+
/// get_value returns a clone of the recored's value.
123133
pub fn get_value(&self) -> Option<String> {
124134
match self {
125135
Record::Json(json) => json.value.clone(),

0 commit comments

Comments
 (0)