about summary refs log tree commit diff
path: root/kittybox-rs/indieauth
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2022-07-19 05:27:39 +0300
committerVika <vika@fireburn.ru>2022-07-19 05:32:18 +0300
commit12c96ceaeaabe0aa7d0d2c70497886d9a5610f10 (patch)
tree397b108e44ca16eb402c3725e46a73a8dc1053a3 /kittybox-rs/indieauth
parent1efb89a75af31d7580fdb06e3b4535bcde08e966 (diff)
downloadkittybox-12c96ceaeaabe0aa7d0d2c70497886d9a5610f10.tar.zst
kittybox-indieauth: convert Error into axum::response::Response
This requires the `axum` feature to be enabled, to prevent unwanted
dependencies (e.g. in client apps or when using a different framework,
since the library doesn't concern itself with I/O)
Diffstat (limited to 'kittybox-rs/indieauth')
-rw-r--r--kittybox-rs/indieauth/Cargo.toml13
-rw-r--r--kittybox-rs/indieauth/src/lib.rs14
2 files changed, 27 insertions, 0 deletions
diff --git a/kittybox-rs/indieauth/Cargo.toml b/kittybox-rs/indieauth/Cargo.toml
index 80511f9..a81fd51 100644
--- a/kittybox-rs/indieauth/Cargo.toml
+++ b/kittybox-rs/indieauth/Cargo.toml
@@ -3,6 +3,10 @@ name = "kittybox-indieauth"
 version = "0.1.0"
 edition = "2021"
 
+[features]
+default = []
+axum = ["axum-core", "serde_json", "http"]
+
 [dev-dependencies]
 serde_json = "^1.0.64"       # A JSON serialization file format
 serde_urlencoded = "^0.7.0"  # `x-www-form-urlencoded` meets Serde
@@ -16,3 +20,12 @@ features = ["serde"]
 [dependencies.serde]         # A generic serialization/deserialization framework
 version = "^1.0.125"
 features = ["derive"]
+[dependencies.axum-core]
+version = "^0.2.6"
+optional = true
+[dependencies.serde_json]
+version = "^1.0.64"
+optional = true
+[dependencies.http]
+version = "^0.2.7"
+optional = true
\ No newline at end of file
diff --git a/kittybox-rs/indieauth/src/lib.rs b/kittybox-rs/indieauth/src/lib.rs
index b461fea..cb99146 100644
--- a/kittybox-rs/indieauth/src/lib.rs
+++ b/kittybox-rs/indieauth/src/lib.rs
@@ -144,6 +144,7 @@ pub enum GrantResponse {
         #[serde(skip_serializing_if = "Option::is_none")]
         profile: Option<Profile>,
         access_token: String,
+        // TODO replace with std::time::Duration
         #[serde(skip_serializing_if = "Option::is_none")]
         expires_in: Option<u64>,
         #[serde(skip_serializing_if = "Option::is_none")]
@@ -176,6 +177,7 @@ pub struct TokenData {
     pub me: Url,
     pub client_id: Url,
     pub scope: Scopes,
+    // TODO replace these two with std::time::SystemTime
     #[serde(skip_serializing_if = "Option::is_none")]
     pub exp: Option<u64>,
     #[serde(skip_serializing_if = "Option::is_none")]
@@ -359,6 +361,18 @@ impl std::fmt::Display for self::Error {
     }
 }
 
+#[cfg(feature = "axum")]
+impl axum_core::response::IntoResponse for self::Error {
+    fn into_response(self) -> axum_core::response::Response {
+        use http::StatusCode;
+
+        (StatusCode::BAD_REQUEST,
+         [("Content-Type", "application/json")],
+         serde_json::to_vec(&self).unwrap())
+            .into_response()
+    }
+}
+
 #[cfg(test)]
 mod tests {
     use super::*;