mild-times / webmanifest

Create a webmanifest file
Apache License 2.0
19 stars 2 forks source link

impl std::str::FromStr #3

Open yoshuawuyts opened 6 years ago

yoshuawuyts commented 6 years ago

Feature Request

Summary

Implement the std::str::fromStr trait for Manifest.

Motivation

It'd be great if the following could work:

let path = "./assets/manifest.webmanifest";
let manifest: Manifest = fs::read_to_string(path)?.parse()?;

This not only looks nice, but it removes the need for consumers to import serde and serde_json as peer dependencies to perform parsing.

Guide-level explanation

By implementing str::FromStr conversions can be done using .parse()?. We should add an example for this too to the README under ## Examples using a heading like: ### Read Manifest from Disk.

Reference-level explanation

The from_str method should wrap around serde_json::from_str()? .

Drawbacks

None.

Rationale and alternatives

Alternatively we could implement TryInto to replace .parse()? with .try_into(). But that's currently not available on stable yet, so we can add it once it is.

Unresolved Questions

None.

yoshuawuyts commented 6 years ago

Alternative usage:

let path = "./assets/manifest.webmanifest";
let manifest = fs::read_to_string(path)?.parse::<Manifest>()?;

But potentially slightly more confusing.

yoshuawuyts commented 5 years ago

References for where I left this off: