From 350cb0cb6f674ec72bb1337f5ca6891f1d77b5cd Mon Sep 17 00:00:00 2001 From: Amit Upadhyay Date: Wed, 25 Oct 2023 16:56:30 +0530 Subject: [PATCH] wip processor --- fastn-core/src/library2022/mod.rs | 1 + fastn-core/src/tutor.rs | 39 +++++++++++++++++++++++++++++++ fastn/src/main.rs | 2 +- 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/fastn-core/src/library2022/mod.rs b/fastn-core/src/library2022/mod.rs index 3d9182cff1..785aae6d03 100644 --- a/fastn-core/src/library2022/mod.rs +++ b/fastn-core/src/library2022/mod.rs @@ -235,6 +235,7 @@ impl Library2022 { processor::figma_tokens::process_figma_tokens_old(value, kind, doc, &self.config) } "http" => processor::http::process(value, kind, doc, &self.config).await, + "tutor" => fastn_core::tutor::process(value, kind, doc).await, "toc" => processor::toc::process(value, kind, doc, &self.config), "get-data" => processor::get_data::process(value, kind, doc, &self.config), "sitemap" => processor::sitemap::process(value, kind, doc, &self.config), diff --git a/fastn-core/src/tutor.rs b/fastn-core/src/tutor.rs index 753a79323d..c6f3074a38 100644 --- a/fastn-core/src/tutor.rs +++ b/fastn-core/src/tutor.rs @@ -15,6 +15,45 @@ pub async fn shutdown() -> fastn_core::Result { std::process::exit(0); } +pub async fn process( + value: ftd::ast::VariableValue, + kind: ftd::interpreter::Kind, + doc: &ftd::interpreter::TDoc<'_>, +) -> ftd::interpreter::Result { + if !fastn_core::tutor::is_tutor() { + return Err(ftd::interpreter::Error::OtherError( + "tutor process only works in tutor mode".to_string(), + )); + } + + let state = match tokio::fs::read( + std::path::PathBuf::from(dirs::home_dir().unwrap()) + .join(".fastn") + .join("tutor.json"), + ) + .await + { + Ok(v) => serde_json::from_slice(&v)?, + Err(e) => match e.kind() { + std::io::ErrorKind::NotFound => TutorState::default(), + _ => { + return Err(ftd::interpreter::Error::OtherError(format!( + "tutor error: {}", + e.to_string() + ))) + } + }, + }; + + doc.from_json(&state, &kind, &value) +} + +#[derive(Debug, Default, serde::Deserialize, serde::Serialize)] +struct TutorState { + done: Vec, + current: String, +} + pub fn is_tutor() -> bool { // https://github.com/orgs/fastn-stack/discussions/1414 // with either of these are passed we allow APIs like /-/shutdown/, `/-/start/` etc diff --git a/fastn/src/main.rs b/fastn/src/main.rs index 4c74907afe..db4d787b91 100644 --- a/fastn/src/main.rs +++ b/fastn/src/main.rs @@ -479,7 +479,7 @@ fn app(version: &'static str) -> clap::Command { .hide(true) // hidden since the feature is not being released yet. ) .subcommand( - clap::Command::new("tutor").about("Start fastn tutor") + clap::Command::new("tutor").about("Start fastn tutor").hide(true) ) .subcommand( clap::Command::new("start-tracking")