Skip to content

Commit

Permalink
Add uri parser
Browse files Browse the repository at this point in the history
  • Loading branch information
SHAcollision committed Feb 2, 2025
1 parent 9278a99 commit 353a815
Show file tree
Hide file tree
Showing 16 changed files with 199 additions and 34 deletions.
3 changes: 2 additions & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub static VERSION: &str = "0.3.0";
pub static APP_PATH: &str = "/pub/pubky.app/";
pub static PUBLIC_PATH: &str = "/pub/";
pub static APP_PATH: &str = "pubky.app/";
pub static PROTOCOL: &str = "pubky://";

#[cfg(target_arch = "wasm32")]
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ mod common;
mod models;
pub mod traits;
mod types;
mod uri_parser;
mod utils;

// Re-export domain types
pub use common::{APP_PATH, PROTOCOL, VERSION};
pub use common::{APP_PATH, PROTOCOL, PUBLIC_PATH, VERSION};
pub use models::bookmark::PubkyAppBookmark;
pub use models::feed::{PubkyAppFeed, PubkyAppFeedLayout, PubkyAppFeedReach, PubkyAppFeedSort};
pub use models::file::PubkyAppFile;
Expand All @@ -17,6 +18,7 @@ pub use models::post::{PubkyAppPost, PubkyAppPostEmbed, PubkyAppPostKind};
pub use models::tag::PubkyAppTag;
pub use models::user::{PubkyAppUser, PubkyAppUserLink};
pub use types::PubkyId;
pub use uri_parser::ParsedUri;
pub use utils::*;

// Our WASM module
Expand Down
8 changes: 5 additions & 3 deletions src/models/bookmark.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
common::timestamp,
traits::{HasPath, HashId, Validatable},
APP_PATH,
APP_PATH, PUBLIC_PATH,
};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -65,8 +65,10 @@ impl HashId for PubkyAppBookmark {
}

impl HasPath for PubkyAppBookmark {
const PATH_SEGMENT: &'static str = "bookmarks/";

fn create_path(&self) -> String {
format!("{}bookmarks/{}", APP_PATH, self.create_id())
[PUBLIC_PATH, APP_PATH, Self::PATH_SEGMENT, &self.create_id()].concat()
}
}

Expand Down Expand Up @@ -101,7 +103,7 @@ mod tests {
created_at: 1627849723,
};
let expected_id = bookmark.create_id();
let expected_path = format!("{}bookmarks/{}", APP_PATH, expected_id);
let expected_path = format!("{}{}bookmarks/{}", PUBLIC_PATH, APP_PATH, expected_id);
let path = bookmark.create_path();
assert_eq!(path, expected_path);
}
Expand Down
6 changes: 4 additions & 2 deletions src/models/feed.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
common::timestamp,
traits::{HasPath, HashId, Validatable},
PubkyAppPostKind, APP_PATH,
PubkyAppPostKind, APP_PATH, PUBLIC_PATH,
};
use serde::{Deserialize, Serialize};
use std::str::FromStr;
Expand Down Expand Up @@ -179,8 +179,10 @@ impl HashId for PubkyAppFeed {
}

impl HasPath for PubkyAppFeed {
const PATH_SEGMENT: &'static str = "feeds/";

fn create_path(&self) -> String {
format!("{}feeds/{}", APP_PATH, self.create_id())
[PUBLIC_PATH, APP_PATH, Self::PATH_SEGMENT, &self.create_id()].concat()
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/models/file.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
common::timestamp,
traits::{HasPath, TimestampId, Validatable},
APP_PATH,
APP_PATH, PUBLIC_PATH,
};
use mime::Mime;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -104,8 +104,10 @@ impl PubkyAppFile {
impl TimestampId for PubkyAppFile {}

impl HasPath for PubkyAppFile {
const PATH_SEGMENT: &'static str = "files/";

fn create_path(&self) -> String {
format!("{}files/{}", APP_PATH, self.create_id())
[PUBLIC_PATH, APP_PATH, Self::PATH_SEGMENT, &self.create_id()].concat()
}
}

Expand Down Expand Up @@ -208,7 +210,7 @@ mod tests {
let path = file.create_path();

// Check if the path starts with the expected prefix
let prefix = format!("{}files/", APP_PATH);
let prefix = format!("{}{}files/", PUBLIC_PATH, APP_PATH);
assert!(path.starts_with(&prefix));

let expected_path_len = prefix.len() + file_id.len();
Expand Down
6 changes: 4 additions & 2 deletions src/models/file_blob.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
traits::{HasPath, HashId, Validatable},
APP_PATH,
APP_PATH, PUBLIC_PATH,
};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -67,8 +67,10 @@ impl HashId for PubkyAppBlob {
}

impl HasPath for PubkyAppBlob {
const PATH_SEGMENT: &'static str = "blobs/";

fn create_path(&self) -> String {
format!("{}blobs/{}", APP_PATH, self.create_id())
[PUBLIC_PATH, APP_PATH, Self::PATH_SEGMENT, &self.create_id()].concat()
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/models/follow.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
common::timestamp,
traits::{HasPubkyIdPath, Validatable},
APP_PATH,
APP_PATH, PUBLIC_PATH,
};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -61,8 +61,10 @@ impl Validatable for PubkyAppFollow {
}

impl HasPubkyIdPath for PubkyAppFollow {
const PATH_SEGMENT: &'static str = "follows/";

fn create_path(&self, pubky_id: &str) -> String {
format!("{}follows/{}", APP_PATH, pubky_id)
[PUBLIC_PATH, APP_PATH, Self::PATH_SEGMENT, pubky_id].concat()
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/models/last_read.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
common::timestamp,
traits::{HasPath, Validatable},
APP_PATH,
APP_PATH, PUBLIC_PATH,
};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -54,8 +54,10 @@ impl Validatable for PubkyAppLastRead {
}

impl HasPath for PubkyAppLastRead {
const PATH_SEGMENT: &'static str = "last_read";

fn create_path(&self) -> String {
format!("{}last_read", APP_PATH)
[PUBLIC_PATH, APP_PATH, Self::PATH_SEGMENT].concat()
}
}

Expand All @@ -76,7 +78,7 @@ mod tests {
fn test_create_path() {
let last_read = PubkyAppLastRead::new();
let path = last_read.create_path();
assert_eq!(path, format!("{}last_read", APP_PATH));
assert_eq!(path, format!("{}{}last_read", PUBLIC_PATH, APP_PATH));
}

#[test]
Expand Down
6 changes: 4 additions & 2 deletions src/models/mute.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
common::timestamp,
traits::{HasPubkyIdPath, Validatable},
APP_PATH,
APP_PATH, PUBLIC_PATH,
};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -55,8 +55,10 @@ impl Validatable for PubkyAppMute {
}

impl HasPubkyIdPath for PubkyAppMute {
const PATH_SEGMENT: &'static str = "mutes/";

fn create_path(&self, pubky_id: &str) -> String {
format!("{}mutes/{}", APP_PATH, pubky_id)
[PUBLIC_PATH, APP_PATH, Self::PATH_SEGMENT, pubky_id].concat()
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/models/post.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
traits::{HasPath, TimestampId, Validatable},
APP_PATH,
APP_PATH, PUBLIC_PATH,
};
use serde::{Deserialize, Serialize};
use std::{fmt, str::FromStr};
Expand Down Expand Up @@ -175,8 +175,10 @@ impl PubkyAppPost {
impl TimestampId for PubkyAppPost {}

impl HasPath for PubkyAppPost {
const PATH_SEGMENT: &'static str = "posts/";

fn create_path(&self) -> String {
format!("{}posts/{}", APP_PATH, self.create_id())
[PUBLIC_PATH, APP_PATH, Self::PATH_SEGMENT, &self.create_id()].concat()
}
}

Expand Down Expand Up @@ -266,7 +268,7 @@ impl Validatable for PubkyAppPost {
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::Validatable;
use crate::{traits::Validatable, PUBLIC_PATH};

#[test]
fn test_create_id() {
Expand Down Expand Up @@ -312,7 +314,7 @@ mod tests {
let path = post.create_path();

// Check if the path starts with the expected prefix
let prefix = format!("{}posts/", APP_PATH);
let prefix = format!("{}{}posts/", PUBLIC_PATH, APP_PATH);
assert!(path.starts_with(&prefix));

let expected_path_len = prefix.len() + post_id.len();
Expand Down
8 changes: 5 additions & 3 deletions src/models/tag.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
common::timestamp,
traits::{HasPath, HashId, Validatable},
APP_PATH,
APP_PATH, PUBLIC_PATH,
};
use serde::{Deserialize, Serialize};
use url::Url;
Expand Down Expand Up @@ -75,8 +75,10 @@ impl PubkyAppTag {
impl ToJson for PubkyAppTag {}

impl HasPath for PubkyAppTag {
const PATH_SEGMENT: &'static str = "tags/";

fn create_path(&self) -> String {
format!("{}tags/{}", APP_PATH, self.create_id())
[PUBLIC_PATH, APP_PATH, Self::PATH_SEGMENT, &self.create_id()].concat()
}
}

Expand Down Expand Up @@ -217,7 +219,7 @@ mod tests {
};

let expected_id = tag.create_id();
let expected_path = format!("{}tags/{}", APP_PATH, expected_id);
let expected_path = format!("{}{}tags/{}", PUBLIC_PATH, APP_PATH, expected_id);
let path = tag.create_path();

assert_eq!(path, expected_path);
Expand Down
10 changes: 6 additions & 4 deletions src/models/user.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
traits::{HasPath, Validatable},
APP_PATH,
APP_PATH, PUBLIC_PATH,
};
use serde::{Deserialize, Serialize};
use url::Url;
Expand Down Expand Up @@ -122,8 +122,10 @@ impl PubkyAppUser {
}

impl HasPath for PubkyAppUser {
const PATH_SEGMENT: &'static str = "profile.json";

fn create_path(&self) -> String {
format!("{}profile.json", APP_PATH)
[PUBLIC_PATH, APP_PATH, Self::PATH_SEGMENT].concat()
}
}

Expand Down Expand Up @@ -287,7 +289,7 @@ impl Validatable for PubkyAppUserLink {
mod tests {
use super::*;
use crate::traits::Validatable;
use crate::APP_PATH;
use crate::{APP_PATH, PUBLIC_PATH};

#[test]
fn test_new() {
Expand Down Expand Up @@ -323,7 +325,7 @@ mod tests {
fn test_create_path() {
let user = PubkyAppUser::default();
let path = user.create_path();
assert_eq!(path, format!("{}profile.json", APP_PATH));
assert_eq!(path, format!("{}{}profile.json", PUBLIC_PATH, APP_PATH));
}

#[test]
Expand Down
2 changes: 2 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,12 @@ pub trait Validatable: Sized + DeserializeOwned {
}

pub trait HasPath {
const PATH_SEGMENT: &'static str;
fn create_path(&self) -> String;
}

pub trait HasPubkyIdPath {
const PATH_SEGMENT: &'static str;
fn create_path(&self, pubky_id: &str) -> String;
}

Expand Down
2 changes: 1 addition & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use utoipa::ToSchema;

/// Represents user data with name, bio, image, links, and status.
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
#[derive(Serialize, Deserialize, Default, Clone, Debug)]
#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub struct PubkyId(String);

Expand Down
Loading

0 comments on commit 353a815

Please sign in to comment.