Skip to content

Commit

Permalink
build: remove unused crates (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonThormeyer authored Oct 22, 2023
1 parent 96c7549 commit 08f4c5b
Show file tree
Hide file tree
Showing 7 changed files with 4 additions and 139 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ members = [

[workspace.dependencies]
tantivy = "0.19.2"
lopdf = "0.29.0"
clap = "4.2.2"
serde = "1.0.160"
serde_json = "1.0"
Expand Down
1 change: 0 additions & 1 deletion index/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ edition = "2021"

[dependencies]
tantivy = { workspace = true }
lopdf = { workspace = true }
uuid = { workspace = true }
serde_json = { workspace = true }
walkdir = "2.3.3"
Expand Down
66 changes: 1 addition & 65 deletions index/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ impl Index {
#[cfg(test)]
mod tests {
use super::*;
use litt_shared::test_helpers::{cleanup_dir_and_file, save_fake_pdf_document};
use litt_shared::test_helpers::cleanup_dir_and_file;
use once_cell::sync::Lazy;
use serial_test::serial;
use std::panic;
Expand All @@ -309,10 +309,6 @@ mod tests {

static SEARCH_SCHEMA: Lazy<SearchSchema> = Lazy::new(SearchSchema::default);

fn setup() {
save_fake_pdf_document(TEST_DIR_NAME, TEST_FILE_PATH, vec!["Hello, world".into()]);
}

fn teardown() {
cleanup_dir_and_file(TEST_DIR_NAME, TEST_FILE_PATH);
}
Expand All @@ -321,8 +317,6 @@ mod tests {
where
T: FnOnce() + panic::UnwindSafe,
{
setup();

let result = panic::catch_unwind(test);

teardown();
Expand Down Expand Up @@ -356,62 +350,4 @@ mod tests {
.is_dir())
});
}

#[test]
#[serial]
fn test_get_pdf_file_paths() {
run_test(|| {
let index = Index::create(TEST_DIR_NAME, SEARCH_SCHEMA.clone()).unwrap();
let dir_entries = index.get_pdf_dir_entries();
let file_name = dir_entries.first().unwrap().file_name().to_str().unwrap();

assert_eq!(1, dir_entries.len());
assert_eq!(TEST_FILE_PATH, file_name);
});
}

#[test]
#[serial]
fn test_add_all_documents() {
run_test(|| {
let mut index = Index::create(TEST_DIR_NAME, SEARCH_SCHEMA.clone()).unwrap();
index.add_all_pdf_documents().unwrap();
let segments = index.index.searchable_segments().unwrap();
assert_eq!(1, segments.len());
});
}

#[test]
#[serial]
fn test_reload() {
run_test(|| {
let mut index = Index::create(TEST_DIR_NAME, SEARCH_SCHEMA.clone()).unwrap();
// index 1st test document
index.add_all_pdf_documents().unwrap();
assert_eq!(1, index.searcher().num_docs());

// save 2nd document and update
save_fake_pdf_document(TEST_DIR_NAME, "test2.pdf", vec!["Hello, world 2".into()]);
index.reload().unwrap();

assert_eq!(2, index.searcher().num_docs());
});
}

#[test]
#[serial]
fn test_update() {
run_test(|| {
let mut index = Index::create(TEST_DIR_NAME, SEARCH_SCHEMA.clone()).unwrap();
// index 1st test document
index.add_all_pdf_documents().unwrap();
assert_eq!(1, index.searcher().num_docs());

// save 2nd document and update
save_fake_pdf_document(TEST_DIR_NAME, "test2.pdf", vec!["Hello, world 2".into()]);
index.add_all_pdf_documents().unwrap();

assert_eq!(2, index.searcher().num_docs());
});
}
}
1 change: 0 additions & 1 deletion litt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ edition = "2021"

[dependencies]
tantivy = { workspace = true }
lopdf = { workspace = true }
clap = { workspace = true, features = ["derive"] }
serde = { workspace = true }
serde_json = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion search/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ crate-type = ["lib"]
tantivy = "0.19.2"
litt_shared = { path = "../shared" }
litt_index = { path = "../index" }
lopdf = { workspace = true }

2 changes: 1 addition & 1 deletion shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
lopdf = { workspace = true }

tantivy = { workspace = true }
70 changes: 1 addition & 69 deletions shared/src/test_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,76 +1,8 @@
use lopdf::dictionary;
use std::fs::{create_dir_all, remove_dir_all, remove_file};
use std::fs::{remove_dir_all, remove_file};
use std::path::PathBuf;

use lopdf::content::{Content, Operation};
use lopdf::{Document, Object, Stream};

use crate::LITT_DIRECTORY_NAME;

/// Generate a fake pdf document.
/// Source: [github.com/J-F-Liu/lopdf](https://github.com/J-F-Liu/lopdf#example-code)
pub fn generate_fake_pdf_document(page_texts: Vec<String>) -> Document {
let mut doc = Document::with_version("1.5");
let pages_id = doc.new_object_id();
let font_id = doc.add_object(dictionary! {
"Type" => "Font",
"Subtype" => "Type1",
"BaseFont" => "Courier",
});
let resources_id = doc.add_object(dictionary! {
"Font" => dictionary! {
"F1" => font_id,
},
});
let mut page_ids = vec![];
for text in page_texts {
let content = Content {
operations: vec![
Operation::new("BT", vec![]),
Operation::new("Tf", vec!["F1".into(), 48.into()]),
Operation::new("Td", vec![100.into(), 600.into()]),
Operation::new("Tj", vec![Object::string_literal(text)]),
Operation::new("ET", vec![]),
],
};
let content_id = doc.add_object(Stream::new(dictionary! {}, content.encode().unwrap()));
let page_id = doc.add_object(dictionary! {
"Type" => "Page",
"Parent" => pages_id,
"Contents" => content_id,
"Resources" => resources_id,
"MediaBox" => vec![0.into(), 0.into(), 595.into(), 842.into()],
});
page_ids.push(page_id);
}

let pages = dictionary! {
"Type" => "Pages",
"Kids" => page_ids.clone()
.into_iter()
.map(Object::Reference)
.collect::<Vec<_>>(),
"Count" => page_ids.len() as u32,
};

doc.objects.insert(pages_id, Object::Dictionary(pages));
let catalog_id = doc.add_object(dictionary! {
"Type" => "Catalog",
"Pages" => pages_id,
});

doc.trailer.set("Root", catalog_id);

doc
}

pub fn save_fake_pdf_document(directory: &str, file_name: &str, page_texts: Vec<String>) {
create_dir_all(directory).unwrap_or_else(|_| panic!("Failed to create directory: {directory}"));
let mut doc = generate_fake_pdf_document(page_texts);
doc.save(format!("{directory}/{file_name}"))
.unwrap_or_else(|_| panic!("Failed to save test document: {file_name}"));
}

pub fn cleanup_dir_and_file(directory: &str, file_name: &str) {
remove_dir_all(directory).unwrap_or_else(|_| panic!("Failed to remove directory: {directory}"));
// remove if exists, drop result
Expand Down

0 comments on commit 08f4c5b

Please sign in to comment.