Skip to content

Commit

Permalink
feat: implement persistence API
Browse files Browse the repository at this point in the history
  • Loading branch information
bysensa committed Jan 25, 2023
1 parent a84c2de commit d3b9280
Show file tree
Hide file tree
Showing 21 changed files with 483 additions and 181 deletions.
Empty file.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use cacao::appkit::AppDelegate;
use cacao::notification_center::Dispatcher;
use cocoa::base::{id, nil};
use cocoa::foundation::NSString;
use flume::*;
Expand All @@ -10,6 +12,8 @@ use objc_id::Id;
use std::ptr;
use std::sync::Once;

pub trait Observer: AppDelegate + Dispatcher<Message = MacObservedEvent> + Sized {}

extern "C" {
static NSWorkspaceDidActivateApplicationNotification: id;
static NSWorkspaceApplicationKey: id;
Expand Down Expand Up @@ -165,7 +169,9 @@ impl MacEventObserverImpl {
chrono::Utc::now(),
);
let sender = EVENTS_CHANNEL.0.clone();
let _ = sender.send(event);
// let _ = sender.send(event);

cacao::appkit::App::<dyn Observer<Message = MacObservedEvent>, MacObservedEvent>::dispatch_main(event)
}
}

Expand Down
20 changes: 0 additions & 20 deletions rust/yad_mac_observer/Cargo.toml

This file was deleted.

20 changes: 0 additions & 20 deletions rust/yad_mac_observer/examples/usage.rs

This file was deleted.

19 changes: 0 additions & 19 deletions rust/yad_mac_observer/src/lib.rs

This file was deleted.

14 changes: 14 additions & 0 deletions rust/yad_net/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "yad-net"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-trait = {workspace = true}
async-std = {workspace = true}


jsonrpsee = { workspace = true, features=["server"] }
salvo = { workspace = true}
1 change: 1 addition & 0 deletions rust/yad_net/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod rpc;
27 changes: 27 additions & 0 deletions rust/yad_net/src/rpc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use jsonrpsee::RpcModule;
use salvo::http::ParseError;
use salvo::prelude::*;
use std::str::from_utf8;
use std::sync::Arc;

pub trait RpcReply {
type Output: Send + Sync + 'static;
}

pub async fn handler<T>(module: Option<&Arc<RpcModule<T>>>, req: &mut Request, res: &mut Response) {
let payload = req
.payload()
.await
.map(|msg| msg.as_slice())
.and_then(|msg| from_utf8(msg).map_err(|_| ParseError::ParseFromStr));
if let (Some(module), Ok(msg)) = (module, payload) {
if let Ok((result, mut _stream)) = module.raw_json_request(msg).await {
let result = result.result;
res.set_status_code(StatusCode::OK);
res.render(result);
return;
}
return res.set_status_code(StatusCode::INTERNAL_SERVER_ERROR);
}
return res.set_status_code(StatusCode::INTERNAL_SERVER_ERROR);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ edition = "2021"
[dependencies]
async-trait = "0.1.61"
async-std = { version = "1", features=["tokio1", "attributes"] }
jsonrpsee = { version = "0.16", features=["server", "macros"] }
salvo = { version = "0.37.9", features=["affix"] }
jsonrpsee = { workspace = true, features=["server", "macros"] }
salvo = { workspace = true, features=["affix"] }
anyhow = "1"
entrait = "0.5"
unimock = "0.4"
rlink = "0.6"
serde = "1"
thiserror = "1.0.38"
yad-net = {workspace = true}

[dev-dependencies]
reqwest = "0.11.13"
21 changes: 21 additions & 0 deletions rust/yad_scope_tracking/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use salvo::prelude::*;
use std::sync::Arc;

mod processor;
mod rpc;

pub struct Scope {}

impl Scope {
pub fn new() -> Self {
Scope {}
}

pub fn router() -> Router {
let rpc_context = rpc::YadTrackingRpcContext;
let rpc_module = Arc::new(rpc_context.module());
Router::with_path("tracking")
.hoop(affix::inject(rpc_module))
.post(rpc_handler)
}
}
File renamed without changes.
77 changes: 77 additions & 0 deletions rust/yad_scope_tracking/src/rpc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use async_trait::async_trait;
use jsonrpsee::core::*;
use jsonrpsee::proc_macros::rpc;
use jsonrpsee::RpcModule;
use salvo::prelude::*;
use std::sync::Arc;
use yad_net::rpc::RpcReply;

#[cfg(test)]
mod tests {
use crate::net::router;
use salvo::prelude::*;

#[async_std::test]
async fn handle_rpc_test() {
let addr = "127.0.0.1:7878";
let route = router();
async_std::task::spawn(Server::new(TcpListener::bind(addr)).serve(route));

let client = reqwest::Client::new();
let res = client
.post("http://127.0.0.1:7878/tracking")
.body(r#"{"jsonrpc": "2.0", "method": "tracking_bar", "params": [42, 23], "id": 1}"#)
.send()
.await
.unwrap()
.text()
.await;
dbg!(&res);
}
}

#[handler]
async fn rpc_handler(req: &mut Request, res: &mut Response, depot: &mut Depot) {
let module = depot.obtain::<Arc<YadTrackingRpcModule>>();
yad_net::rpc::handler(module, req, res).await;
}

pub type YadTrackingRpcModule = RpcModule<YadTrackingRpcContext>;

impl RpcReply for YadTrackingReply {
type Output = Self;
}

#[derive(Serialize)]
pub enum YadTrackingReply {
Processed,
}

#[rpc(server, namespace = "tracking", server_bounds(T::Output: jsonrpsee::core::Serialize))]
pub trait YadTrackingRpc<T: RpcReply> {
#[method(name = "bar")]
async fn bar(&self) -> RpcResult<T::Output>;

#[method(name = "foo")]
fn foo(&self) -> RpcResult<()>;
}

#[derive(Debug)]
pub struct YadTrackingRpcContext;

impl YadTrackingRpcContext {
pub fn module(self) -> YadTrackingRpcModule {
self.into_rpc()
}
}

#[async_trait]
impl YadTrackingRpcServer<YadTrackingReply> for YadTrackingRpcContext {
async fn bar(&self) -> Result<<YadTrackingReply as RpcReply>::Output, Error> {
Ok(YadTrackingReply::Processed)
}

fn foo(&self) -> RpcResult<()> {
Ok(())
}
}
8 changes: 8 additions & 0 deletions rust/yad_storage/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "yad"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Loading

0 comments on commit d3b9280

Please sign in to comment.