From ed0dfa41aed39a89a016fe27ea0c5b73ed1c9593 Mon Sep 17 00:00:00 2001 From: PandaDEV <70103896+0PandaDEV@users.noreply.github.com> Date: Wed, 11 Dec 2024 14:30:04 +1000 Subject: [PATCH] feat: universal types for rust and ts --- src-tauri/src/utils/types.rs | 87 ++++++++++++++++++++++++++++-------- types/types.ts | 36 ++++++++++----- 2 files changed, 94 insertions(+), 29 deletions(-) diff --git a/src-tauri/src/utils/types.rs b/src-tauri/src/utils/types.rs index 5b84d87..4a3fe45 100644 --- a/src-tauri/src/utils/types.rs +++ b/src-tauri/src/utils/types.rs @@ -1,23 +1,74 @@ -#[derive(Deserialize, Serialize)] -struct HistoryItem { - id: String, - content_type: ContentType, - content: String, - favicon: String, - timestamp: DATETIME, +use serde::{Deserialize, Serialize}; +use chrono::{DateTime, Utc}; +use std::fmt; +use uuid::Uuid; + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +pub struct HistoryItem { + pub id: String, + pub content_type: ContentType, + pub content: String, + #[serde(default)] + pub favicon: Option, + pub timestamp: DateTime, } -#[derive(Deserialize, Serialize)] -struct Settings { - key: String, - value: String, +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum ContentType { + Text, + Image, + File, + Link, + Color, + Code, } -enum ContentType { - TEXT, - IMAGE, - FILE, - LINK, - COLOR, - CODE, +impl fmt::Display for ContentType { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + ContentType::Text => write!(f, "text"), + ContentType::Image => write!(f, "image"), + ContentType::File => write!(f, "file"), + ContentType::Link => write!(f, "link"), + ContentType::Color => write!(f, "color"), + ContentType::Code => write!(f, "code"), + } + } +} + +impl From for ContentType { + fn from(s: String) -> Self { + match s.to_lowercase().as_str() { + "text" => ContentType::Text, + "image" => ContentType::Image, + "file" => ContentType::File, + "link" => ContentType::Link, + "color" => ContentType::Color, + "code" => ContentType::Code, + _ => ContentType::Text, + } + } +} + +impl HistoryItem { + pub fn new(content_type: ContentType, content: String, favicon: Option) -> Self { + Self { + id: Uuid::new_v4().to_string(), + content_type, + content, + favicon, + timestamp: Utc::now(), + } + } + + pub fn to_row(&self) -> (String, String, String, Option, DateTime) { + ( + self.id.clone(), + self.content_type.to_string(), + self.content.clone(), + self.favicon.clone(), + self.timestamp, + ) + } } diff --git a/types/types.ts b/types/types.ts index f923c37..99b8cd4 100644 --- a/types/types.ts +++ b/types/types.ts @@ -1,21 +1,35 @@ -export interface HistoryItem { +import { v4 as uuidv4 } from 'uuid'; + +export enum ContentType { + Text = "text", + Image = "image", + File = "file", + Link = "link", + Color = "color", + Code = "code", +} + +export class HistoryItem { id: string; content_type: ContentType; content: string; - favicon: string; + favicon?: string; timestamp: Date; + + constructor(content_type: ContentType, content: string, favicon?: string) { + this.id = uuidv4(); + this.content_type = content_type; + this.content = content; + this.favicon = favicon; + this.timestamp = new Date(); + } + + toRow(): [string, string, string, string | undefined, Date] { + return [this.id, this.content_type, this.content, this.favicon, this.timestamp]; + } } export interface Settings { key: string; value: string; } - -export enum ContentType { - TEXT, - IMAGE, - FILE, - LINK, - COLOR, - CODE, -}