From 718e57355b06f166ac9101b6a2b9e0a9ad99d28e Mon Sep 17 00:00:00 2001 From: Alex Wellnitz Date: Thu, 26 Oct 2023 10:54:11 +0200 Subject: [PATCH] feat: Add make Record function --- src/search.rs | 9 +++++++++ tests/rustysearch.rs | 14 ++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/search.rs b/src/search.rs index 71329f0..caaad88 100644 --- a/src/search.rs +++ b/src/search.rs @@ -1,5 +1,7 @@ use std::{cmp::min, collections::HashMap, fs, path::Path}; +use serde_json::{Value, json}; + use crate::{analyze::tokenizer::Tokenizer, types::Stats}; pub struct Rustysearch { @@ -195,4 +197,11 @@ impl Rustysearch { let info = parts.next().unwrap().to_string(); (term, info) } + + /// Given a ``term`` and a dict of ``term_info``, creates a line for + /// writing to the segment file. + /// + pub fn make_record(&self, term: &str, term_info: &Value) -> String { + format!("{}\t{}\n", term, json!(term_info).to_string()) + } } diff --git a/tests/rustysearch.rs b/tests/rustysearch.rs index b0d84b5..bef568b 100644 --- a/tests/rustysearch.rs +++ b/tests/rustysearch.rs @@ -1,6 +1,7 @@ #[cfg(test)] mod tests { use rustysearch::{search::Rustysearch, types::Stats}; + use serde_json::json; #[test] fn test_write_new_stats() { @@ -118,6 +119,19 @@ mod tests { assert_eq!(tokens, vec!["hello", "world"]); } + #[test] + fn test_make_record() { + let search = Rustysearch::new("/tmp/rustysearch"); + let term = "hello world"; + let term_info = json!({ + "frequency": 100, + "idf": 1.5, + }); + + let record = search.make_record(term, &term_info); + assert_eq!(record, "hello world\t{\"frequency\":100,\"idf\":1.5}\n"); + } + // Helper function to clean up the stats file fn clean_stats(tmp_path: &str) { let search = Rustysearch::new(tmp_path);