You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yuurei/src/state/local.rs

92 lines
2.5 KiB

//! Handles updating posts
use super::*;
use tokio::{
sync::{
RwLock,
watch,
},
};
const MAX_SINGLE_DELTA_SIZE: usize = 16;
/// Information about the delta to be applied in `Delta`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DeltaKind
{
/// Append to the end of body. Equivilant to `Insert` with a location at `karada.scape.len()` (the end of the buffer). Might be removed idk
Append{
span: [char; MAX_SINGLE_DELTA_SIZE],
span_len: u8,
},
/// Insert `span_len` chars from `span` into body starting ahead of `location` and moving ahead
Insert{
span: [char; MAX_SINGLE_DELTA_SIZE],
span_len: u8,
},
/// Remove `span_len` chars ahead of `location`. (INCLUSIVE)
RemoveAhead{
span_len: usize,
},
/// Remove `span_len` chars behind this `location`. (EXCLUSIVE)
RemoveBehind{
span_len: usize,
},
/// Remove char at `location`
RemoveSingle,
/// Remove entire post body
Clear,
}
/// A delta to apply to `Karada`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Delta
{
/// Location to insert into. This is the INclusive range of: 0..=(karada.scape.len()).
///
/// Insertions off the end of the buffer are to be appened instead.
location: usize,
/// The kind of delta t oinsert
kind: DeltaKind,
}
/// Static assertion: `MAX_SINGLE_DELTA_SIZE` can fit into `u8`.
const _: [u8;(MAX_SINGLE_DELTA_SIZE < (!0u8 as usize)) as usize] = [0];
/// Contains post deltas and an intermediate representation of the still-open post body.
/// Created and modified with `Kokoro` worker instances.
///
/// This should not be created by itself, instead `Kokoro` should create instances of this, so that it can retain the `watch::Sender` and other such things.
#[derive(Debug)]
pub struct Karada
{
/// The post body so far as a vector of `char`s.
scape: RwLock<Vec<char>>,
/// All applied deltas so far. Last applied one is at the end.
deltas: RwLock<Vec<Delta>>,
/// the latest render of the whole body string. Updated whenever a delta(s) are applied atomically.
current_body: watch::Receiver<String>,
}
/// Handles working on `Karada` instances.
pub struct Kokoro
{
...//TODO
}
/// An open, as yet unfinied post
#[derive(Debug)]
pub struct Imouto
{
id: identity::PostID,
user: identity::User,
title: Option<String>,
karada: Karada,
/// Hash of the current post data
hash: crypto::sha256::Sha256Hash,
timestamp: post::PostTimestamp, //Note that `closed` should always be `None` in `Imouto`. We use this for post lifetimes and such
}