#![feature(round_char_boundary)] #![feature(exact_size_is_empty)] use std::io::Read; use std::path::Path; use std::path::PathBuf; use futures::future::BoxFuture; use futures::future::FutureExt; use organic::compare::run_anonymous_compare; use organic::compare::run_compare_on_file; use tokio::sync::Semaphore; use tokio::task::JoinError; #[cfg(feature = "tracing")] use crate::init_tracing::init_telemetry; #[cfg(feature = "tracing")] use crate::init_tracing::shutdown_telemetry; #[cfg(feature = "tracing")] mod init_tracing; #[cfg(not(feature = "tracing"))] fn main() -> Result<(), Box> { let rt = tokio::runtime::Runtime::new()?; let result = rt.block_on(async { let main_body_result = main_body().await; main_body_result }); result } #[cfg(feature = "tracing")] fn main() -> Result<(), Box> { let rt = tokio::runtime::Runtime::new()?; let result = rt.block_on(async { init_telemetry()?; let main_body_result = main_body().await; shutdown_telemetry()?; main_body_result }); result } #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] async fn main_body() -> Result<(), Box> { let single_file = TestConfig::SingleFile(SingleFile { file_path: PathBuf::from("/tmp/test.org"), }); // let result = single_file.run_test().await; let result = tokio::spawn(single_file.run_test()).await; println!("{:?}", result); // let test_config = TestConfig::TestLayer(TestLayer { // name: "foo", // children: vec![TestConfig::SingleFile(SingleFile { // file_path: Path::new("/tmp/test.org"), // })], // }); Ok(()) } static TEST_PERMITS: Semaphore = Semaphore::const_new(8); #[derive(Debug)] enum TestConfig { TestLayer(TestLayer), SingleFile(SingleFile), } #[derive(Debug)] struct TestLayer { name: String, children: Vec, } #[derive(Debug)] struct SingleFile { file_path: PathBuf, } #[derive(Debug)] enum TestResult { ResultLayer(ResultLayer), SingleFileResult(SingleFileResult), } #[derive(Debug)] struct ResultLayer { name: String, children: Vec, } #[derive(Debug)] struct SingleFileResult { file_path: PathBuf, status: TestStatus, } #[derive(Debug)] pub(crate) enum TestStatus { Pass, Fail, } impl TestConfig { fn run_test(self) -> BoxFuture<'static, Result> { async move { match self { TestConfig::TestLayer(test) => Ok(TestResult::ResultLayer(test.run_test().await?)), TestConfig::SingleFile(test) => { Ok(TestResult::SingleFileResult(test.run_test().await?)) } } } .boxed() } } impl SingleFile { async fn run_test(self) -> Result { let _permit = TEST_PERMITS.acquire().await.unwrap(); let result = run_compare_on_file(&self.file_path); Ok(SingleFileResult { file_path: self.file_path, status: if result.is_ok() { TestStatus::Pass } else { TestStatus::Fail }, }) } } impl TestLayer { async fn run_test(self) -> Result { let running_children: Vec<_> = self .children .into_iter() .map(|c| tokio::spawn(c.run_test())) .collect(); let mut children = Vec::with_capacity(running_children.len()); for c in running_children { children.push(c.await??); } Ok(ResultLayer { name: self.name, children, }) } }