Skip to content

Commit

Permalink
WASM experiments with OOP
Browse files Browse the repository at this point in the history
  • Loading branch information
SHAcollision committed Jan 11, 2025
1 parent 0484f0c commit 94c9d07
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
6 changes: 4 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mime = "0.3"
utoipa = { git = "https://github.com/juhaku/utoipa", rev = "d522f744259dc4fde5f45d187983fb68c8167029", optional = true }
wasm-bindgen = "0.2.99"
serde-wasm-bindgen = "0.6.5"
js-sys = "0.3.76"

[dev-dependencies]
pubky = "0.3.0"
Expand Down
21 changes: 20 additions & 1 deletion src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::common::timestamp;
use base32::{decode, encode, Alphabet};
use blake3::Hasher;
use serde::de::DeserializeOwned;
use serde::{de::DeserializeOwned, Serialize};
use wasm_bindgen::prelude::*;

pub trait TimestampId {
/// Creates a unique identifier based on the current timestamp.
Expand Down Expand Up @@ -124,3 +125,21 @@ pub trait HasPath {
pub trait HasPubkyIdPath {
fn create_path(&self, pubky_id: &str) -> String;
}

pub trait JSdata: HasPath + Serialize {
// helper that returns { id, path, json }
fn get_data(&self) -> Result<JsValue, JsValue> {
let path = self.create_path();

let json_val = serde_wasm_bindgen::to_value(&self)
.map_err(|e| JsValue::from_str(&format!("JSON Error: {}", e)))?;

// Construct a small JS object
let obj = js_sys::Object::new();
js_sys::Reflect::set(&obj, &JsValue::from_str("id"), &JsValue::null())?;
js_sys::Reflect::set(&obj, &JsValue::from_str("path"), &JsValue::from_str(&path))?;
js_sys::Reflect::set(&obj, &JsValue::from_str("json"), &json_val)?;

Ok(obj.into())
}
}
31 changes: 30 additions & 1 deletion src/user.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::{
traits::{HasPath, Validatable},
traits::{HasPath, JSdata, Validatable},
APP_PATH,
};
use serde::{Deserialize, Serialize};
use url::Url;
use wasm_bindgen::prelude::*;

// Validation constants
const MIN_USERNAME_LENGTH: usize = 3;
Expand All @@ -19,26 +20,38 @@ const MAX_STATUS_LENGTH: usize = 50;
use utoipa::ToSchema;

/// URI: /pub/pubky.app/profile.json
#[wasm_bindgen]
#[derive(Deserialize, Serialize, Debug, Default, Clone)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub struct PubkyAppUser {
#[wasm_bindgen(skip)]
// Avoid wasm-pack automatically generating getter/setters for the pub fields.
pub name: String,
#[wasm_bindgen(skip)]
pub bio: Option<String>,
#[wasm_bindgen(skip)]
pub image: Option<String>,
#[wasm_bindgen(skip)]
pub links: Option<Vec<PubkyAppUserLink>>,
#[wasm_bindgen(skip)]
pub status: Option<String>,
}

/// Represents a user's single link with a title and URL.
#[wasm_bindgen]
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub struct PubkyAppUserLink {
#[wasm_bindgen(skip)]
pub title: String,
#[wasm_bindgen(skip)]
pub url: String,
}

#[wasm_bindgen]
impl PubkyAppUser {
/// Creates a new `PubkyAppUser` instance and sanitizes it.
#[wasm_bindgen(constructor)]
pub fn new(
name: String,
bio: Option<String>,
Expand All @@ -55,8 +68,15 @@ impl PubkyAppUser {
}
.sanitize()
}

#[wasm_bindgen]
pub fn get_data(&self) -> Result<JsValue, JsValue> {
JSdata::get_data(self)
}
}

impl JSdata for PubkyAppUser {}

impl HasPath for PubkyAppUser {
fn create_path(&self) -> String {
format!("{}profile.json", APP_PATH)
Expand Down Expand Up @@ -171,6 +191,15 @@ impl Validatable for PubkyAppUser {
}
}

#[wasm_bindgen]
impl PubkyAppUserLink {
/// Creates a new `PubkyAppUserLink` instance and sanitizes it.
#[wasm_bindgen(constructor)]
pub fn new(title: String, url: String) -> Self {
Self { title, url }.sanitize()
}
}

impl Validatable for PubkyAppUserLink {
fn sanitize(self) -> Self {
let title = self
Expand Down

0 comments on commit 94c9d07

Please sign in to comment.