Skip to content

Commit

Permalink
mut version of providers
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu committed Dec 8, 2024
1 parent e047f8b commit 9bd1cb0
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 22 deletions.
1 change: 1 addition & 0 deletions v0.5/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions v0.5/fastn-continuation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ mod result;
mod ur;

#[cfg(feature = "async_provider")]
pub use provider::{AsyncProvider, AsyncProviderWith};
pub use provider::{Provider, ProviderWith};
pub use provider::{AsyncMutProvider, AsyncMutProviderWith, AsyncProvider, AsyncProviderWith};
pub use provider::{MutProvider, MutProviderWith, Provider, ProviderWith};
pub use result::Result;
pub use ur::{FromWith, UR};

Expand Down
34 changes: 34 additions & 0 deletions v0.5/fastn-continuation/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,37 @@ pub trait ProviderWith {

fn provide(&self, context: &mut Self::Context, needed: Self::Needed) -> Self::Found;
}

pub trait MutProvider {
type Needed;
type Found;

fn provide(&mut self, needed: Self::Needed) -> Self::Found;
}

#[cfg(feature = "async_provider")]
#[async_trait::async_trait]
pub trait AsyncMutProvider {
type Needed;
type Found;

async fn provide(&mut self, needed: Self::Needed) -> Self::Found;
}

#[cfg(feature = "async_provider")]
#[async_trait::async_trait]
pub trait AsyncMutProviderWith {
type Needed;
type Found;
type Context;

async fn provide(&mut self, context: &mut Self::Context, needed: Self::Needed) -> Self::Found;
}

pub trait MutProviderWith {
type Needed;
type Found;
type Context;

fn provide(&mut self, context: &mut Self::Context, needed: Self::Needed) -> Self::Found;
}
80 changes: 80 additions & 0 deletions v0.5/fastn-continuation/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,84 @@ where
}
}
}

pub fn mut_consume<P>(mut self, mut p: P) -> C::Output
where
P: fastn_continuation::MutProvider<Needed = C::Needed, Found = C::Found>,
{
loop {
match self {
fastn_continuation::Result::Init(ic) => {
self = ic.continue_after(Default::default());
}
fastn_continuation::Result::Stuck(ic, needed) => {
self = ic.continue_after(p.provide(needed));
}
fastn_continuation::Result::Done(c) => {
return c;
}
}
}
}

pub fn mut_consume_with<P>(mut self, mut p: P) -> C::Output
where
P: fastn_continuation::MutProviderWith<Needed = C::Needed, Found = C::Found, Context = C>,
{
loop {
match self {
fastn_continuation::Result::Init(ic) => {
self = ic.continue_after(Default::default());
}
fastn_continuation::Result::Stuck(mut ic, needed) => {
let o = p.provide(&mut ic, needed);
self = ic.continue_after(o);
}
fastn_continuation::Result::Done(c) => {
return c;
}
}
}
}

#[cfg(feature = "async_provider")]
pub async fn mut_consume_async<P>(mut self, mut p: P) -> C::Output
where
P: fastn_continuation::AsyncMutProvider<Needed = C::Needed, Found = C::Found>,
{
loop {
match self {
fastn_continuation::Result::Init(ic) => {
self = ic.continue_after(Default::default());
}
fastn_continuation::Result::Stuck(ic, needed) => {
self = ic.continue_after(p.provide(needed).await);
}
fastn_continuation::Result::Done(c) => {
return c;
}
}
}
}

#[cfg(feature = "async_provider")]
pub async fn mut_consume_with_async<P>(mut self, p: P) -> C::Output
where
P: fastn_continuation::AsyncProviderWith<Needed = C::Needed, Found = C::Found, Context = C>,
{
loop {
match self {
fastn_continuation::Result::Init(ic) => {
self = ic.continue_after(Default::default());
}
fastn_continuation::Result::Stuck(mut ic, needed) => {
let o = p.provide(&mut ic, needed).await;
self = ic.continue_after(o);
}
fastn_continuation::Result::Done(c) => {
return c;
}
}
}
}
}
3 changes: 2 additions & 1 deletion v0.5/fastn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ repository.workspace = true
homepage.workspace = true

[dependencies]
async-trait.workspace = true
fastn-compiler.workspace = true
fastn-package.workspace = true
fastn-section.workspace = true
fastn-continuation.workspace = true
fastn-continuation = { workspace = true, features = ["async_provider"] }
fastn-router.workspace = true
fastn-runtime.workspace = true
fastn-unresolved.workspace = true
Expand Down
20 changes: 4 additions & 16 deletions v0.5/fastn/src/commands/render.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
impl fastn::commands::Render {
pub async fn run(self, _package: &mut fastn_package::Package, _router: fastn_router::Router) {
let route = fastn_router::Router::reader().consume(&self).route(
"/",
fastn_router::Method::Get,
&[],
);
pub async fn run(self, _package: &mut fastn_package::Package, router: fastn_router::Router) {
let route = router.route("/", fastn_router::Method::Get, &[]);
match route {
fastn_router::Route::Document(path, data) => {
let html =
Expand All @@ -17,20 +13,12 @@ impl fastn::commands::Render {
}
}

impl fastn_continuation::Provider for &fastn::commands::Render {
type Needed = Vec<String>;
type Found = Vec<(String, Option<fastn_section::Document>)>;

fn provide(&self, _needed: Self::Needed) -> Self::Found {
todo!()
}
}

pub async fn render_document(path: &str, _data: serde_json::Value, _strict: bool) -> String {
let source = std::fs::File::open(path)
.and_then(std::io::read_to_string)
.unwrap();
let o = fastn_compiler::compile(&source, "main", None).consume_with_fn(fastn::symbols::lookup);
let o = fastn_compiler::compile(&source, "main", None)
.consume_with_fn(fastn::definition_provider::lookup);
let h = fastn_runtime::HtmlData::from_cd(o.unwrap());
h.to_test_html()
}
File renamed without changes.
5 changes: 4 additions & 1 deletion v0.5/fastn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ extern crate self as fastn;
use tokio as _;

pub mod commands;
mod symbols;
mod definition_provider;
mod section_provider;

pub use section_provider::SectionProvider;

pub enum Action {
Read,
Expand Down
7 changes: 5 additions & 2 deletions v0.5/fastn/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#[tokio::main]
async fn main() {
let command = fastn::commands::parse();
let mut package = fastn_package::Package::reader().consume_fn(fastn::full_filler);
let section_provider = fastn::SectionProvider::default();
let mut package = fastn_package::Package::reader()
.mut_consume_async(&section_provider)
.await;
let router = fastn_router::Router::reader()
.consume_async_fn(fastn::full_filler_async)
.mut_consume_async(&section_provider)
.await;
// read config here and pass to everyone?
// do common build stuff here
Expand Down
12 changes: 12 additions & 0 deletions v0.5/fastn/src/section_provider.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#[derive(Default)]
pub struct SectionProvider {}

#[async_trait::async_trait]
impl fastn_continuation::AsyncMutProvider for &SectionProvider {
type Needed = Vec<String>;
type Found = Vec<(String, Option<fastn_section::Document>)>;

async fn provide(&mut self, _needed: Vec<String>) -> Self::Found {
todo!()
}
}

0 comments on commit 9bd1cb0

Please sign in to comment.