Files
tuono/crates/tuono_lib/src/request.rs
T

55 lines
1.2 KiB
Rust
Raw Normal View History

use serde::Serialize;
use std::collections::HashMap;
use axum::http::{HeaderMap, Uri};
2024-06-23 20:15:33 +02:00
/// Location must match client side interface
#[derive(Serialize, Debug)]
pub struct Location {
href: String,
pathname: String,
2024-07-11 21:43:38 +02:00
#[serde(rename(serialize = "searchStr"))]
2024-06-23 20:15:33 +02:00
search_str: String,
2024-07-11 21:43:38 +02:00
search: HashMap<String, String>,
2024-06-23 20:15:33 +02:00
}
impl Location {
pub fn pathname(&self) -> &String {
&self.pathname
}
}
2024-07-09 19:00:48 +02:00
impl From<Uri> for Location {
fn from(uri: Uri) -> Self {
2024-07-11 21:43:38 +02:00
let query = uri.query().unwrap_or("");
2024-06-23 20:15:33 +02:00
Location {
2024-07-11 21:43:38 +02:00
// TODO: build correct href
2024-06-23 20:15:33 +02:00
href: uri.to_string(),
pathname: uri.path().to_string(),
2024-07-11 21:43:38 +02:00
search_str: query.to_string(),
search: serde_urlencoded::from_str(query).unwrap_or(HashMap::new()),
2024-06-23 20:15:33 +02:00
}
}
}
2024-06-02 20:32:53 +02:00
#[derive(Debug, Clone)]
2024-07-09 19:00:48 +02:00
pub struct Request {
uri: Uri,
pub headers: HeaderMap,
2024-06-02 20:32:53 +02:00
pub params: HashMap<String, String>,
}
2024-07-09 19:00:48 +02:00
impl Request {
pub fn new(uri: Uri, headers: HeaderMap, params: HashMap<String, String>) -> Request {
2024-06-02 20:32:53 +02:00
Request {
uri,
headers,
params,
}
}
2024-06-23 20:15:33 +02:00
pub fn location(&self) -> Location {
2024-07-09 19:00:48 +02:00
Location::from(self.uri.to_owned())
}
}