Skip to content

Commit

Permalink
clippy, formatting and version change
Browse files Browse the repository at this point in the history
  • Loading branch information
georgbuechner committed Oct 22, 2023
1 parent 126c321 commit bb2350b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
43 changes: 29 additions & 14 deletions index/src/index.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::LittIndexError::{
CreationError, OpenError, PdfParseError, TxtParseError, ReloadError, UpdateError, WriteError,
CreationError, OpenError, PdfParseError, ReloadError, TxtParseError, UpdateError, WriteError,
};
use crate::Result;
use litt_shared::search_schema::SearchSchema;
use litt_shared::LITT_DIRECTORY_NAME;
use std::collections::HashMap;
use std::convert::AsRef;
use std::fs::{create_dir_all, File};
use std::io::{Read, self};
use std::io::{self, Read};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::SystemTime;
Expand Down Expand Up @@ -91,15 +91,19 @@ impl Index {
self.reader.reload().map_err(|e| ReloadError(e.to_string()))
}

pub fn process_file(&mut self, checksum_map: &mut HashMap<String, (u64, SystemTime)>, path: DirEntry) -> Result<()> {
pub fn process_file(
&mut self,
checksum_map: &mut HashMap<String, (u64, SystemTime)>,
path: DirEntry,
) -> Result<()> {
let relative_path = path
.path()
.strip_prefix(&self.documents_path)
.map_err(|e| CreationError(e.to_string()))?;

let str_path = &path.path().to_string_lossy().to_string();
if !self
.compare_checksum(str_path, &checksum_map)
.compare_checksum(str_path, checksum_map)
.unwrap_or(false)
{
println!("Adding document: {}", relative_path.to_string_lossy());
Expand Down Expand Up @@ -162,11 +166,11 @@ impl Index {
.follow_links(true)
.into_iter()
.filter_map(|entry_result| entry_result.ok())
.filter(|entry|
entry.file_name().to_string_lossy().ends_with("pdf") ||
entry.file_name().to_string_lossy().ends_with("md") ||
entry.file_name().to_string_lossy().ends_with("txt")
)
.filter(|entry| {
entry.file_name().to_string_lossy().ends_with("pdf")
|| entry.file_name().to_string_lossy().ends_with("md")
|| entry.file_name().to_string_lossy().ends_with("txt")
})
.collect::<Vec<_>>()
}

Expand Down Expand Up @@ -198,7 +202,12 @@ impl Index {
Ok(())
}

fn add_pdf_document(&self, dir_entry: &DirEntry, pages_path: PathBuf, full_path: &Path) -> Result<u64> {
fn add_pdf_document(
&self,
dir_entry: &DirEntry,
pages_path: PathBuf,
full_path: &Path,
) -> Result<u64> {
// loop over pages
let mut pdf_to_text_successful = true;
let mut page_number = 0;
Expand Down Expand Up @@ -234,20 +243,26 @@ impl Index {
Ok(page_number)
}

fn add_txt_document(&self, dir_entry: &DirEntry, pages_path: PathBuf, full_path: &Path) -> Result<u64> {
fn add_txt_document(
&self,
dir_entry: &DirEntry,
pages_path: PathBuf,
full_path: &Path,
) -> Result<u64> {
let page_number = 1;
let mut page_path = pages_path.join(page_number.to_string());
page_path.set_extension("txt");
// Open the file in read-only mode
let mut file = File::open(full_path).map_err(|e| TxtParseError(e.to_string()))?;
// 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()).map_err(|e| TxtParseError(e.to_string()))?;
io::copy(&mut file, &mut destination_file).map_err(|e| TxtParseError(e.to_string()))?;
// 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 body = String::new();
file.read_to_string(&mut body)
.map_err(|e| TxtParseError(e.to_string() + &full_path.to_string_lossy().to_string()))?;
.map_err(|e| TxtParseError(e.to_string() + full_path.to_string_lossy().as_ref()))?;
// Finally, add page
self.add_page(dir_entry.path(), page_number, &page_path, &body)?;
Ok(page_number)
Expand Down
2 changes: 1 addition & 1 deletion litt/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "litt"
version = "1.0.0"
version = "1.0.1"
edition = "2021"

[dependencies]
Expand Down
14 changes: 7 additions & 7 deletions litt/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ fn get_first_term(query: &str) -> String {

fn open_pdf(path: String, page: u32, term: String) -> Result<(), LittError> {
let mut cmd = std::process::Command::new("zathura");
cmd.arg(&path)
.arg("-P")
.arg(&page.to_string())
.arg("-f")
.arg(&term);
cmd.arg(&path)
.arg("-P")
.arg(&page.to_string())
.arg("-f")
.arg(&term);

let zathura_was_successful = match cmd.status() {
Ok(status) => match status.code() {
Expand All @@ -63,7 +63,8 @@ fn open_pdf(path: String, page: u32, term: String) -> Result<(), LittError> {
if !zathura_was_successful {
println!(
"Consider installing zathura so we can open the PDF on the correct page for you.\n\
Using standard system PDF viewer... {}", path.to_string()
Using standard system PDF viewer... {}",
path
);
open_std_programm(path)?;
}
Expand Down Expand Up @@ -112,7 +113,6 @@ fn main() -> Result<(), LittError> {
let path = fast_results
.get(last_result)
.expect("Number not in last results");
println!("Got path: {}", path.0.to_string());
if path.0.ends_with("pdf") {
open_pdf(path.0.clone(), path.1, path.2.clone())?;
} else {
Expand Down

0 comments on commit bb2350b

Please sign in to comment.