Browse Source

wip: wrangling warp

dev
meeg_leeto 8 months ago
parent
commit
b0be6e999a
  1. 1407
      Cargo.lock
  2. 5
      Cargo.toml
  3. 68
      src/main.rs

1407
Cargo.lock generated

File diff suppressed because it is too large Load Diff

5
Cargo.toml

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

68
src/main.rs

@ -1,3 +1,67 @@ @@ -1,3 +1,67 @@
fn main() {
println!("Hello, world!");
use std::{collections::BTreeSet, str::FromStr};
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;
}

Loading…
Cancel
Save