use camino::{Utf8Path, Utf8PathBuf};
use cap_std::{ambient_authority, fs_utf8::Dir};
use mdtablefix::io::SourceDocument;
use tempfile::{TempDir, tempdir};
use super::ReadOnlyDir;
pub(super) const RAGGED: &str = "|A|B|\n|---|---|\n|1|2|\n";
pub(super) const ALIGNED: &str = "| A | B |\n| --- | --- |\n| 1 | 2 |\n";
pub(super) fn identity(document: &SourceDocument<'_>) -> String {
document.render(
document
.body()
.lines()
.map(str::to_string)
.collect::<Vec<_>>()
.as_slice(),
)
}
pub(super) fn align(document: &SourceDocument<'_>) -> String {
document.render(
ALIGNED
.lines()
.map(str::to_string)
.collect::<Vec<_>>()
.as_slice(),
)
}
pub(super) fn fixture(name: &str, content: &str) -> (TempDir, Dir) {
let dir = tempdir().expect("create temporary directory");
let path = Utf8PathBuf::from_path_buf(dir.path().to_path_buf())
.expect("the temporary directory path is UTF-8");
let directory =
Dir::open_ambient_dir(&path, ambient_authority()).expect("open directory capability");
let name = Utf8Path::new(name);
if let Some(parent) = name.parent().filter(|parent| !parent.as_str().is_empty()) {
directory
.create_dir_all(parent)
.expect("create the fixture's parent directory");
}
directory.write(name, content).expect("write fixture");
(dir, directory)
}
pub(super) fn read(directory: &Dir, name: &str) -> String {
directory
.read_to_string(Utf8Path::new(name))
.expect("read fixture")
}
pub(super) fn readable(directory: &Dir) -> ReadOnlyDir {
ReadOnlyDir::new(
directory
.try_clone()
.expect("duplicate directory capability"),
)
}