feat: universal types for rust and ts

This commit is contained in:
PandaDEV 2024-12-11 14:30:04 +10:00
parent a94496dbdb
commit ed0dfa41ae
No known key found for this signature in database
GPG key ID: 13EFF9BAF70EE75C
2 changed files with 94 additions and 29 deletions

View file

@ -1,23 +1,74 @@
#[derive(Deserialize, Serialize)] use serde::{Deserialize, Serialize};
struct HistoryItem { use chrono::{DateTime, Utc};
id: String, use std::fmt;
content_type: ContentType, use uuid::Uuid;
content: String,
favicon: String, #[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
timestamp: DATETIME, pub struct HistoryItem {
pub id: String,
pub content_type: ContentType,
pub content: String,
#[serde(default)]
pub favicon: Option<String>,
pub timestamp: DateTime<Utc>,
} }
#[derive(Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
struct Settings { #[serde(rename_all = "lowercase")]
key: String, pub enum ContentType {
value: String, Text,
Image,
File,
Link,
Color,
Code,
} }
enum ContentType { impl fmt::Display for ContentType {
TEXT, fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
IMAGE, match self {
FILE, ContentType::Text => write!(f, "text"),
LINK, ContentType::Image => write!(f, "image"),
COLOR, ContentType::File => write!(f, "file"),
CODE, ContentType::Link => write!(f, "link"),
ContentType::Color => write!(f, "color"),
ContentType::Code => write!(f, "code"),
}
}
}
impl From<String> 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<String>) -> Self {
Self {
id: Uuid::new_v4().to_string(),
content_type,
content,
favicon,
timestamp: Utc::now(),
}
}
pub fn to_row(&self) -> (String, String, String, Option<String>, DateTime<Utc>) {
(
self.id.clone(),
self.content_type.to_string(),
self.content.clone(),
self.favicon.clone(),
self.timestamp,
)
}
} }

View file

@ -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; id: string;
content_type: ContentType; content_type: ContentType;
content: string; content: string;
favicon: string; favicon?: string;
timestamp: Date; 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 { export interface Settings {
key: string; key: string;
value: string; value: string;
} }
export enum ContentType {
TEXT,
IMAGE,
FILE,
LINK,
COLOR,
CODE,
}