summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOndřej Hruška <ondra@ondrovo.com>2019-09-29 12:12:58 +0200
committerPaul Woolcock <paul@woolcock.us>2020-04-07 12:36:28 -0400
commit98932ac5d662f09fd3719994933b3dbc09e3d7e3 (patch)
treef5e8f18f0acfda52895dde3e750df657af8df7d5
parentacb87aba9c41f65b8147a71ac1c69f89814fceeb (diff)
Make Scopes deserializable
-rw-r--r--src/scopes.rs30
1 files changed, 29 insertions, 1 deletions
diff --git a/src/scopes.rs b/src/scopes.rs
index 884119c..d780ce1 100644
--- a/src/scopes.rs
+++ b/src/scopes.rs
@@ -9,6 +9,8 @@ use std::{
use serde::ser::{Serialize, Serializer};
use errors::Error;
+use serde::{Deserialize, Deserializer};
+use serde::de::{self, Visitor};
/// Represents a set of OAuth scopes
///
@@ -52,6 +54,29 @@ impl Serialize for Scopes {
}
}
+struct DeserializeScopesVisitor;
+
+impl<'de> Visitor<'de> for DeserializeScopesVisitor {
+ type Value = Scopes;
+
+ fn expecting(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
+ write!(formatter, "space separated scopes")
+ }
+
+ fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
+ where E: de::Error
+ {
+ Scopes::from_str(v).map_err(de::Error::custom)
+ }
+}
+
+impl<'de> Deserialize<'de> for Scopes {
+ fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'de>>::Error> where
+ D: Deserializer<'de> {
+ deserializer.deserialize_str(DeserializeScopesVisitor)
+ }
+}
+
impl Scopes {
/// Represents all available oauth scopes: "read write follow push"
///
@@ -725,7 +750,7 @@ mod tests {
}
#[test]
- fn test_scopes_serialize() {
+ fn test_scopes_serialize_deserialize() {
let tests = [
(
Scopes::read_all() | Scopes::write(Write::Notifications) | Scopes::follow(),
@@ -738,6 +763,9 @@ mod tests {
let ser = serde_json::to_string(&a).expect("Couldn't serialize Scopes");
let expected = format!("\"{}\"", b);
assert_eq!(&ser, &expected);
+
+ let des : Scopes = serde_json::from_str(&ser).expect("Couldn't deserialize Scopes");
+ assert_eq!(&des, a);
}
}