Extract types into a separate module

This commit is contained in:
Andras Schmelczer 2025-06-29 16:22:32 +01:00
parent 806a5bc113
commit b18a692d46
8 changed files with 57 additions and 32 deletions

29
src/types/history.rs Normal file
View file

@ -0,0 +1,29 @@
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg(feature = "wasm")]
pub enum History {
Unchanged = "Unchanged",
AddedFromLeft = "AddedFromLeft",
AddedFromRight = "AddedFromRight",
RemovedFromLeft = "RemovedFromLeft",
RemovedFromRight = "RemovedFromRight",
}
/// Simple enum for describing the result of `reconcile` in a flat list.
/// When compiled to WASM, the enum values are the same as their names.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg(not(feature = "wasm"))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum History {
Unchanged,
AddedFromLeft,
AddedFromRight,
RemovedFromLeft,
RemovedFromRight,
}

22
src/types/side.rs Normal file
View file

@ -0,0 +1,22 @@
use std::fmt::Display;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
/// Pretty-printable flag to tell which conflicting edit (side)
/// an operation is associated with.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Side {
Left,
Right,
}
impl Display for Side {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Side::Left => write!(f, "Left"),
Side::Right => write!(f, "Right"),
}
}
}

View file

@ -0,0 +1,26 @@
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;
use crate::types::history::History;
/// Wrapper type to expose `(History, String)` to JS.
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct TextWithHistory {
history: History,
text: String,
}
#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl TextWithHistory {
pub fn new(history: History, text: String) -> Self { TextWithHistory { history, text } }
#[must_use]
pub fn history(&self) -> History { self.history }
#[must_use]
pub fn text(&self) -> String { self.text.clone() }
}