use std::path::{Path, PathBuf};
use crate::error::NbError;
use crate::native::{confine, ensure_no_symlink_prefix};
fn scope_dir_abs(
notebook_root: &Path,
folder: &str,
) -> Result<PathBuf, NbError> {
let scope_rel = if folder.is_empty() {
String::new()
} else {
match confine(folder).filter(|r| !r.is_empty()) {
Some(rel) => rel,
None => {
return Err(NbError::NotFound {
selector: folder.to_string(),
});
},
}
};
if !scope_rel.is_empty() {
ensure_no_symlink_prefix(notebook_root, &scope_rel).map_err(|e| {
match e {
NbError::NotFound { .. } => NbError::NotFound {
selector: folder.to_string(),
},
other => other,
}
})?;
}
let abs = notebook_root.join(&scope_rel);
match std::fs::symlink_metadata(&abs) {
Ok(meta) if meta.file_type().is_symlink() => Err(NbError::NotFound {
selector: folder.to_string(),
}),
Ok(meta) if meta.is_dir() => Ok(abs),
Ok(_) => Err(NbError::NotFound {
selector: folder.to_string(),
}),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
Err(NbError::NotFound {
selector: folder.to_string(),
})
},
Err(e) => Err(NbError::Io {
path: abs,
source: e.into(),
}),
}
}
fn scope_entries(
notebook_root: &Path,
folder: &str,
files: bool,
) -> Result<Vec<String>, NbError> {
let abs = scope_dir_abs(notebook_root, folder)?;
let scope_rel = if folder.is_empty() {
String::new()
} else {
confine(folder)
.filter(|r| !r.is_empty())
.unwrap_or_default()
};
let mut out = Vec::new();
let entries = std::fs::read_dir(&abs).map_err(|e| NbError::Io {
path: abs.clone(),
source: e.into(),
})?;
for entry in entries {
let entry = entry.map_err(|e| NbError::Io {
path: abs.clone(),
source: e.into(),
})?;
let path = entry.path();
let file_type = entry.file_type().map_err(|e| NbError::Io {
path: path.clone(),
source: e.into(),
})?;
let wanted = if files {
file_type.is_file()
} else {
file_type.is_dir()
};
if file_type.is_symlink() || !wanted {
continue;
}
let Some(name) = path.file_name().and_then(|s| s.to_str()) else {
continue;
};
if name.starts_with('.') {
continue;
}
if scope_rel.is_empty() {
out.push(name.to_string());
} else {
out.push(format!("{scope_rel}/{name}"));
}
}
out.sort();
Ok(out)
}
pub(crate) fn scope_files_recursive(
notebook_root: &Path,
folder: &str,
) -> Result<Vec<String>, NbError> {
fn walk(
abs: &Path,
rel: &str,
out: &mut Vec<String>,
) -> Result<(), NbError> {
let entries = std::fs::read_dir(abs).map_err(|e| NbError::Io {
path: abs.to_path_buf(),
source: e.into(),
})?;
let mut names: Vec<(String, PathBuf, bool)> = Vec::new();
for entry in entries {
let entry = entry.map_err(|e| NbError::Io {
path: abs.to_path_buf(),
source: e.into(),
})?;
let path = entry.path();
let file_type = entry.file_type().map_err(|e| NbError::Io {
path: path.clone(),
source: e.into(),
})?;
if file_type.is_symlink()
|| (!file_type.is_dir() && !file_type.is_file())
{
continue;
}
let Some(name) = path.file_name().and_then(|s| s.to_str()) else {
continue;
};
if name.starts_with('.') {
continue;
}
names.push((name.to_string(), path, file_type.is_dir()));
}
names.sort();
for (name, path, is_dir) in names {
let child = if rel.is_empty() {
name
} else {
format!("{rel}/{name}")
};
if is_dir {
walk(&path, &child, out)?;
} else {
out.push(child);
}
}
Ok(())
}
let abs = scope_dir_abs(notebook_root, folder)?;
let scope_rel = if folder.is_empty() {
String::new()
} else {
confine(folder)
.filter(|r| !r.is_empty())
.unwrap_or_default()
};
let mut out = Vec::new();
walk(&abs, &scope_rel, &mut out)?;
out.sort();
Ok(out)
}
pub(crate) fn todo_items(source: &[u8]) -> Vec<bool> {
let mut out = Vec::new();
for line in source.split(|b| *b == b'\n') {
let line = line.strip_suffix(b"\r").unwrap_or(line);
if line.starts_with(b"- [ ]") {
out.push(false);
} else if line.starts_with(b"- [x]") || line.starts_with(b"- [X]") {
out.push(true);
}
}
out
}
pub(crate) fn scope_files(
notebook_root: &Path,
folder: &str,
) -> Result<Vec<String>, NbError> {
scope_entries(notebook_root, folder, true)
}
pub(crate) fn scope_dirs(
notebook_root: &Path,
folder: &str,
) -> Result<Vec<String>, NbError> {
scope_entries(notebook_root, folder, false)
}