Skip to content

Commit

Permalink
more RE
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu committed Oct 26, 2023
1 parent a8aeb72 commit d75fc76
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions fastn-core/src/tutor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use itertools::Itertools;

pub async fn pwd() -> fastn_core::Result<fastn_core::http::Response> {
if !is_tutor() {
return Ok(fastn_core::not_found!("this only works in tutor mode"));
Expand Down Expand Up @@ -57,17 +55,19 @@ impl TutorStateFS {
self: TutorStateFS,
path: T,
) -> ftd::interpreter::Result<TutorState> {
use itertools::Itertools;

let mut workshops = vec![];
static RE: once_cell::sync::Lazy<regex::Regex> =
once_cell::sync::Lazy::new(|| regex::Regex::new(r"^[a-zA-Z]-[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;
}
Expand All @@ -80,11 +80,12 @@ impl TutorStateFS {
}

fn sort_path(
a: &std::result::Result<std::fs::DirEntry, std::io::Error>,
b: &std::result::Result<std::fs::DirEntry, std::io::Error>,
a: &std::io::Result<std::fs::DirEntry>,
b: &std::io::Result<std::fs::DirEntry>,
) -> std::cmp::Ordering {
a.as_ref().unwrap().path().cmp(&b.as_ref().unwrap().path())
}

#[derive(Debug, serde::Serialize, PartialEq)]
struct Workshop {
title: String,
Expand All @@ -96,15 +97,24 @@ struct Workshop {

impl Workshop {
fn load(path: &std::path::Path, state: &TutorStateFS) -> ftd::interpreter::Result<Self> {
use itertools::Itertools;

let mut tutorials = vec![];
let id = path.file_name().unwrap().to_string_lossy();

static RE: once_cell::sync::Lazy<regex::Regex> =
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)?);
}
Expand Down

0 comments on commit d75fc76

Please sign in to comment.