Skip to content

Commit

Permalink
wip processor
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu committed Oct 25, 2023
1 parent 70540b6 commit 350cb0c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions fastn-core/src/library2022/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
39 changes: 39 additions & 0 deletions fastn-core/src/tutor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,45 @@ pub async fn shutdown() -> fastn_core::Result<fastn_core::http::Response> {
std::process::exit(0);
}

pub async fn process(
value: ftd::ast::VariableValue,
kind: ftd::interpreter::Kind,
doc: &ftd::interpreter::TDoc<'_>,
) -> ftd::interpreter::Result<ftd::interpreter::Value> {
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())

Check failure on line 30 in fastn-core/src/tutor.rs

View workflow job for this annotation

GitHub Actions / Rust Checks

useless conversion to the same type: `std::path::PathBuf`
.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()

Check failure on line 42 in fastn-core/src/tutor.rs

View workflow job for this annotation

GitHub Actions / Rust Checks

`to_string` applied to a type that implements `Display` in `format!` args
)))
}
},
};

doc.from_json(&state, &kind, &value)
}

#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
struct TutorState {
done: Vec<String>,
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
Expand Down
2 changes: 1 addition & 1 deletion fastn/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 350cb0c

Please sign in to comment.