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 85c18b0 commit e6e45b9
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 9 deletions.
28 changes: 28 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ hyper-util = { version = "0.1", features = ["full"] }
scru128 = { version = "3", features = ["serde"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_urlencoded = "0.7.1"
ssri = "9.2.0"
tokio = { version = "1", features = ["full"] }
tokio-stream = "0.1.15"
Expand Down
23 changes: 16 additions & 7 deletions foo.nu
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
#!/usr/bin/env -S nu --stdin
#!/usr/bin/env -S nu

def h. [path: string] {
curl -sN --unix-socket ./store/sock $"'localhost($path)'" | lines | each { from json }
}
alias and-then = if ($in | is-not-empty)
alias ? = if ($in | is-not-empty) { $in }
alias ?? = ? else { return }

let clip = ( h. / | first )
def h. [
path: string
--last-id: string
] {
let query = ( $last_id | and-then { $"?( {last_id: $last_id} | url build-query)" } )
let url = $"localhost($path)($query)"
curl -sN --unix-socket ./store/sock $url | lines | each { from json }
}

$clip.hash
h. / --last-id "foo"

h. $"/cas/($clip.hash)"
# let clip = ( h. / | first )
# $clip.hash
# h. $"/cas/($clip.hash)"
4 changes: 3 additions & 1 deletion src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ async fn get(store: Store, req: Request<hyper::body::Incoming>) -> HTTPResult {
eprintln!("path: {:?}", req.uri().path());
match match_route(req.uri().path()) {
Routes::Root => {
let rx = store.subscribe(ReadOptions { follow: false }).await;
let rx = store
.subscribe(ReadOptions::from_query(req.uri().query()))
.await;
let stream = ReceiverStream::new(rx);
let stream = stream.map(|frame| {
eprintln!("streaming");
Expand Down
41 changes: 40 additions & 1 deletion src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,33 @@ pub struct Store {
commands_tx: mpsc::Sender<Command>,
}

#[derive(Debug)]
use serde::Deserializer;

fn deserialize_follow<'de, D>(deserializer: D) -> Result<bool, D::Error>
where
D: Deserializer<'de>,
{
let s: String = Deserialize::deserialize(deserializer)?;
match s.as_str() {
"false" | "no" | "0" => Ok(false),
_ => Ok(true),
}
}

#[derive(Deserialize, Debug, Default)]
pub struct ReadOptions {
#[serde(default, deserialize_with = "deserialize_follow")]
pub follow: bool,
pub last_id: Option<String>,
}

impl ReadOptions {
pub fn from_query(query: Option<&str>) -> Self {
match query {
Some(q) => serde_urlencoded::from_str(q).unwrap(),
None => Self::default(),
}
}
}

#[derive(Debug)]
Expand Down Expand Up @@ -130,3 +154,18 @@ mod tests {
assert_impl_all!(Store: Send, Sync);
}
}

#[cfg(test)]
mod tests_read_options {
use super::*;

#[test]
fn test_from_query() {
assert_eq!(ReadOptions::from_query(None).follow, false);
assert_eq!(ReadOptions::from_query(Some("foo=bar")).follow, false);
assert_eq!(ReadOptions::from_query(Some("follow")).follow, true);
assert_eq!(ReadOptions::from_query(Some("follow=1")).follow, true);
assert_eq!(ReadOptions::from_query(Some("follow=yes")).follow, true);
assert_eq!(ReadOptions::from_query(Some("follow=true")).follow, true);
}
}

0 comments on commit e6e45b9

Please sign in to comment.