Skip to content

Commit

Permalink
Change links to forms
Browse files Browse the repository at this point in the history
lu-zero committed Apr 28, 2022

Verified

This commit was signed with the committer’s verified signature.
Exirel Florian Strzelecki
1 parent aec6655 commit 60c5599
Showing 3 changed files with 33 additions and 42 deletions.
13 changes: 6 additions & 7 deletions src/property.rs
Original file line number Diff line number Diff line change
@@ -46,20 +46,19 @@ pub trait Property: Send + Sync {
/// Returns a JSON value describing the property.
fn as_property_description(&self) -> serde_json::Map<String, serde_json::Value> {
let mut description = self.get_metadata();
let link = json!(
let form = json!(
{
"rel": "property",
"href": self.get_href(),
}
);

if let Some(links) = description
.get_mut("links")
.map(|links| links.as_array_mut().unwrap())
if let Some(forms) = description
.get_mut("forms")
.map(|forms| forms.as_array_mut().unwrap())
{
links.push(link);
forms.push(form);
} else {
description.insert("links".to_string(), json!([link]));
description.insert("forms".to_string(), json!([form]));
}
description
}
22 changes: 10 additions & 12 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -340,21 +340,20 @@ async fn handle_get_things(req: HttpRequest, state: web::Data<AppState>) -> Http
for thing in things.iter() {
let thing = thing.read().unwrap();

let mut link = serde_json::Map::new();
link.insert("rel".to_owned(), json!("alternate"));
link.insert(
let mut form = serde_json::Map::new();
form.insert(
"href".to_owned(),
json!(format!("{}{}", ws_href, thing.get_href())),
);

let mut description = thing.as_thing_description().clone();
{
let links = description
.get_mut("links")
let forms = description
.get_mut("forms")
.unwrap()
.as_array_mut()
.unwrap();
links.push(json!(link));
forms.push(json!(form));
}

description.insert("href".to_owned(), json!(thing.get_href()));
@@ -393,18 +392,17 @@ async fn handle_get_thing(req: HttpRequest, state: web::Data<AppState>) -> HttpR
thing.get_href()
);

let mut link = serde_json::Map::new();
link.insert("rel".to_owned(), json!("alternate"));
link.insert("href".to_owned(), json!(ws_href));
let mut form = serde_json::Map::new();
form.insert("href".to_owned(), json!(ws_href));

let mut description = thing.as_thing_description();
{
let links = description
.get_mut("links")
let forms = description
.get_mut("forms")
.unwrap()
.as_array_mut()
.unwrap();
links.push(json!(link));
forms.push(json!(form));
}

description.insert(
40 changes: 17 additions & 23 deletions src/thing.rs
Original file line number Diff line number Diff line change
@@ -324,50 +324,45 @@ impl Thing for BaseThing {
json!(self.get_property_descriptions()),
);

let mut links: Vec<serde_json::Map<String, serde_json::Value>> = Vec::new();
let mut forms: Vec<serde_json::Map<String, serde_json::Value>> = Vec::new();

let mut properties_link = serde_json::Map::new();
properties_link.insert("rel".to_owned(), json!("properties"));
properties_link.insert(
let mut properties_form = serde_json::Map::new();
properties_form.insert(
"href".to_owned(),
json!(format!("{}/properties", self.get_href_prefix())),
);
links.push(properties_link);
forms.push(properties_form);

let mut actions_link = serde_json::Map::new();
actions_link.insert("rel".to_owned(), json!("actions"));
actions_link.insert(
let mut actions_form = serde_json::Map::new();
actions_form.insert(
"href".to_owned(),
json!(format!("{}/actions", self.get_href_prefix())),
);
links.push(actions_link);
forms.push(actions_form);

let mut events_link = serde_json::Map::new();
events_link.insert("rel".to_owned(), json!("events"));
events_link.insert(
let mut events_form = serde_json::Map::new();
events_form.insert(
"href".to_owned(),
json!(format!("{}/events", self.get_href_prefix())),
);
links.push(events_link);
forms.push(events_form);

if let Some(ui_href) = self.get_ui_href() {
let mut ui_link = serde_json::Map::new();
ui_link.insert("rel".to_owned(), json!("alternate"));
ui_link.insert("mediaType".to_owned(), json!("text/html"));
ui_link.insert("href".to_owned(), json!(ui_href));
links.push(ui_link);
let mut ui_form = serde_json::Map::new();
ui_form.insert("mediaType".to_owned(), json!("text/html"));
ui_form.insert("href".to_owned(), json!(ui_href));
forms.push(ui_form);
}

description.insert("links".to_owned(), json!(links));
description.insert("forms".to_owned(), json!(forms));

let mut actions = serde_json::Map::new();
for (name, action) in self.available_actions.iter() {
let mut metadata = action.get_metadata().clone();
metadata.insert(
"links".to_string(),
"forms".to_string(),
json!([
{
"rel": "action",
"href": format!("{}/actions/{}", self.get_href_prefix(), name),
},
]),
@@ -381,10 +376,9 @@ impl Thing for BaseThing {
for (name, event) in self.available_events.iter() {
let mut metadata = event.get_metadata().clone();
metadata.insert(
"links".to_string(),
"forms".to_string(),
json!([
{
"rel": "event",
"href": format!("{}/events/{}", self.get_href_prefix(), name),
},
]),

0 comments on commit 60c5599

Please sign in to comment.