From 785aaf2c113f6a91e957b2707fd07bc2df293bb4 Mon Sep 17 00:00:00 2001 From: Muhindo Galien <74584848+Muhindo-Galien@users.noreply.github.com> Date: Sat, 23 Dec 2023 16:16:36 +0300 Subject: [PATCH] Update paste.rs --- src/icpbin_backend/src/paste.rs | 75 +++++++++++++++++++++++++-------- 1 file changed, 58 insertions(+), 17 deletions(-) diff --git a/src/icpbin_backend/src/paste.rs b/src/icpbin_backend/src/paste.rs index d6fabfd..0263697 100644 --- a/src/icpbin_backend/src/paste.rs +++ b/src/icpbin_backend/src/paste.rs @@ -4,15 +4,15 @@ use serde::{Deserialize, Serialize}; use std::borrow::Cow; pub const MAX_PASTE_VALUE_SIZE: u32 = 16 * 1024; -pub const DELETE_TEPMLATE: &str = "DELETE"; +pub const DELETE_TEMPLATE: &str = "DELETE"; -// allow to store Paste Data in the stable memory +// Allow storing PasteData in stable memory impl Storable for PasteData { - fn to_bytes(&self) -> std::borrow::Cow<[u8]> { + fn to_bytes(&self) -> Cow<[u8]> { Cow::Owned(Encode!(self).unwrap()) } - fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self { + fn from_bytes(bytes: Cow<[u8]>) -> Self { Decode!(bytes.as_ref(), Self).unwrap() } @@ -24,18 +24,13 @@ impl Storable for PasteData { #[derive(CandidType, Serialize, Deserialize, Clone)] pub struct PasteData { - // id is the unique identifier of the paste pub id: String, pub name: String, pub description: String, pub content: String, - // creator is the principal who created the paste and it will be null when the paste is created by not logged in user pub creator: Option, - // track number of change in paste pub version: i32, - // time to convert date to delete, it is base on second pub expire_date: u32, - // container to push any extra data about PasteData pub tags: Vec, } @@ -57,17 +52,63 @@ pub struct PasteDataUpdater { pub tags: Option, } +impl PasteData { + pub fn create(id: u64, user_id: Option, info: PasteDataCreator) -> Self { + PasteData { + id: id.to_string(), + name: info.name, + creator: user_id, + description: info.description, + expire_date: info.expire_date, + content: info.content, + tags: _create_tags(info.tags), + version: 1, + } + } + + pub fn update(&mut self, info: PasteDataUpdater) { + if let Some(name) = info.name { + self.name = name; + } + if let Some(desc) = info.description { + self.description = desc; + } + if let Some(content) = info.content { + self.content = content; + } + if let Some(tags) = info.tags { + self.tags = _create_tags(tags); + } + + // Increase the number of changes + self.version += 1; + } + + // Clear the content of the paste + pub fn clear(&mut self) { + self.name = DELETE_TEMPLATE.to_string(); + self.content = DELETE_TEMPLATE.to_string(); + self.tags = Vec::new(); + } +} + +#[derive(CandidType, Deserialize, Serialize)] +pub enum IcpPasteError { + ShortUrlShouldBeBetween4And10, + ShortUrlAlreadyExist, + PasteNotFound, + PasteAlreadyExist, + PasteIsNotAccessable, + WrongExpireDate, +} + fn _create_tags(input: String) -> Vec { input .trim() - .split(" ") - .filter_map(|v| { - if v.is_empty() { - return None; - } - return Some(v.to_string()); - }) - .collect::>() + .split_whitespace() + .filter(|v| !v.is_empty()) + .map(String::from) + .collect() } impl PasteData {