Skip to content

Commit

Permalink
Remove map error calls (#75)
Browse files Browse the repository at this point in the history
* refactor: extend usage of thiserror crate

* chore: remove map_err() calls
  • Loading branch information
SimonThormeyer authored Oct 6, 2024
1 parent bee26d6 commit 07bd550
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 152 deletions.
90 changes: 31 additions & 59 deletions index/src/index.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::LittIndexError::{
CreationError, OpenError, PdfParseError, ReloadError, StateError, TxtParseError, UpdateError,
WriteError,
};
use crate::LittIndexError::{PdfParseError, StateError};
use crate::Result;
use litt_shared::search_schema::SearchSchema;
use litt_shared::LITT_DIRECTORY_NAME;
Expand Down Expand Up @@ -49,7 +46,7 @@ impl Index {
let index_path = documents_path
.join(LITT_DIRECTORY_NAME)
.join(INDEX_DIRECTORY_NAME);
create_dir_all(&index_path).map_err(|e| CreationError(e.to_string()))?;
create_dir_all(&index_path)?;
let index = Self::create_index(&index_path, schema.schema.clone())?;
let writer = Self::build_writer(&index)?;
Ok(Self::Writing {
Expand Down Expand Up @@ -82,7 +79,7 @@ impl Index {
let index_path = documents_path
.join(LITT_DIRECTORY_NAME)
.join(INDEX_DIRECTORY_NAME);
create_dir_all(&index_path).map_err(|e| CreationError(e.to_string()))?;
create_dir_all(&index_path)?;
let index_create_result = Self::create_index(&index_path, schema.schema.clone());
match index_create_result {
Ok(index) => {
Expand Down Expand Up @@ -133,18 +130,15 @@ impl Index {
mut writer,
} = self
{
writer.commit().map_err(|e| WriteError(e.to_string()))?;
writer.commit()?;
let reader = Self::build_reader(&index)?;
reader.reload().map_err(|e| ReloadError(e.to_string()))?;
reader.reload()?;
self = Index::Reading {
index,
schema,
reader,
documents_path,
failed_documents: failed_documents
.lock()
.map_err(|e| WriteError(e.to_string()))?
.to_vec(),
failed_documents: failed_documents.lock()?.to_vec(),
};
Ok(self)
} else {
Expand All @@ -160,7 +154,7 @@ impl Index {
..
} = self
{
let writer = Self::build_writer(&index).map_err(|e| UpdateError(e.to_string()))?;
let writer = Self::build_writer(&index)?;
self = Index::Writing {
index,
schema,
Expand Down Expand Up @@ -190,10 +184,7 @@ impl Index {
existing_checksum: Option<&(u64, SystemTime)>,
) -> Result<(String, (u64, SystemTime))> {
if let Index::Writing { documents_path, .. } = &self {
let relative_path = path
.path()
.strip_prefix(documents_path)
.map_err(|e| CreationError(e.to_string()))?;
let relative_path = path.path().strip_prefix(documents_path)?;

let str_path = path.path().to_string_lossy().to_string();
if !Self::checksum_is_equal(&str_path, existing_checksum).unwrap_or(false) {
Expand Down Expand Up @@ -222,9 +213,7 @@ impl Index {
} = self
{
let writer = Self::build_writer(index)?;
writer
.delete_all_documents()
.map_err(|e| UpdateError(e.to_string()))?;
writer.delete_all_documents()?;
let checksum_map = PathBuf::from(documents_path)
.join(LITT_DIRECTORY_NAME)
.join(CHECK_SUM_MAP_FILENAME);
Expand Down Expand Up @@ -252,25 +241,23 @@ impl Index {
}

fn create_index(path: &PathBuf, schema: Schema) -> Result<TantivyIndex> {
TantivyIndex::create_in_dir(path, schema).map_err(|e| CreationError(e.to_string()))
TantivyIndex::create_in_dir(path, schema).map_err(Into::into)
}

fn open_tantivy_index(path: &PathBuf) -> Result<TantivyIndex> {
TantivyIndex::open_in_dir(path).map_err(|e| OpenError(e.to_string()))
TantivyIndex::open_in_dir(path).map_err(Into::into)
}

fn build_reader(index: &TantivyIndex) -> Result<IndexReader> {
index
.reader_builder()
.reload_policy(ReloadPolicy::Manual)
.try_into()
.map_err(|e| CreationError(e.to_string()))
.map_err(Into::into)
}

fn build_writer(index: &TantivyIndex) -> Result<IndexWriter> {
index
.writer(TARGET_MEMORY_BYTES)
.map_err(|e| CreationError(e.to_string()))
index.writer(TARGET_MEMORY_BYTES).map_err(Into::into)
}

fn collect_document_files(&self) -> Vec<DirEntry> {
Expand Down Expand Up @@ -300,7 +287,7 @@ impl Index {
.join(LITT_DIRECTORY_NAME)
.join(PAGES_DIRECTORY_NAME)
.join(doc_id.to_string());
create_dir_all(&pages_path).map_err(|e| CreationError(e.to_string()))?;
create_dir_all(&pages_path)?;
let full_path = dir_entry.path();

// Check filetype (pdf/ txt)
Expand Down Expand Up @@ -354,8 +341,7 @@ impl Index {

if pdf_to_text_successful {
// read page-body from generated .txt file
let page_body = std::fs::read_to_string(&page_path)
.map_err(|e| PdfParseError(e.to_string()))?;
let page_body = std::fs::read_to_string(&page_path)?;
self.add_page(dir_entry.path(), page_number, &page_path, &page_body)?;
}
}
Expand All @@ -373,16 +359,14 @@ impl Index {
let mut page_path = pages_path.join(page_number.to_string());
page_path.set_extension("pageinfo");
// Open the file in read-only mode
let mut file = File::open(full_path).map_err(|e| TxtParseError(e.to_string()))?;
let mut file = File::open(full_path)?;
// Store as page seperatly
let mut destination_file =
File::create(page_path.clone()).map_err(|e| TxtParseError(e.to_string()))?;
io::copy(&mut file, &mut destination_file).map_err(|e| TxtParseError(e.to_string()))?;
let mut destination_file = File::create(page_path.clone())?;
io::copy(&mut file, &mut destination_file)?;
// Read the contents of the file into a string
let mut file = File::open(full_path).map_err(|e| TxtParseError(e.to_string()))?;
let mut file = File::open(full_path)?;
let mut body = String::new();
file.read_to_string(&mut body)
.map_err(|e| TxtParseError(e.to_string() + full_path.to_string_lossy().as_ref()))?;
file.read_to_string(&mut body)?;
// Finally, add page
self.add_page(dir_entry.path(), page_number, &page_path, &body)?;
Ok(page_number)
Expand All @@ -402,9 +386,7 @@ impl Index {
..
} = self
{
let relative_path = full_path
.strip_prefix(documents_path)
.map_err(|e| CreationError(e.to_string()))?;
let relative_path = full_path.strip_prefix(documents_path)?;
// documents_path base from path
let mut tantivy_document = TantivyDocument::new();

Expand All @@ -413,9 +395,7 @@ impl Index {
tantivy_document.add_text(schema.title, relative_path.to_string_lossy());
tantivy_document.add_u64(schema.page, page_number);
tantivy_document.add_text(schema.body, page_body);
writer
.add_document(tantivy_document)
.map_err(|e| WriteError(e.to_string()))?;
writer.add_document(tantivy_document)?;
Ok(())
} else {
Err(StateError("Writing".to_string()))
Expand All @@ -427,8 +407,8 @@ impl Index {
let path = documents_path
.join(LITT_DIRECTORY_NAME)
.join(CHECK_SUM_MAP_FILENAME);
let data = std::fs::read_to_string(path).map_err(|e| CreationError(e.to_string()))?;
Ok(serde_json::from_str(&data).map_err(|e| CreationError(e.to_string()))?)
let data = std::fs::read_to_string(path)?;
Ok(serde_json::from_str(&data)?)
} else {
Err(StateError("Writing".to_string()))
}
Expand All @@ -439,35 +419,27 @@ impl Index {
let path = documents_path
.join(LITT_DIRECTORY_NAME)
.join(CHECK_SUM_MAP_FILENAME);
std::fs::write(
path,
serde_json::to_string(&checksum_map).map_err(|e| CreationError(e.to_string()))?,
)
.map_err(|e| CreationError(e.to_string()))
std::fs::write(path, serde_json::to_string(&checksum_map)?).map_err(Into::into)
} else {
Err(StateError("Writing".to_string()))
}
}

/// Calculates the checksum of a file that consists of the metadata length and last modified time
fn calculate_checksum(path: &str) -> Result<(String, (u64, SystemTime))> {
let file = File::open(path).map_err(|e| CreationError(e.to_string()))?;
let metadata = file.metadata().map_err(|e| CreationError(e.to_string()))?;
let modified = metadata
.modified()
.map_err(|e| CreationError(e.to_string()))?;
let file = File::open(path)?;
let metadata = file.metadata()?;
let modified = metadata.modified()?;

let result = (path.to_string(), (metadata.len(), modified));
Ok(result)
}

fn checksum_is_equal(path: &str, checksum: Option<&(u64, SystemTime)>) -> Result<bool> {
if let Some((len, last_modified)) = checksum {
let file = File::open(path).map_err(|e| CreationError(e.to_string()))?;
let metadata = file.metadata().map_err(|e| CreationError(e.to_string()))?;
let modified = metadata
.modified()
.map_err(|e| CreationError(e.to_string()))?;
let file = File::open(path)?;
let metadata = file.metadata()?;
let modified = metadata.modified()?;
Ok(*len == metadata.len() && *last_modified == modified)
} else {
Ok(false)
Expand Down
17 changes: 17 additions & 0 deletions index/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::io;
use thiserror::Error;

pub mod index;
Expand All @@ -22,6 +23,22 @@ pub enum LittIndexError {
PdfParseError(String),
#[error("Error parsing txt-file: `{0}`")]
TxtParseError(String),
#[error(transparent)]
IoError(#[from] io::Error),
#[error(transparent)]
TantivyError(#[from] tantivy::TantivyError),
#[error(transparent)]
StripPrefixError(#[from] std::path::StripPrefixError),
#[error(transparent)]
SerdeJsonError(#[from] serde_json::error::Error),
#[error("One of the locks is poisoned: {0}")]
LockPoisoned(String),
}

impl<T> From<std::sync::PoisonError<T>> for LittIndexError {
fn from(error: std::sync::PoisonError<T>) -> Self {
Self::LockPoisoned(error.to_string())
}
}

pub type Result<T> = std::result::Result<T, LittIndexError>;
39 changes: 15 additions & 24 deletions litt/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::collections::HashMap;
use std::env;
use std::fs;
use std::path::Path;
use std::time::Instant;
use std::{env, io};

use clap::CommandFactory;
use clap::Parser;
Expand All @@ -25,6 +25,12 @@ use thiserror::Error;
enum LittError {
#[error("Error:`{0}`")]
General(String),
#[error(transparent)]
IoError(#[from] io::Error),
#[error(transparent)]
LittIndexError(#[from] litt_index::LittIndexError),
#[error(transparent)]
LittIndexTrackerError(#[from] tracker::LittIndexTrackerError),
}

fn get_first_term(query: &str) -> String {
Expand Down Expand Up @@ -67,24 +73,17 @@ Using standard system PDF viewer... {}",

fn open_std_programm(path: String) -> Result<(), LittError> {
#[cfg(target_os = "macos")]
std::process::Command::new("open")
.arg(&path)
.spawn()
.map_err(|e| LittError::General(e.to_string()))?;
std::process::Command::new("open").arg(&path).spawn()?;

#[cfg(target_os = "linux")]
std::process::Command::new("xdg-open")
.arg(&path)
.spawn()
.map_err(|e| LittError::General(e.to_string()))?;
std::process::Command::new("xdg-open").arg(&path).spawn()?;

#[cfg(windows)]
std::process::Command::new("cmd")
.arg("/c")
.arg("start")
.arg(&path)
.spawn()
.map_err(|e| LittError::General(e.to_string()))?;
.spawn()?;

Ok(())
}
Expand Down Expand Up @@ -148,17 +147,15 @@ fn main() -> Result<(), LittError> {
// check if name of litt index was given by user
let index_name = match cli.litt_index {
None => {
Cli::command()
.print_help()
.map_err(|e| LittError::General(e.to_string()))?;
Cli::command().print_help()?;
return Err(LittError::General("Litt index missing!".into()));
}
Some(index_name) => index_name,
};

// initialize new index
if !cli.init.is_empty() {
let current_dir = env::current_dir().map_err(|e| LittError::General(e.to_string()))?;
let current_dir = env::current_dir()?;
let path = current_dir.join(cli.init);
println!(
"Creating new index \"{}\" at: {}: ",
Expand Down Expand Up @@ -189,9 +186,7 @@ fn main() -> Result<(), LittError> {
Err(e) => return Err(LittError::General(e.to_string())),
};

let searcher = index
.searcher()
.map_err(|e| LittError::General(e.to_string()))?;
let searcher = index.searcher()?;
println!(
"Successfully indexed {} document pages in {:?}",
searcher.num_docs(),
Expand All @@ -218,16 +213,12 @@ fn main() -> Result<(), LittError> {
}

// get index:
let index_path = index_tracker
.get_path(&index_name)
.map_err(|e| LittError::General(e.to_string()))?;
let index_path = index_tracker.get_path(&index_name)?;
let index = match Index::open(index_path.clone(), SearchSchema::default()) {
Ok(index) => index,
Err(e) => return Err(LittError::General(e.to_string())),
};
let searcher = index
.searcher()
.map_err(|e| LittError::General(e.to_string()))?;
let searcher = index.searcher()?;

// update existing index
if cli.update {
Expand Down
Loading

0 comments on commit 07bd550

Please sign in to comment.