bin/darkwikid: attach document title to the patch

This commit is contained in:
ghassmo
2022-08-19 15:18:20 +04:00
parent afdd4b45dc
commit 43a7fd586e
2 changed files with 18 additions and 15 deletions

View File

@@ -92,16 +92,14 @@ fn get_from_index(local_path: &Path, title: &str) -> DarkWikiResult<String> {
}
fn save_to_index(local_path: &Path, id: &str, title: &str) -> DarkWikiResult<()> {
let mut index = load_json_file::<FxHashMap<String, String>>(&local_path.join("index"))?;
let mut index = load_json_file::<FxHashMap<String, String>>(&local_path.join("index"))
.unwrap_or(FxHashMap::default());
index.insert(id.to_owned(), title.to_owned());
save_json_file(&local_path.join("index"), &index)?;
Ok(())
}
fn extract_title(content: &str) -> String {
str_to_chars(content).into_iter().skip_while(|&c| c == "#").take_while(|&c| c == "\n").collect()
}
fn lcs(a: &str, b: &str) -> Vec<OpMethod> {
let a: Vec<_> = str_to_chars(a);
let b: Vec<_> = str_to_chars(b);
@@ -183,12 +181,11 @@ fn on_receive_update(settings: &DarkWikiSettings) -> DarkWikiResult<Vec<Patch>>
};
// load doc content
// TODO
let edit = load_file(&docs_path.join(doc_title)).map_err(Error::from)?;
let edit = edit.trim();
// create new patch
let mut new_patch = Patch::new(&doc_id, &settings.author);
let mut new_patch = Patch::new(doc_title, &doc_id, &settings.author);
// check for any changes found with local doc and darkwiki doc
if let Ok(local_edit) = load_file(&local_path.join(&doc_id)) {
@@ -245,7 +242,7 @@ fn on_receive_update(settings: &DarkWikiSettings) -> DarkWikiResult<Vec<Patch>>
}
let sync_patch_str = sync_patch.to_string();
let file_title = extract_title(&sync_patch_str);
let file_title = sync_patch.title();
save_to_index(&local_path, file_id, &file_title)?;
save_file(&docs_path.join(&file_title), &sync_patch_str)?;

View File

@@ -21,6 +21,7 @@ pub struct OpMethods(pub Vec<OpMethod>);
#[derive(PartialEq, Eq, SerialEncodable, SerialDecodable, Serialize, Deserialize, Clone, Debug)]
pub struct Patch {
title: String,
author: String,
id: String,
base: String,
@@ -67,8 +68,9 @@ impl std::string::ToString for Patch {
}
impl Patch {
pub fn new(id: &str, author: &str) -> Self {
pub fn new(title: &str, id: &str, author: &str) -> Self {
Self {
title: title.to_string(),
id: id.to_string(),
ops: OpMethods(vec![]),
base: String::new(),
@@ -147,6 +149,10 @@ impl Patch {
self.id.clone()
}
pub fn title(&self) -> String {
self.title.clone()
}
pub fn ops(&self) -> OpMethods {
self.ops.clone()
}
@@ -158,7 +164,7 @@ impl Patch {
//
// TODO need more work to get better performance with iterators
pub fn transform(&self, other: &Self) -> Self {
let mut new_patch = Self::new(&self.id, &self.author);
let mut new_patch = Self::new(&self.title, &self.id, &self.author);
new_patch.set_base(&self.base);
let mut ops1 = self.ops.0.iter().cloned();
@@ -260,7 +266,7 @@ impl Patch {
let mut ops1 = ops1.iter().cloned();
let mut ops2 = other.ops.0.iter().cloned();
let mut new_patch = Self::new(&self.id, &self.author);
let mut new_patch = Self::new(&self.title, &self.id, &self.author);
new_patch.set_base(&self.base);
let mut op1 = ops1.next();
@@ -432,7 +438,7 @@ mod tests {
#[test]
fn test_to_string() {
let mut patch = Patch::new(&gen_id(30), "");
let mut patch = Patch::new("", &gen_id(30), "");
patch.set_base("text example\n hello");
patch.retain(14);
patch.delete(5);
@@ -443,7 +449,7 @@ mod tests {
#[test]
fn test_merge() {
let mut patch_init = Patch::new(&gen_id(30), "");
let mut patch_init = Patch::new("", &gen_id(30), "");
let base = "text example\n hello";
patch_init.set_base(base);
@@ -481,7 +487,7 @@ mod tests {
#[test]
fn test_transform() {
let mut patch_init = Patch::new(&gen_id(30), "");
let mut patch_init = Patch::new("", &gen_id(30), "");
let base = "text example\n hello";
patch_init.set_base(base);
@@ -528,7 +534,7 @@ mod tests {
assert_eq!(op_method, op_method_deser);
// serialize & deserialize Patch
let mut patch = Patch::new(&gen_id(30), "");
let mut patch = Patch::new("", &gen_id(30), "");
patch.insert("hello");
patch.delete(2);