Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillt committed Feb 29, 2024
1 parent 2d33c5c commit 861df5a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 72 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async fn main() {
(None, true, false) => &EntryOutput::Id,
(None, false, true) => &EntryOutput::Path,
(None, true, true) => &EntryOutput::Both,
(None, false, false) => &EntryOutput::Id, // default value
(None, false, false) => &EntryOutput::Id, // default mode
_ => panic!(
"incompatible entry output options, please choose only one"
),
Expand Down
78 changes: 7 additions & 71 deletions src/models/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,69 +57,6 @@ impl Storage {
})
}

#[allow(dead_code)]
pub fn load(&mut self) -> Result<(), String> {
match self.storage_type {
StorageType::File => {
let atomic_file =
AtomicFile::new(self.path.clone()).map_err(|e| {
format!(
"Failed to create atomic file at {:?} with error: {:?}",
self.path, e
)
})?;

let atomic_file_data = atomic_file.load().map_err(|e| {
format!(
"Failed to load atomic file at {:?} with error: {:?}",
self.path, e
)
})?;

let data = atomic_file_data.read_to_string().map_err(|_| {
"Could not read atomic file content.".to_string()
})?;

for (i, line) in data.lines().enumerate() {
let mut line = line.split(':');
let id = line.next().unwrap();
match id.parse::<ResourceId>().map_err(|_| {
format!("Failed to parse ResourceId from line: {i}",)
}) {
Ok(id) => self.files.push(id),
Err(e) => {
eprintln!("Error parsing line {}: {}", i, e);
}
}
}
}
StorageType::Folder => {
let folder_entries =
std::fs::read_dir(&self.path).map_err(|e| {
format!(
"Failed to read folder at {:?} with error: {:?}",
self.path, e
)
})?;

for entry in folder_entries {
let entry = entry.map_err(|e| {
format!("Error reading folder entry: {:?}", e)
})?;

if let Some(file_name) = entry.file_name().to_str() {
let id = file_name.parse::<ResourceId>().map_err(|_| {
format!("Failed to parse ResourceId from folder entry: {:?}", file_name)
})?;
self.files.push(id);
}
}
}
};

Ok(())
}

pub fn append(
&mut self,
id: ResourceId,
Expand All @@ -128,14 +65,13 @@ impl Storage {
) -> Result<(), String> {
match self.storage_type {
StorageType::File => {
let atomic_file = AtomicFile::new(&self.path)
// .expect("ERROR: Could not create atomic file");
.map_err(|e| {
format!(
"Failed to create atomic file at {} with error: {:?}",
self.path.display(), e
)
})?;
let atomic_file = AtomicFile::new(&self.path).map_err(|e| {
format!(
"Failed to create atomic file at {} with error: {:?}",
self.path.display(),
e
)
})?;

let content = match format {
Format::KeyValue => return Err(
Expand Down

0 comments on commit 861df5a

Please sign in to comment.