-
Notifications
You must be signed in to change notification settings - Fork 2
Update paste.rs #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Muhindo-Galien
wants to merge
1
commit into
setbap:main
Choose a base branch
from
Muhindo-Galien:patch-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<Principal>, | ||
| // 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<String>, | ||
| } | ||
|
|
||
|
|
@@ -57,17 +52,63 @@ pub struct PasteDataUpdater { | |
| pub tags: Option<String>, | ||
| } | ||
|
|
||
| impl PasteData { | ||
| pub fn create(id: u64, user_id: Option<Principal>, 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(); | ||
| } | ||
| } | ||
|
Comment on lines
+55
to
+93
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i don't understand what is your changes. is it git diff problem? |
||
|
|
||
| #[derive(CandidType, Deserialize, Serialize)] | ||
| pub enum IcpPasteError { | ||
| ShortUrlShouldBeBetween4And10, | ||
| ShortUrlAlreadyExist, | ||
| PasteNotFound, | ||
| PasteAlreadyExist, | ||
| PasteIsNotAccessable, | ||
| WrongExpireDate, | ||
| } | ||
|
|
||
| fn _create_tags(input: String) -> Vec<String> { | ||
| input | ||
| .trim() | ||
| .split(" ") | ||
| .filter_map(|v| { | ||
| if v.is_empty() { | ||
| return None; | ||
| } | ||
| return Some(v.to_string()); | ||
| }) | ||
| .collect::<Vec<String>>() | ||
| .split_whitespace() | ||
| .filter(|v| !v.is_empty()) | ||
| .map(String::from) | ||
| .collect() | ||
|
Comment on lines
+108
to
+111
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is much nice and cleaner. thanks |
||
| } | ||
|
|
||
| impl PasteData { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😉