wip: wrangling warp

This commit is contained in:
meeg_leeto 2022-03-23 19:13:09 +00:00
parent c19d418526
commit b0be6e999a
3 changed files with 1478 additions and 2 deletions

1407
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -6,3 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
base64 = "0.13.0"
envy = "0.4.2"
tokio = { version = "~1.17.0", features = ["full"] }
validators = "0.24.1"
warp = "0.3.2"

View File

@ -1,3 +1,67 @@
fn main() { use std::{collections::BTreeSet, str::FromStr};
println!("Hello, world!");
use validators::prelude::*;
use warp::{Filter, Reply};
#[derive(Debug, Validator)]
#[validator(base64_url(padding(NotAllow)))]
struct Base64WithoutPaddingUrl(String);
impl FromStr for Base64WithoutPaddingUrl {
type Err = <Self as ValidateString>::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::parse_str(s)
}
}
struct SlugParser {
slug_length: usize,
slug_chars: BTreeSet<char>,
}
struct Slug(String);
enum InvalidSlug {
TooLong,
BadChar,
}
impl SlugParser {
fn slug_from_str(s: &str) -> Result<Slug, InvalidSlug> {
todo!()
}
}
async fn shorten<'s>(b64url: &'s str) -> Result<impl Reply, impl Reply> {
let url = base64::decode_config(b64url, base64::URL_SAFE_NO_PAD).map_err(|_| {
warp::reply::with_status(warp::reply(), warp::http::StatusCode::BAD_REQUEST)
})?;
todo!();
Ok(warp::reply())
}
macro_rules! unwrap_and_err {
($x: ident) => {
};
}
#[tokio::main]
async fn main() {
// GET /
let homepage = warp::path::end().and(warp::fs::file("index.html"));
// GET /shorten/:Base64WithoutPaddingUrl
let shorten = warp::path!("shorten" / Base64WithoutPaddingUrl)
.map(|link: Base64WithoutPaddingUrl| shorten(&link.0));
// GET /l/:Slug
let link = warp::path("l")
.and(warp::path::param())
.map(|slug: String| warp::reply());
let routes = warp::get().and(homepage.or(shorten).or(link));
warp::serve(routes).run(([127, 0, 0, 1], 8892)).await;
} }