Skip to content
This repository has been archived by the owner on Jul 19, 2020. It is now read-only.

Add explanation of the format module. #56

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

teymour-aldridge
Copy link
Contributor

No description provided.

Comment on lines 10 to 13
// convert the data into JSON
let json_data = Json(&data);
// convert the JSON back
let Json(dump) = json_data;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this example isn't actually doing any conversion. Could you change it to match what you have below with Into/From Text or Binary

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@teymour-aldridge I don't think you addressed this comment

`Contribute to our docs:` [Explain the format module in depth](https://github.com/yewstack/docs/issues/24)
{% endhint %}
## Introduction
Yew provides the format module to make it easy to convert from Rust types to common data formats (and vice versa). Rust provides support for a number of different data formats. Some of these formats are binary-only (e.g. CBOR) while others support both text and binary (e.g. JSON).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also like if there was a usage section. The format module is not a service itself, but is necessary for using the FetchService and WebSocketService

```

## Further reading
* [The API documentation](https://docs.rs/yew/0.14.3/yew/format/index.html)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: drop the version from the url so it always goes to latest

use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
pub struct APIRequest {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think this naming is confusing. Maybe you could name it like RequestData?

// convert the custom data type into JSON text – note that `.into()` had to be called before the data was converted
let raw_api_request: Text = Json(&new_request).into();
// convert the JSON back into our custom data type.
let Json(rebuilt_request): Json<Result<APIRequest, _>> = Json::from(Ok(raw_api_request.unwrap()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you unwrap and then re-wrap with Ok?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants