Skip to content

Commit

Permalink
Initial filtering code
Browse files Browse the repository at this point in the history
  • Loading branch information
glyn committed Jun 5, 2024
1 parent 6c54149 commit ca838cf
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 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 @@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
axum = "0.7.4"
clap = { version = "4.5.4", features = ["derive"] }
http = "1.1.0"
serde = { version = "1.0.203", features = ["derive"] }
serde_json = "1.0.117"
tokio = { version = "1.35.1", features = ["macros", "rt-multi-thread"] }
26 changes: 25 additions & 1 deletion src/jrdmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ licensed under the GPL.

use std::collections::hash_map::HashMap;
use std::option::Option;
use std::str::FromStr;

use http::uri::Uri;
use serde;
use serde_json;

Expand All @@ -28,21 +30,39 @@ pub struct Jrd {

// The "aliases" array is an array of zero or more URI strings that
// identify the same entity as the "subject" URI.
#[serde(skip_serializing_if = "Option::is_none")]
pub aliases: Option<Vec<String>>,


// The "properties" object comprises zero or more name/value pairs whose
// names are URIs (referred to as "property identifiers") and whose
// values are strings or null. Properties are used to convey additional
// information about the subject of the JRD.
#[serde(skip_serializing_if = "Option::is_none")]
pub properties: Option<HashMap<String, String>>,


// The "links" array has any number of member objects, each of which
// represents a link [4].
#[serde(skip_serializing_if = "Option::is_none")]
pub links: Option<Vec<ResourceLink>>,
}

impl Jrd {
pub fn filter(&self, rel: String) -> Jrd {
Jrd {
subject: self.subject.clone(),
aliases: self.aliases.clone(),
properties: self.properties.clone(),
links: self.links.clone().
map(|lks| lks.into_iter().
filter(|lk| Uri::from_str(&lk.rel).unwrap() == Uri::from_str(&rel).unwrap()).
collect()
)
}
}
}

#[derive(Clone, serde::Deserialize, serde::Serialize)]
pub struct ResourceLink {
// Each of these link objects can have the following members:
Expand All @@ -63,11 +83,13 @@ pub struct ResourceLink {

// The value of the "type" member is a string that indicates the media
// type of the target resource (see RFC 6838).
#[serde(skip_serializing_if = "Option::is_none")]
pub type_: Option<String>,


// The value of the "href" member is a string that contains a URI
// pointing to the target resource.
#[serde(skip_serializing_if = "Option::is_none")]
pub href: Option<String>,


Expand All @@ -78,6 +100,7 @@ pub struct ResourceLink {
// utilize the link relation, and, if used, a language identifier SHOULD
// be duly used as the name. If the language is unknown or unspecified,
// then the name is "und".
#[serde(skip_serializing_if = "Option::is_none")]
pub titles: Option<HashMap<String, String>>,


Expand All @@ -86,11 +109,12 @@ pub struct ResourceLink {
// "property identifiers") and whose values are strings or null.
// Properties are used to convey additional information about the link
// relation.
#[serde(skip_serializing_if = "Option::is_none")]
pub properties: Option<HashMap<String, String>>,
}


pub fn to_json(resource : &JrdMap) -> String {
pub fn to_json(resource : &Jrd) -> String {
serde_json::to_string(&resource).unwrap()
}

Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,11 @@ async fn handler(
) -> String {
// use `state`...

jrdmap::to_json(&state.webfinger_jrdmap)
let uri = "acct:[email protected]".to_string();

let jrd = state.webfinger_jrdmap.get(&uri).expect("No JRD found for input URI");

let rel = "http://webfinger.net/rel/avatar".to_string();

jrdmap::to_json(&jrd.filter(rel))
}

0 comments on commit ca838cf

Please sign in to comment.