Skip to content

Commit

Permalink
watchman: rust: implement subscribe method
Browse files Browse the repository at this point in the history
Summary:
adds `Client::subscribe` which is similar in usage to
`Client::query`, but returns a subscription handle that will
yield file changes as they occur.

An example of usage is in `examples/subscribe.rs`.

Note that the metadata fields are omitted in this version and commented out; to
implement those we need to have an alternative to `serde_json::Value` that can
handle the binary strings that watchman produces for some metadata; that will
be tackled in the next diff in this stack.

Reviewed By: jsgf

Differential Revision: D18598229

fbshipit-source-id: 6d89a8544d3571d820855c78c261265465fbe1cb
  • Loading branch information
wez authored and facebook-github-bot committed Nov 23, 2019
1 parent 8f03f19 commit 69b6772
Show file tree
Hide file tree
Showing 4 changed files with 432 additions and 29 deletions.
4 changes: 1 addition & 3 deletions rust/watchman_client/examples/glob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}

async fn run() -> Result<(), Box<dyn std::error::Error>> {
let mut client = Connector::new().connect().await?;

let opt = Opt::from_args();

let client = Connector::new().connect().await?;
let resolved = client
.resolve_root(CanonicalPath::canonicalize(opt.path)?)
.await?;
Expand Down
40 changes: 40 additions & 0 deletions rust/watchman_client/examples/subscribe.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//! This example shows how to initiate a subscription and print out
//! file changes as they are reported
use std::path::PathBuf;
use structopt::StructOpt;
use watchman_client::prelude::*;

#[derive(Debug, StructOpt)]
#[structopt(about = "Subscribe to watchman and stream file changes for a path")]
struct Opt {
#[structopt(default_value = ".")]
path: PathBuf,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
if let Err(err) = run().await {
// Print a prettier error than the default
eprintln!("{}", err);
std::process::exit(1);
}
Ok(())
}

async fn run() -> Result<(), Box<dyn std::error::Error>> {
let opt = Opt::from_args();
let client = Connector::new().connect().await?;
let resolved = client
.resolve_root(CanonicalPath::canonicalize(opt.path)?)
.await?;

let (mut sub, initial) = client
.subscribe::<NameOnly>(&resolved, SubscribeRequest::default())
.await?;

println!("{} {:#?}", sub.name(), initial);
loop {
let item = sub.next().await?;
println!("{:#?}", item);
}
}
Loading

0 comments on commit 69b6772

Please sign in to comment.