Extract binary merging logic

This commit is contained in:
Andras Schmelczer 2024-12-12 22:07:36 +00:00
commit 10bc8c7099
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
9 changed files with 55 additions and 57 deletions

View file

@ -1,16 +1,29 @@
use std::str::Utf8Error;
use base64::DecodeError; use base64::DecodeError;
use thiserror::Error; use thiserror::Error;
use wasm_bindgen::JsValue; use wasm_bindgen::JsValue;
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum SyncLibError { pub enum SyncLibError {
#[error("Base64 decoding error: {}", .reason)] #[error("Base64 decoding error because of {}", .reason)]
DecodingError { reason: String }, Base64DecodingError { reason: String },
#[error("Bytes cannot be decoded as UTF-8 string because of {}", .reason)]
StringDecodingError { reason: String },
} }
impl From<DecodeError> for SyncLibError { impl From<DecodeError> for SyncLibError {
fn from(e: DecodeError) -> Self { fn from(e: DecodeError) -> Self {
SyncLibError::DecodingError { SyncLibError::Base64DecodingError {
reason: e.to_string(),
}
}
}
impl From<Utf8Error> for SyncLibError {
fn from(e: Utf8Error) -> Self {
SyncLibError::StringDecodingError {
reason: e.to_string(), reason: e.to_string(),
} }
} }
@ -18,7 +31,7 @@ impl From<DecodeError> for SyncLibError {
impl From<std::string::FromUtf8Error> for SyncLibError { impl From<std::string::FromUtf8Error> for SyncLibError {
fn from(e: std::string::FromUtf8Error) -> Self { fn from(e: std::string::FromUtf8Error) -> Self {
SyncLibError::DecodingError { SyncLibError::Base64DecodingError {
reason: e.to_string(), reason: e.to_string(),
} }
} }

View file

@ -1,3 +1,5 @@
use core::str;
use base64::{engine::general_purpose::STANDARD_NO_PAD, Engine as _}; use base64::{engine::general_purpose::STANDARD_NO_PAD, Engine as _};
use errors::SyncLibError; use errors::SyncLibError;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
@ -27,6 +29,20 @@ pub fn base64_to_string(input: &str) -> Result<String, SyncLibError> {
String::from_utf8(bytes).map_err(SyncLibError::from) String::from_utf8(bytes).map_err(SyncLibError::from)
} }
#[wasm_bindgen]
pub fn merge(parent: &[u8], left: &[u8], right: &[u8]) -> Result<Vec<u8>, SyncLibError> {
Ok(if is_binary(right) {
right.to_vec()
} else {
reconcile::reconcile(
str::from_utf8(parent).map_err(SyncLibError::from)?,
str::from_utf8(left).map_err(SyncLibError::from)?,
str::from_utf8(right).map_err(SyncLibError::from)?,
)
.into_bytes()
})
}
#[wasm_bindgen] #[wasm_bindgen]
pub fn is_binary(data: &[u8]) -> bool { data.iter().any(|&b| b == 0) } pub fn is_binary(data: &[u8]) -> bool { data.iter().any(|&b| b == 0) }

View file

@ -70,7 +70,6 @@ impl Database {
created_date as "created_date: chrono::DateTime<Utc>", created_date as "created_date: chrono::DateTime<Utc>",
updated_date as "updated_date: chrono::DateTime<Utc>", updated_date as "updated_date: chrono::DateTime<Utc>",
relative_path, relative_path,
is_binary,
is_deleted is_deleted
from latest_documents from latest_documents
where is_deleted = false and vault_id = ? where is_deleted = false and vault_id = ?
@ -103,7 +102,6 @@ impl Database {
updated_date as "updated_date: chrono::DateTime<Utc>", updated_date as "updated_date: chrono::DateTime<Utc>",
relative_path, relative_path,
content, content,
is_binary,
is_deleted is_deleted
from latest_documents from latest_documents
where vault_id = ? and document_id = ? where vault_id = ? and document_id = ?
@ -137,7 +135,6 @@ impl Database {
updated_date as "updated_date: chrono::DateTime<Utc>", updated_date as "updated_date: chrono::DateTime<Utc>",
relative_path, relative_path,
content, content,
is_binary,
is_deleted is_deleted
from latest_documents from latest_documents
where vault_id = ? and relative_path = ? and is_deleted = false where vault_id = ? and relative_path = ? and is_deleted = false
@ -172,7 +169,6 @@ impl Database {
updated_date as "updated_date: chrono::DateTime<Utc>", updated_date as "updated_date: chrono::DateTime<Utc>",
relative_path, relative_path,
content, content,
is_binary,
is_deleted is_deleted
from documents from documents
where vault_id = ? and document_id = ? and version_id = ?"#, where vault_id = ? and document_id = ? and version_id = ?"#,
@ -204,10 +200,9 @@ impl Database {
updated_date, updated_date,
relative_path, relative_path,
content, content,
is_binary,
is_deleted is_deleted
) )
values (?, ?, ?, ?, ?, ?, ?, ?, ?) values (?, ?, ?, ?, ?, ?, ?, ?)
"#, "#,
version.vault_id, version.vault_id,
version.document_id, version.document_id,
@ -216,7 +211,6 @@ impl Database {
version.updated_date, version.updated_date,
version.relative_path, version.relative_path,
version.content, version.content,
version.is_binary,
version.is_deleted version.is_deleted
); );

View file

@ -16,14 +16,9 @@ pub struct StoredDocumentVersion {
pub updated_date: DateTime<Utc>, pub updated_date: DateTime<Utc>,
pub relative_path: String, pub relative_path: String,
pub content: Vec<u8>, pub content: Vec<u8>,
pub is_binary: bool,
pub is_deleted: bool, pub is_deleted: bool,
} }
impl StoredDocumentVersion {
pub fn content_as_string(&self) -> String { String::from_utf8_lossy(&self.content).to_string() }
}
#[derive(Debug, Clone, Serialize, JsonSchema)] #[derive(Debug, Clone, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct DocumentVersionWithoutContent { pub struct DocumentVersionWithoutContent {
@ -33,7 +28,6 @@ pub struct DocumentVersionWithoutContent {
pub created_date: DateTime<Utc>, pub created_date: DateTime<Utc>,
pub updated_date: DateTime<Utc>, pub updated_date: DateTime<Utc>,
pub relative_path: String, pub relative_path: String,
pub is_binary: bool,
pub is_deleted: bool, pub is_deleted: bool,
} }
@ -46,7 +40,6 @@ impl From<StoredDocumentVersion> for DocumentVersionWithoutContent {
created_date: value.created_date, created_date: value.created_date,
updated_date: value.updated_date, updated_date: value.updated_date,
relative_path: value.relative_path, relative_path: value.relative_path,
is_binary: value.is_binary,
is_deleted: value.is_deleted, is_deleted: value.is_deleted,
} }
} }
@ -69,7 +62,6 @@ pub struct DocumentVersion {
pub updated_date: DateTime<Utc>, pub updated_date: DateTime<Utc>,
pub relative_path: String, pub relative_path: String,
pub content_base64: String, pub content_base64: String,
pub is_binary: bool,
pub is_deleted: bool, pub is_deleted: bool,
} }
@ -83,7 +75,6 @@ impl From<StoredDocumentVersion> for DocumentVersion {
updated_date: value.updated_date, updated_date: value.updated_date,
relative_path: value.relative_path, relative_path: value.relative_path,
content_base64: bytes_to_base64(&value.content), content_base64: bytes_to_base64(&value.content),
is_binary: value.is_binary,
is_deleted: value.is_deleted, is_deleted: value.is_deleted,
} }
} }

View file

@ -9,7 +9,7 @@ use axum_extra::{
}; };
use schemars::JsonSchema; use schemars::JsonSchema;
use serde::Deserialize; use serde::Deserialize;
use sync_lib::{base64_to_bytes, base64_to_string}; use sync_lib::{base64_to_bytes, merge};
use super::{auth::auth, requests::CreateDocumentVersion}; use super::{auth::auth, requests::CreateDocumentVersion};
use crate::{ use crate::{
@ -53,20 +53,17 @@ pub async fn create_document(
.map_err(server_error)?; .map_err(server_error)?;
let new_version = if let Some(existing_version) = maybe_existing_version { let new_version = if let Some(existing_version) = maybe_existing_version {
let merged_content = if request.is_binary { let content_bytes = base64_to_bytes(&request.content_base64)
base64_to_bytes(&request.content_base64)
.context("Failed to decode base64 content in request") .context("Failed to decode base64 content in request")
.map_err(client_error)? .map_err(client_error)?;
} else {
reconcile::reconcile( let merged_content = merge(
"", // the empty string is the first common parent of the two documents &[], // the empty string is the first common parent of the two documents,
&existing_version.content_as_string(), &existing_version.content,
&base64_to_string(&request.content_base64) &content_bytes,
.context("Failed to decode base64 content in request")
.map_err(client_error)?,
) )
.into_bytes() .context("Failed to decode bytes as UTF-8")
}; .map_err(client_error)?;
StoredDocumentVersion { StoredDocumentVersion {
vault_id, vault_id,
@ -76,7 +73,6 @@ pub async fn create_document(
created_date: request.created_date, created_date: request.created_date,
relative_path: request.relative_path, relative_path: request.relative_path,
updated_date: chrono::Utc::now(), updated_date: chrono::Utc::now(),
is_binary: request.is_binary,
is_deleted: false, is_deleted: false,
} }
} else { } else {
@ -90,7 +86,6 @@ pub async fn create_document(
created_date: request.created_date, created_date: request.created_date,
relative_path: request.relative_path, relative_path: request.relative_path,
updated_date: chrono::Utc::now(), updated_date: chrono::Utc::now(),
is_binary: request.is_binary,
is_deleted: false, is_deleted: false,
} }
}; };

View file

@ -63,7 +63,6 @@ pub async fn delete_document(
created_date: request.created_date, created_date: request.created_date,
updated_date: chrono::Utc::now(), updated_date: chrono::Utc::now(),
relative_path: latest_version.relative_path, relative_path: latest_version.relative_path,
is_binary: latest_version.is_binary,
is_deleted: true, is_deleted: true,
}; };

View file

@ -4,7 +4,7 @@ use axum_extra::{
TypedHeader, TypedHeader,
}; };
use super::auth::{self, auth}; use super::auth::auth;
use crate::{app_state::AppState, database::models::PingResponse, errors::SyncServerError}; use crate::{app_state::AppState, database::models::PingResponse, errors::SyncServerError};
#[axum::debug_handler] #[axum::debug_handler]

View file

@ -10,7 +10,6 @@ pub struct CreateDocumentVersion {
pub created_date: DateTime<Utc>, pub created_date: DateTime<Utc>,
pub relative_path: String, pub relative_path: String,
pub content_base64: String, pub content_base64: String,
pub is_binary: bool,
} }
#[derive(Debug, Deserialize, JsonSchema)] #[derive(Debug, Deserialize, JsonSchema)]
@ -20,7 +19,6 @@ pub struct UpdateDocumentVersion {
pub created_date: DateTime<Utc>, pub created_date: DateTime<Utc>,
pub relative_path: String, pub relative_path: String,
pub content_base64: String, pub content_base64: String,
pub is_binary: bool,
} }
#[derive(Debug, Deserialize, JsonSchema)] #[derive(Debug, Deserialize, JsonSchema)]

View file

@ -9,7 +9,7 @@ use axum_extra::{
}; };
use schemars::JsonSchema; use schemars::JsonSchema;
use serde::Deserialize; use serde::Deserialize;
use sync_lib::{base64_to_bytes, base64_to_string}; use sync_lib::{base64_to_bytes, merge};
use super::{auth::auth, requests::UpdateDocumentVersion}; use super::{auth::auth, requests::UpdateDocumentVersion};
use crate::{ use crate::{
@ -76,20 +76,13 @@ pub async fn update_document(
))); )));
} }
let merged_content = if request.is_binary { let content_bytes = base64_to_bytes(&request.content_base64)
base64_to_bytes(&request.content_base64)
.context("Failed to decode base64 content in request") .context("Failed to decode base64 content in request")
.map_err(client_error)? .map_err(client_error)?;
} else {
reconcile::reconcile( let merged_content = merge(&parent.content, &latest_version.content, &content_bytes)
&parent.content_as_string(), .context("Failed to decode bytes as UTF-8")
&latest_version.content_as_string(), .map_err(client_error)?;
&base64_to_string(&request.content_base64)
.context("Failed to decode base64 content in request")
.map_err(client_error)?,
)
.into_bytes()
};
let new_version = StoredDocumentVersion { let new_version = StoredDocumentVersion {
vault_id, vault_id,
@ -99,7 +92,6 @@ pub async fn update_document(
created_date: request.created_date, created_date: request.created_date,
relative_path: request.relative_path, relative_path: request.relative_path,
updated_date: chrono::Utc::now(), updated_date: chrono::Utc::now(),
is_binary: request.is_binary,
is_deleted: false, is_deleted: false,
}; };