From 9bd1cb02a4b76291fdb07e87bcef185860c28c75 Mon Sep 17 00:00:00 2001 From: Amit Upadhyay Date: Sun, 8 Dec 2024 21:10:12 +0530 Subject: [PATCH] mut version of providers --- v0.5/Cargo.lock | 1 + v0.5/fastn-continuation/src/lib.rs | 4 +- v0.5/fastn-continuation/src/provider.rs | 34 ++++++++ v0.5/fastn-continuation/src/result.rs | 80 +++++++++++++++++++ v0.5/fastn/Cargo.toml | 3 +- v0.5/fastn/src/commands/render.rs | 20 +---- .../{symbols.rs => definition_provider.rs} | 0 v0.5/fastn/src/lib.rs | 5 +- v0.5/fastn/src/main.rs | 7 +- v0.5/fastn/src/section_provider.rs | 12 +++ 10 files changed, 144 insertions(+), 22 deletions(-) rename v0.5/fastn/src/{symbols.rs => definition_provider.rs} (100%) create mode 100644 v0.5/fastn/src/section_provider.rs diff --git a/v0.5/Cargo.lock b/v0.5/Cargo.lock index 62a269a49..85c52f5da 100644 --- a/v0.5/Cargo.lock +++ b/v0.5/Cargo.lock @@ -157,6 +157,7 @@ checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" name = "fastn" version = "0.1.0" dependencies = [ + "async-trait", "fastn-compiler", "fastn-continuation", "fastn-package", diff --git a/v0.5/fastn-continuation/src/lib.rs b/v0.5/fastn-continuation/src/lib.rs index 7fdc5eae9..17078895d 100644 --- a/v0.5/fastn-continuation/src/lib.rs +++ b/v0.5/fastn-continuation/src/lib.rs @@ -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}; diff --git a/v0.5/fastn-continuation/src/provider.rs b/v0.5/fastn-continuation/src/provider.rs index 3152d1329..2c19fb0e3 100644 --- a/v0.5/fastn-continuation/src/provider.rs +++ b/v0.5/fastn-continuation/src/provider.rs @@ -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; +} diff --git a/v0.5/fastn-continuation/src/result.rs b/v0.5/fastn-continuation/src/result.rs index d0b80ea73..260007736 100644 --- a/v0.5/fastn-continuation/src/result.rs +++ b/v0.5/fastn-continuation/src/result.rs @@ -174,4 +174,84 @@ where } } } + + pub fn mut_consume

(mut self, mut p: P) -> C::Output + where + P: fastn_continuation::MutProvider, + { + 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

(mut self, mut p: P) -> C::Output + where + P: fastn_continuation::MutProviderWith, + { + 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

(mut self, mut p: P) -> C::Output + where + P: fastn_continuation::AsyncMutProvider, + { + 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

(mut self, p: P) -> C::Output + where + P: fastn_continuation::AsyncProviderWith, + { + 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; + } + } + } + } } diff --git a/v0.5/fastn/Cargo.toml b/v0.5/fastn/Cargo.toml index 7e057b3f9..87faffe73 100644 --- a/v0.5/fastn/Cargo.toml +++ b/v0.5/fastn/Cargo.toml @@ -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 diff --git a/v0.5/fastn/src/commands/render.rs b/v0.5/fastn/src/commands/render.rs index 08478162a..0861f9671 100644 --- a/v0.5/fastn/src/commands/render.rs +++ b/v0.5/fastn/src/commands/render.rs @@ -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 = @@ -17,20 +13,12 @@ impl fastn::commands::Render { } } -impl fastn_continuation::Provider for &fastn::commands::Render { - type Needed = Vec; - type Found = Vec<(String, Option)>; - - 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() } diff --git a/v0.5/fastn/src/symbols.rs b/v0.5/fastn/src/definition_provider.rs similarity index 100% rename from v0.5/fastn/src/symbols.rs rename to v0.5/fastn/src/definition_provider.rs diff --git a/v0.5/fastn/src/lib.rs b/v0.5/fastn/src/lib.rs index f1c8d4032..bc53996cd 100644 --- a/v0.5/fastn/src/lib.rs +++ b/v0.5/fastn/src/lib.rs @@ -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, diff --git a/v0.5/fastn/src/main.rs b/v0.5/fastn/src/main.rs index 73efeafa7..262178ca6 100644 --- a/v0.5/fastn/src/main.rs +++ b/v0.5/fastn/src/main.rs @@ -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(§ion_provider) + .await; let router = fastn_router::Router::reader() - .consume_async_fn(fastn::full_filler_async) + .mut_consume_async(§ion_provider) .await; // read config here and pass to everyone? // do common build stuff here diff --git a/v0.5/fastn/src/section_provider.rs b/v0.5/fastn/src/section_provider.rs new file mode 100644 index 000000000..bf97bfc13 --- /dev/null +++ b/v0.5/fastn/src/section_provider.rs @@ -0,0 +1,12 @@ +#[derive(Default)] +pub struct SectionProvider {} + +#[async_trait::async_trait] +impl fastn_continuation::AsyncMutProvider for &SectionProvider { + type Needed = Vec; + type Found = Vec<(String, Option)>; + + async fn provide(&mut self, _needed: Vec) -> Self::Found { + todo!() + } +}