napi-rs/examples/napi/src/callback.rs
2021-10-27 14:42:57 +08:00

22 lines
567 B
Rust

use napi::bindgen_prelude::*;
use std::env;
#[napi]
fn get_cwd<T: Fn(String) -> Result<()>>(callback: T) {
callback(env::current_dir().unwrap().to_string_lossy().to_string()).unwrap();
}
/// napi = { version = 2, features = ["serde-json"] }
#[napi]
fn read_file<T: Fn(Result<()>, Option<String>) -> Result<()>>(callback: T) {
match read_file_content() {
Ok(s) => callback(Ok(()), Some(s)),
Err(e) => callback(Err(e), None),
}
.unwrap();
}
fn read_file_content() -> Result<String> {
// serde_json::from_str(&s)?;
Ok("hello world".to_string())
}