From 35ed74d092852311dd0eaf5cf815a6f5aaba2902 Mon Sep 17 00:00:00 2001 From: Amit Upadhyay Date: Thu, 26 Oct 2023 14:56:56 +0530 Subject: [PATCH] more RE --- fastn-core/src/tutor.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/fastn-core/src/tutor.rs b/fastn-core/src/tutor.rs index cfbaf324fe..65c3229a01 100644 --- a/fastn-core/src/tutor.rs +++ b/fastn-core/src/tutor.rs @@ -1,5 +1,3 @@ -use itertools::Itertools; - pub async fn pwd() -> fastn_core::Result { if !is_tutor() { return Ok(fastn_core::not_found!("this only works in tutor mode")); @@ -57,6 +55,8 @@ impl TutorStateFS { self: TutorStateFS, path: T, ) -> ftd::interpreter::Result { + use itertools::Itertools; + let mut workshops = vec![]; static RE: once_cell::sync::Lazy = once_cell::sync::Lazy::new(|| regex::Regex::new(r"^[a-zA-Z]-[a-zA-Z]+.*$").unwrap()); @@ -64,10 +64,10 @@ impl TutorStateFS { for entry in std::fs::read_dir(path)?.sorted_by(sort_path) { let entry = entry?; let path = entry.path(); + if !path.is_dir() { continue; } - if !RE.is_match(&path.file_name().unwrap().to_string_lossy()) { continue; } @@ -80,11 +80,12 @@ impl TutorStateFS { } fn sort_path( - a: &std::result::Result, - b: &std::result::Result, + a: &std::io::Result, + b: &std::io::Result, ) -> std::cmp::Ordering { a.as_ref().unwrap().path().cmp(&b.as_ref().unwrap().path()) } + #[derive(Debug, serde::Serialize, PartialEq)] struct Workshop { title: String, @@ -96,15 +97,24 @@ struct Workshop { impl Workshop { fn load(path: &std::path::Path, state: &TutorStateFS) -> ftd::interpreter::Result { + use itertools::Itertools; + let mut tutorials = vec![]; let id = path.file_name().unwrap().to_string_lossy(); + static RE: once_cell::sync::Lazy = + once_cell::sync::Lazy::new(|| regex::Regex::new(r"^[0-9][0-9]-[a-zA-Z]+.*$").unwrap()); + for entry in std::fs::read_dir(path)?.sorted_by(sort_path) { let entry = entry?; let path = entry.path(); + if !path.is_dir() { continue; } + if !RE.is_match(&path.file_name().unwrap().to_string_lossy()) { + continue; + } tutorials.push(Tutorial::load(&id, &path, state)?); }