Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
cablehead committed May 18, 2024
1 parent 520f97e commit 4ed44ef
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
18 changes: 14 additions & 4 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ fn match_route(path: &str) -> Routes {
}

async fn get(store: Store, req: Request<hyper::body::Incoming>) -> HTTPResult {
eprintln!("path: {:?}", req.uri().path());
eprintln!("uri: {:?}", req.uri());
match match_route(req.uri().path()) {
Routes::Root => {
let rx = store
.subscribe(ReadOptions::from_query(req.uri().query()))
.await;
let options = match ReadOptions::from_query(req.uri().query()) {
Ok(opts) => opts,
Err(err) => return response_400(err),
};

let rx = store.subscribe(options).await;
let stream = ReceiverStream::new(rx);
let stream = stream.map(|frame| {
eprintln!("streaming");
Expand Down Expand Up @@ -150,6 +153,13 @@ fn response_404() -> HTTPResult {
.body(empty())?)
}

fn response_400<E: std::error::Error>(err: E) -> HTTPResult {
let body = full(err.to_string());
Ok(Response::builder()
.status(StatusCode::BAD_REQUEST)
.body(body)?)
}

fn full<T: Into<Bytes>>(chunk: T) -> BoxBody<Bytes, BoxError> {
Full::new(chunk.into())
.map_err(|never| match never {})
Expand Down
17 changes: 10 additions & 7 deletions src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,19 @@ where
}
}

#[derive(PartialEq, Deserialize, Debug, Default)]
#[derive(PartialEq, Deserialize, Clone, Debug, Default)]
pub struct ReadOptions {
#[serde(default, deserialize_with = "deserialize_bool")]
pub follow: bool,
#[serde(rename = "last-id")]
pub last_id: Option<Scru128Id>,
}

impl ReadOptions {
pub fn from_query(query: Option<&str>) -> Self {
pub fn from_query(query: Option<&str>) -> Result<Self, serde_urlencoded::de::Error> {
match query {
Some(q) => serde_urlencoded::from_str(q).unwrap(),
None => Self::default(),
Some(q) => serde_urlencoded::from_str(q),
None => Ok(Self::default()),
}
}
}
Expand Down Expand Up @@ -213,14 +214,14 @@ mod tests_read_options {
},
},
TestCase {
input: Some("last_id=03BIDZVKNOTGJPVUEW3K23G45"),
input: Some("last-id=03BIDZVKNOTGJPVUEW3K23G45"),
expected: ReadOptions {
follow: false,
last_id: Some("03BIDZVKNOTGJPVUEW3K23G45".parse().unwrap()),
},
},
TestCase {
input: Some("follow&last_id=03BIDZVKNOTGJPVUEW3K23G45"),
input: Some("follow&last-id=03BIDZVKNOTGJPVUEW3K23G45"),
expected: ReadOptions {
follow: true,
last_id: Some("03BIDZVKNOTGJPVUEW3K23G45".parse().unwrap()),
Expand All @@ -230,7 +231,9 @@ mod tests_read_options {

for case in &test_cases {
let options = ReadOptions::from_query(case.input);
assert_eq!(options, case.expected);
assert_eq!(options, Ok(case.expected.clone()));
}

assert!(ReadOptions::from_query(Some("last-id=123")).is_err());
}
}

0 comments on commit 4ed44ef

Please sign in to comment.