Skip to content

Commit

Permalink
some changes
Browse files Browse the repository at this point in the history
Signed-off-by: Pushkar Mishra <[email protected]>
  • Loading branch information
Pushkarm029 committed Mar 10, 2024
1 parent 56e27e6 commit 28beaa0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
8 changes: 8 additions & 0 deletions fs-storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ serde = { version = "1.0.138", features = ["derive"] }
log = { version = "0.4.17", features = ["release_max_level_off"] }
tempdir = "0.3.7"
tempfile = "3.10.1"

[[bin]]
name = "read"
path = "src/cli/read.rs"

[[bin]]
name = "write"
path = "src/cli/write.rs"
19 changes: 12 additions & 7 deletions fs-storage/src/cli/read.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::env;
use fs_storage::file_storage::FileStorage; // Import FileStorage
use std::collections::HashMap;
use std::str::FromStr;
use data_error::ArklibError;
use std::fmt::Debug;
use std::path::Path;
use fs_storage::file_storage::FileStorage;

fn main() {
let args: Vec<String> = env::args().collect();
Expand All @@ -14,9 +11,17 @@ fn main() {
let storage_path = &args[1];
let key = &args[2];

let file_storage = FileStorage::new("Storage".to_string(), storage_path);
println!("Storage Path: {}", storage_path);
println!("Key: {}", key);

if !Path::new(storage_path).exists() {
println!("Error: Storage file does not exist.");
return;
}

let file_storage = FileStorage::new("our_label".to_string(), Path::new(storage_path));
match file_storage.read_file::<String, String>(|value_by_id| {
println!("ruun3");
if let Some(value) = value_by_id.get(key) {
println!("Value for key '{}': {:?}", key, value);
} else {
Expand All @@ -28,4 +33,4 @@ fn main() {
println!("Error reading storage file: {:?}", e);
}
}
}
}
34 changes: 19 additions & 15 deletions fs-storage/src/cli/write.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::collections::HashMap;
use std::env;
use std::fs::{self, File};
use std::io::{self, Write, BufWriter};
use std::path::Path;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use fs_storage::file_storage::FileStorage;

fn main() {
let args: Vec<String> = env::args().collect();
Expand All @@ -16,19 +17,14 @@ fn main() {
println!("Storage already exists at {}", storage_path);
return;
}
if let Err(err) = fs::create_dir(storage_path) {

if let Err(err) = fs::create_dir_all(storage_path) {
println!("Error creating storage directory: {}", err);
return;
}
println!("Storage created successfully at {}", storage_path);
println!("Storage directory created successfully at {}", storage_path);

let mut kv_pairs: HashMap<String, String> = HashMap::new();

println!("Please specify the storage type:");
let mut storage_type = String::new();
io::stdin().read_line(&mut storage_type).expect("Failed to read line");
let storage_type = storage_type.trim();

loop {
println!("Enter a key-value pair (key=value), or enter 'done' to finish:");
let mut input = String::new();
Expand All @@ -51,7 +47,6 @@ fn main() {
kv_pairs.insert(key, value);
}

println!("Storage Type: {}", storage_type);
println!("Key-Value Pairs:");
for (key, value) in &kv_pairs {
println!("{}: {}", key, value);
Expand All @@ -63,6 +58,15 @@ fn main() {
}

fn write_to_file(kv_pairs: HashMap<String, String>, storage_path: &str) -> io::Result<()> {
let mut storage = file_storage::FileStorage::new(storage_path);
storage.write_file(&kv_pairs)
}
let mut storage_file = PathBuf::from(storage_path);
storage_file.push("storage.txt"); // Adjust the filename as needed

let mut storage = FileStorage::new("our_label".to_string(), &storage_file);
match storage.write_file(&kv_pairs) {
Ok(_) => Ok(()),
Err(err) => {
let io_err = io::Error::new(io::ErrorKind::Other, format!("ArklibError: {:?}", err));
Err(io_err)
}
}
}

0 comments on commit 28beaa0

Please sign in to comment.