Skip to content

Commit

Permalink
Migrate CLI to async implementation
Browse files Browse the repository at this point in the history
Use same asynchronous implementation for CLI & website
  • Loading branch information
noboruma committed Feb 26, 2021
1 parent 7706219 commit 3eb82c5
Show file tree
Hide file tree
Showing 10 changed files with 457 additions and 418 deletions.
635 changes: 305 additions & 330 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ exclude = [
crate-type = ["cdylib", "rlib"]

[dependencies]
reqwest = { version = "0.10", features = ["blocking","json"] }
reqwest = { version = "0.11", features = ["json"] }
dotenv = "0.15.0"
dirs = "2.0.2"
url = "2.1.1"
Expand All @@ -28,6 +28,7 @@ mocktopus = "0.7.5"
base64 = "0.12.3"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
assert_cmd = "1.0.1"
tokio = { version = "1", features = ["full"] }
[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4.14"
Expand Down
4 changes: 3 additions & 1 deletion src/bin/rclip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ fn fail_error(res: &Result<(), rclip::ClipboardError>) {
match res {
Err(rclip::ClipboardError::BackendError) => fail("Failed to use clipboard with the backend (Is it opened/linked?)"),
Err(rclip::ClipboardError::NetworkError(url)) => fail(("Failed to contact the backend: ".to_string()+&url).as_str()),
Err(rclip::ClipboardError::AwaitError) => fail("Async failure"),
Ok(_) => {},
}
}
Expand All @@ -74,7 +75,8 @@ fn construct_clipboard(stdout: &mut io::Stdout) -> rclip::Clipboard {
return rclip::Clipboard::from(config);
}

fn main() {
#[tokio::main]
async fn main() {
let mut stdout = io::stdout();
let stdin = io::stdin();

Expand Down
39 changes: 3 additions & 36 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,15 @@ use url::Url;

use mocktopus::macros::*;

type HttpCompletionFn = dyn FnOnce(Result<HashMap<String, String>, ClipboardError>);

pub fn prepare_endpoint(config_context: &config::ConfigContext, path: &str) -> Url {
let mut url = config_context.base_url.clone();
url.set_path(path);
append_query(&mut url, &mut config_context.token.as_bytes(), &TOKEN_PARAM);
return url;
}

#[cfg(not(target_arch = "wasm32"))]
#[mockable]
pub fn get_http(url: &Url) -> Result<(), ClipboardError> {
match reqwest::blocking::get(url.as_str()) {
Ok(_) => Ok(()),
Err(_) => return Err(ClipboardError::NetworkError(url.to_string())),
}
}

#[cfg(not(target_arch = "wasm32"))]
#[mockable]
pub fn get_http_response(url: &Url) -> Result<HashMap<String, String>, ClipboardError>{
let resp = reqwest::blocking::get(url.as_str());
let resp = match resp {
Ok(resp) => resp,
Err(_) => return Err(ClipboardError::NetworkError(url.to_string())),
};
return match resp .json::<HashMap<String, String>>() {
Ok(resp) => Ok(resp),
Err(_) => return Err(ClipboardError::BackendError),
}
}

#[cfg(not(target_arch = "wasm32"))]
pub fn execute<F: futures::Future>(completion : F) {
futures::executor::block_on(completion);
Expand All @@ -47,7 +26,7 @@ pub fn execute<F: 'static + futures::Future<Output = ()>>(completion : F) {
}

#[mockable]
pub fn get_http_response_comp(url: &Url, completion: Box<dyn Fn(Result<HashMap<String, String>, ClipboardError>)>) {
pub fn get_http_response_comp(url: &Url, completion: Box<HttpCompletionFn>) {

let boxed = Box::from(url.clone());
execute(async move {
Expand All @@ -63,18 +42,6 @@ pub fn get_http_response_comp(url: &Url, completion: Box<dyn Fn(Result<HashMap<S
});
}

#[cfg(target_arch = "wasm32")]
#[mockable]
pub fn get_http(url: &Url) -> Result<(), ClipboardError> {
use wasm_bindgen_futures::spawn_local;

let boxed = Box::from(url.clone());
spawn_local(async move {
let _db = (reqwest::get(boxed.as_str())).await;
});
Ok(())
}

pub fn append_query(url: &mut Url, input: &mut dyn Read, param: &str) {
let mut query = String::from("");
input.read_to_string(&mut query).unwrap();
Expand Down
17 changes: 15 additions & 2 deletions src/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,24 @@ pub fn open(completion: js_sys::Function) -> () {
}

#[wasm_bindgen]
pub fn copy(token:String, input: String) {
pub fn copy(token:String, input: String, completion: js_sys::Function) {
console_error_panic_hook::set_once();
let clipboard = default_clipboard(token);
let mut ss = stream::StringStream::from(input);
let _ = clipboard.copy(&mut ss);

let completion = move |resp : Result<(), ClipboardError>| {
let this = JsValue::null();
match resp {
Ok(_) => {
completion.call0(&this).unwrap();
return ();
},
_ => (),
};
completion.call1(&this, &JsValue::from_str("error")).unwrap();
};

let _ = clipboard.copy_comp(&mut ss, Box::new(completion));
}

#[wasm_bindgen]
Expand Down
Loading

0 comments on commit 3eb82c5

Please sign in to comment.