From a436ec3dee22fcf58d9ce1e7b6bbba7766aabd79 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sat, 26 Mar 2022 09:43:42 +0000 Subject: feat(response): inputs --- examples/windmark.rs | 14 ++++++++++++++ src/lib.rs | 4 ++-- src/response.rs | 12 ++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/examples/windmark.rs b/examples/windmark.rs index 391b13e..f45c9a7 100644 --- a/examples/windmark.rs +++ b/examples/windmark.rs @@ -81,5 +81,19 @@ fn main() -> std::io::Result<()> { .mount("/param/:lang", |_, _url, dynamic_parameter| { Response::Success(format!("Parameter lang is {:?}", dynamic_parameter)) }) + .mount("/input", |_, url, _| { + if let Some(name) = url.query() { + Response::Success(format!("Your name is {}!", name)) + } else { + Response::Input("What is your name?".into()) + } + }) + .mount("/sensitive-input", |_, url, _| { + if let Some(password) = url.query() { + Response::Success(format!("Your password is {}!", password)) + } else { + Response::SensitiveInput("What is your password?".into()) + } + }) .run() } diff --git a/src/lib.rs b/src/lib.rs index f532679..1b17318 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -367,11 +367,11 @@ impl Router { "{}{}\r\n{}", response_status, match response_status { - 40 | 50 | 51 => &*content, + 10 | 11 | 40 | 50 | 51 => &*content, _ => " text/gemini; charset=utf-8", }, match response_status { - 40 | 50 | 51 => "".to_string(), + 10 | 11 | 40 | 50 | 51 => "".to_string(), _ => format!("{}{}{}", header, content, footer), } ) diff --git a/src/response.rs b/src/response.rs index 961cf79..cad2f1a 100644 --- a/src/response.rs +++ b/src/response.rs @@ -27,6 +27,8 @@ impl ToString for Header { } pub enum Response { + Input(String), + SensitiveInput(String), Success(String), NotFound(String), TemporaryFailure(String), @@ -38,6 +40,16 @@ pub(crate) fn to_value_set_status( status: &mut i32, ) -> String { match response { + Response::Input(value) => { + *status = 10; + + value + } + Response::SensitiveInput(value) => { + *status = 11; + + value + } Response::Success(value) => { *status = 20; -- cgit v1.2.3