Skip to content

Commit

Permalink
Merge pull request #25 from PlexSheep/devel
Browse files Browse the repository at this point in the history
refactor(reader): load store as readonly
  • Loading branch information
PlexSheep authored Nov 12, 2024
2 parents 5a1e65d + d10a7ca commit 240f7da
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
34 changes: 16 additions & 18 deletions src/bins/netpulse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ fn main() {
print_version()
}
if matches.opt_present("dump") {
dump(failed_only);
if let Err(e) = dump(failed_only) {
error!("{e}");
std::process::exit(1)
}
} else if matches.opt_present("test") {
if let Err(e) = test_checks() {
error!("{e}");
Expand All @@ -66,7 +69,10 @@ fn main() {
std::process::exit(1)
}
} else {
analysis();
if let Err(e) = analysis() {
error!("{e}");
std::process::exit(1)
}
}
}

Expand All @@ -80,18 +86,8 @@ fn test_checks() -> Result<(), RunError> {
Ok(())
}

fn store_load() -> Store {
match Store::load() {
Err(e) => {
eprintln!("The store could not be loaded: {e}");
std::process::exit(1)
}
Ok(s) => s,
}
}

fn dump(failed_only: bool) {
let store = store_load();
fn dump(failed_only: bool) -> Result<(), RunError> {
let store = Store::load(true)?;
let mut buf = String::new();
let ref_checks: Vec<&Check> = if failed_only {
store.checks().iter().filter(|c| !c.is_success()).collect()
Expand All @@ -102,24 +98,26 @@ fn dump(failed_only: bool) {
eprintln!("{e}");
std::process::exit(1);
}
println!("{buf}")
println!("{buf}");
Ok(())
}

fn rewrite() -> Result<(), RunError> {
let s = Store::load()?;
let s = Store::load(true)?;
s.save()?;
Ok(())
}

fn analysis() {
let store = store_load();
fn analysis() -> Result<(), RunError> {
let store = Store::load(true)?;
match analyze::analyze(&store) {
Err(e) => {
eprintln!("Error while making the analysis: {e}");
std::process::exit(1);
}
Ok(report) => println!("{report}"),
}
Ok(())
}

fn print_version() -> ! {
Expand Down
19 changes: 17 additions & 2 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ impl Store {
/// store.save().unwrap();
/// ```
pub fn load_or_create() -> Result<Self, StoreError> {
match Self::load() {
match Self::load(false) {
Ok(store) => Ok(store),
Err(err) => match &err {
StoreError::DoesNotExist => Self::create(),
Expand Down Expand Up @@ -392,7 +392,7 @@ impl Store {
/// - Store file doesn't exist
/// - Read/parse fails
/// - Version unsupported
pub fn load() -> Result<Self, StoreError> {
pub fn load(readonly: bool) -> Result<Self, StoreError> {
let file = match fs::File::options()
.read(true)
.write(false)
Expand Down Expand Up @@ -447,6 +447,11 @@ impl Store {
return Err(StoreError::UnsupportedVersion);
}
}

if readonly {
store.set_readonly();
}

Ok(store)
}

Expand Down Expand Up @@ -689,6 +694,16 @@ impl Store {
let version_only: VersionOnly = bincode::deserialize_from(reader)?;
Ok(version_only.version)
}

/// True if this [Store] is read only
pub fn readonly(&self) -> bool {
self.readonly
}

/// Make this [Store] read only
pub fn set_readonly(&mut self) {
self.readonly = true;
}
}

fn has_cap_net_raw() -> bool {
Expand Down

0 comments on commit 240f7da

Please sign in to comment.