From 0b47da2af8c02da35e310981919ad097e6897d16 Mon Sep 17 00:00:00 2001 From: Vika Date: Thu, 14 Jul 2022 17:51:49 +0300 Subject: kittybox-indieauth: add From impls for TokenIntrospectionResponse This makes converting Option into a response and vice versa a breeze, and hide the complexity of TokenIntrospectionResponse forced upon this library by the IndieAuth standard. Really, this type should've been represented as Option, I just don't know how to add the "active" field to it properly. --- kittybox-rs/indieauth/src/lib.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'kittybox-rs/indieauth/src') diff --git a/kittybox-rs/indieauth/src/lib.rs b/kittybox-rs/indieauth/src/lib.rs index 242317d..3575132 100644 --- a/kittybox-rs/indieauth/src/lib.rs +++ b/kittybox-rs/indieauth/src/lib.rs @@ -197,6 +197,10 @@ impl TokenData { } } +// I don't like this type, because it could've been represented +// internally by Option. But the IndieAuth standard +// requires the "active" field to be present. I can't do anything +// about it. #[derive(Debug, Serialize, Deserialize)] pub struct TokenIntrospectionResponse { active: bool, @@ -204,6 +208,8 @@ pub struct TokenIntrospectionResponse { #[serde(skip_serializing_if = "Option::is_none")] data: Option } +// These wrappers and impls should take care of making use of this +// type as painless as possible. impl TokenIntrospectionResponse { pub fn inactive() -> Self { Self { active: false, data: None } @@ -228,6 +234,16 @@ impl Default for TokenIntrospectionResponse { Self::inactive() } } +impl From> for TokenIntrospectionResponse { + fn from(data: Option) -> Self { + Self { active: data.is_some(), data } + } +} +impl From for Option { + fn from(response: TokenIntrospectionResponse) -> Option { + response.data + } +} #[derive(Debug, Serialize, Deserialize)] pub struct TokenRevocationRequest { -- cgit 1.4.1