Hit recursive async.

This commit is contained in:
Tom Alexander 2023-10-11 19:41:32 -04:00
parent a1f8cbe079
commit 5f93cabff5
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -2,6 +2,7 @@
#![feature(exact_size_is_empty)] #![feature(exact_size_is_empty)]
use std::io::Read; use std::io::Read;
use std::path::Path; use std::path::Path;
use std::path::PathBuf;
use organic::compare::run_anonymous_compare; use organic::compare::run_anonymous_compare;
use organic::compare::run_compare_on_file; use organic::compare::run_compare_on_file;
@ -37,47 +38,53 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
async fn main_body() -> Result<(), Box<dyn std::error::Error>> { async fn main_body() -> Result<(), Box<dyn std::error::Error>> {
let test_config = TestConfig::TestLayer(TestLayer { let single_file = TestConfig::SingleFile(SingleFile {
name: "foo", file_path: PathBuf::from("/tmp/test.org"),
children: vec![TestConfig::SingleFile(SingleFile {
file_path: Path::new("/tmp/test.org"),
})],
}); });
// let result = single_file.run_test().await;
let result = tokio::spawn(single_file.run_test());
println!("{:?}", result);
// let test_config = TestConfig::TestLayer(TestLayer {
// name: "foo",
// children: vec![TestConfig::SingleFile(SingleFile {
// file_path: Path::new("/tmp/test.org"),
// })],
// });
Ok(()) Ok(())
} }
#[derive(Debug)] #[derive(Debug)]
enum TestConfig<'s> { enum TestConfig {
TestLayer(TestLayer<'s>), TestLayer(TestLayer),
SingleFile(SingleFile<'s>), SingleFile(SingleFile),
} }
#[derive(Debug)] #[derive(Debug)]
struct TestLayer<'s> { struct TestLayer {
name: &'s str, name: String,
children: Vec<TestConfig<'s>>, children: Vec<TestConfig>,
} }
#[derive(Debug)] #[derive(Debug)]
struct SingleFile<'s> { struct SingleFile {
file_path: &'s Path, file_path: PathBuf,
} }
#[derive(Debug)] #[derive(Debug)]
enum TestResult<'s> { enum TestResult {
ResultLayer(ResultLayer<'s>), ResultLayer(ResultLayer),
SingleFileResult(SingleFileResult<'s>), SingleFileResult(SingleFileResult),
} }
#[derive(Debug)] #[derive(Debug)]
struct ResultLayer<'s> { struct ResultLayer {
name: &'s str, name: String,
children: Vec<TestResult<'s>>, children: Vec<TestResult>,
} }
#[derive(Debug)] #[derive(Debug)]
struct SingleFileResult<'s> { struct SingleFileResult {
file_path: &'s Path, file_path: PathBuf,
status: TestStatus, status: TestStatus,
} }
@ -87,8 +94,8 @@ pub(crate) enum TestStatus {
Bad, Bad,
} }
impl<'s> TestConfig<'s> { impl TestConfig {
async fn run_test(&self) -> TestResult<'s> { async fn run_test(self) -> TestResult {
match self { match self {
TestConfig::TestLayer(test) => TestResult::ResultLayer(test.run_test().await), TestConfig::TestLayer(test) => TestResult::ResultLayer(test.run_test().await),
TestConfig::SingleFile(test) => TestResult::SingleFileResult(test.run_test().await), TestConfig::SingleFile(test) => TestResult::SingleFileResult(test.run_test().await),
@ -96,9 +103,9 @@ impl<'s> TestConfig<'s> {
} }
} }
impl<'s> SingleFile<'s> { impl SingleFile {
async fn run_test(&self) -> SingleFileResult<'s> { async fn run_test(self) -> SingleFileResult {
let result = run_compare_on_file(self.file_path); let result = run_compare_on_file(&self.file_path);
SingleFileResult { SingleFileResult {
file_path: self.file_path, file_path: self.file_path,
status: if result.is_ok() { status: if result.is_ok() {
@ -110,8 +117,16 @@ impl<'s> SingleFile<'s> {
} }
} }
impl<'s> TestLayer<'s> { impl TestLayer {
async fn run_test(&self) -> ResultLayer<'s> { async fn run_test(self) -> ResultLayer {
todo!() let mut children = Vec::with_capacity(self.children.len());
for config in self.children {
let result = config.run_test().await;
children.push(result);
}
ResultLayer {
name: self.name,
children,
}
} }
} }