Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
oscartbeaumont committed Dec 27, 2023
1 parent 50ba4c3 commit e1cde93
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ unstable = ["rspc-core/unstable"] # APIs where one line of code can blow up your

[dependencies]
rspc-core = { path = "./crates/core" }
rspc-core2 = { path = "./crates/core2" }
specta = { workspace = true }

serde = { workspace = true }
Expand Down
2 changes: 2 additions & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ tracing = ["dep:tracing"]
unstable = []

[dependencies]
rspc-core2 = { path = "../core2" }

specta = { workspace = true, features = ["typescript"] } # TODO: `typescript` should be required
serde = { workspace = true }
thiserror = { workspace = true }
Expand Down
8 changes: 8 additions & 0 deletions crates/core/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,11 @@ mod unstable {
}
}
}

impl<TCtx> rspc_core2::Router for Router<TCtx> {
type Ctx = TCtx;

fn build(self) -> rspc_core2::Executor {
todo!()
}
}
12 changes: 12 additions & 0 deletions crates/core2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "rspc-core2"
version = "0.0.0"
edition = "2021"
publish = false

[dependencies]
futures-core = "0.3"
erased-serde = "0.4.1"

[dev-dependencies]
tokio = { version = "1", features = ["full"] }
13 changes: 13 additions & 0 deletions crates/core2/TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FORMAT AGNOSTIC

Async context functions

Allow streaming large files to the `result`

Tracing integration
Benchmark proper trie router

Remove `erased-serde` from the public API


What if forget to call `ctx.result.serialize_*`???
31 changes: 31 additions & 0 deletions crates/core2/src/executor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::{borrow::Cow, collections::HashMap, future::Future, pin::Pin};

use erased_serde::Deserializer;

use crate::serializer::Serializer;

pub struct RequestContext<'a> {
// pub id: u32,
pub arg: Option<&'a mut (dyn Deserializer<'a> + Send)>, // TODO: Remove `erased-serde` from public API
pub result: Serializer<'a>,
}

#[derive(Default)]
pub struct Executor {
procedures: HashMap<Cow<'static, str>, Procedure>,
}

impl Executor {
// pub async fn execute() -> () {}

// pub async fn execute_blocking() -> () {}

// pub async fn execute_streaming() -> () {}

// TODO: How can the user get a `Value` without major overhead
}

pub struct Procedure {
// TODO: Make this private
pub handler: Box<dyn Fn(RequestContext) -> Pin<Box<dyn Future<Output = ()> + Send + '_>>>,
}
Empty file added crates/core2/src/internal.rs
Empty file.
23 changes: 23 additions & 0 deletions crates/core2/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//! rspc core

// TODO: Clippy lints

mod executor;
#[doc(hidden)]
pub mod internal;
mod router;
mod serializer;
mod task;

// pub trait Body = Stream<Item = Vec<u8>>; // TODO: Should this allow returning `Value`

pub use executor::{Executor, Procedure};
pub use router::Router;
pub use task::Task;

// // TODO: How can `ctx_fn` get stuff from request context or Tauri handle???
// pub fn todo_router<R: Router>(router: R, ctx_fn: impl Fn() -> R::Ctx) {
// let executor = router.build();

// // executor.execute();
// }
8 changes: 8 additions & 0 deletions crates/core2/src/router.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::Executor;

// TODO: Seal this
pub trait Router {
type Ctx;

fn build(self) -> Executor;
}
11 changes: 11 additions & 0 deletions crates/core2/src/serializer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub struct Serializer<'a> {
serializer: &'a mut (dyn erased_serde::Serializer + Send),
}

impl<'a> Serializer<'a> {}

// TODO: How could this serialize bytes/files

// pub struct BytesSerializer {}

// pub struct ValueSerializer {}
35 changes: 35 additions & 0 deletions crates/core2/src/task.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::{
fmt,
future::Future,
pin::Pin,
task::{Context, Poll},
};

use futures_core::{FusedStream, Stream};

pub struct Task {
pub id: u32,
// pub should_be_queued: bool,
// done: bool,
stream: Pin<Box<dyn Future<Output = ()> + Send>>,
}

impl fmt::Debug for Task {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Task").field("id", &self.id).finish()
}
}

impl Stream for Task {
type Item = Vec<u8>; // TODO: What if the user wants `serde_json::Value`

fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
todo!()
}
}

impl FusedStream for Task {
fn is_terminated(&self) -> bool {
todo!()
}
}
41 changes: 41 additions & 0 deletions crates/core2/tests/basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use rspc_core2::Procedure;

#[tokio::test]
async fn test_procedure() {
let _p = Procedure {
handler: Box::new(|ctx| {
Box::pin(async move {
let _input: String = erased_serde::deserialize(ctx.arg.unwrap()).unwrap();
// ctx.result.erased_serialize_str("todo");
// TODO
})
}),
};

let _p = Procedure {
handler: Box::new(|ctx| {
Box::pin(async move {
let _input: String = erased_serde::deserialize(ctx.arg.unwrap()).unwrap();
// ctx.result.erased_serialize_str("todo");
// TODO: Stream multiple JSON results
})
}),
};

let _p = Procedure {
handler: Box::new(|ctx| {
Box::pin(async move {
let _input: String = erased_serde::deserialize(ctx.arg.unwrap()).unwrap();
// ctx.result.erased_serialize_str("todo");
// TODO: Stream entire file worth of bytes
})
}),
};
}

#[tokio::test]
async fn test_executor() {
// TODO:
// let executor = Executor::default();
// executor.
}
17 changes: 17 additions & 0 deletions src/router_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,21 @@ where

BuildResult::Ok(router)
}

// pub fn build2(self, ctx_fn: impl Fn() -> TCtx) -> () {
// if !self.errors.is_empty() {
// // return BuildResult::Err(self.errors);
// todo!();
// }

// let mut router = Router::default();
// for (key, build_fn) in self.procedures.into_iter() {
// // TODO: Pass in the `key` here with the router merging prefixes already applied so it's the final runtime key
// (build_fn)(key, &mut router);
// }

// // router.

// todo!();
// }
}

0 comments on commit e1cde93

Please sign in to comment.