use pi_result::{
attachment::{Module, Operation, PathAttachment, Stage},
prelude::*,
testing::{assert_contains_text, assert_has_attachment, assert_has_context, assert_kind},
};
use std::path::{Path, PathBuf};
#[derive(Debug, ThisError, PartialEq, Eq)]
#[error("leaf io error")]
struct LeafIoError;
#[derive(Debug, ThisError, PartialEq, Eq)]
enum ParseContext {
#[error("parse decode failed")]
Decode,
}
#[derive(Debug, ThisError, PartialEq, Eq)]
enum ConfigContext {
#[error("config load failed")]
Load,
}
#[derive(Debug, ThisError, PartialEq, Eq)]
enum RuntimeContext {
#[error("runtime prepare failed")]
Prepare,
}
#[derive(Debug, ThisError, PartialEq, Eq)]
enum ServiceContext {
#[error("service init failed")]
Init,
}
mod leaf {
use super::*;
pub fn step01_read_source(_: &Path) -> RawResult<String, LeafIoError> {
Err(LeafIoError)
}
}
mod parse {
use super::*;
pub fn step02_decode(path: &Path) -> TypedResult<String, ParseContext> {
leaf::step01_read_source(path)
.attach(PathAttachment(path.to_path_buf()))
.attach(Operation("step02_decode"))
.into_typed(ParseContext::Decode)
}
pub fn step03_validate(path: &Path) -> TypedResult<String, ParseContext> {
let text = step02_decode(path)
.attach(Operation("step03_validate"))
.attach(Module("parse"))?;
Ok(text)
}
}
mod config {
use super::*;
pub fn step04_load(path: &Path) -> TypedResult<String, ConfigContext> {
parse::step03_validate(path)
.attach(Operation("step04_load"))
.into_typed(ConfigContext::Load)
}
pub fn step05_normalize(path: &Path) -> TypedResult<String, ConfigContext> {
let text = step04_load(path)
.attach(Operation("step05_normalize"))
.attach(Stage("config"))?;
Ok(text)
}
}
mod runtime {
use super::*;
pub fn step06_prepare(path: &Path) -> TypedResult<String, RuntimeContext> {
config::step05_normalize(path)
.attach(Operation("step06_prepare"))
.into_typed(RuntimeContext::Prepare)
}
pub fn step07_bind(path: &Path) -> TypedResult<String, RuntimeContext> {
let text = step06_prepare(path)
.attach(Operation("step07_bind"))
.attach(Module("runtime"))?;
Ok(text)
}
}
mod service {
use super::*;
pub fn step08_bootstrap(path: &Path) -> TypedResult<String, ServiceContext> {
runtime::step07_bind(path)
.attach(Operation("step08_bootstrap"))
.into_typed(ServiceContext::Init)
}
pub fn step09_warmup(path: &Path) -> TypedResult<String, ServiceContext> {
let text = step08_bootstrap(path)
.attach(Operation("step09_warmup"))
.attach(Stage("service"))?;
Ok(text)
}
}
mod app {
use super::*;
pub fn step10_boot(path: &Path) -> Result<String> {
service::step09_warmup(path)
.attach(Operation("step10_boot"))
.into_error(ErrorKind::Dependency)
}
pub fn step11_start(path: &Path) -> Result<String> {
let text = step10_boot(path)
.attach(Operation("step11_start"))
.attach(Module("app"))?;
Ok(text)
}
pub fn step12_run(path: &Path) -> Result<()> {
let _ = step11_start(path)
.attach(Operation("step12_run"))
.attach_with(|| format!("entry_path={}", path.display()))?;
Ok(())
}
}
#[test]
fn test_deep_stack_spans_more_than_ten_frames_and_multiple_context_layers() {
let path = PathBuf::from("deep/missing-config.toml");
let report = app::step12_run(&path).unwrap_err();
assert_kind(&report, ErrorKind::Dependency);
assert_has_context::<LeafIoError>(&report);
assert_has_context::<ParseContext>(&report);
assert_has_context::<ConfigContext>(&report);
assert_has_context::<RuntimeContext>(&report);
assert_has_context::<ServiceContext>(&report);
assert_has_attachment::<Operation>(&report);
assert_has_attachment::<Module>(&report);
assert_has_attachment::<Stage>(&report);
assert_has_attachment::<PathAttachment>(&report);
assert_has_attachment::<String>(&report);
assert_contains_text(&report, "deep/missing-config.toml");
let frame_count = report.frames().count();
assert!(
frame_count > 10,
"expected deep stack to have more than 10 frames, got {frame_count}"
);
}
#[test]
fn test_deep_stack_keeps_latest_and_intermediate_attachments_observable() {
let path = PathBuf::from("deep/missing-config.toml");
let report = app::step12_run(&path).unwrap_err();
assert_eq!(
report.attachment_ref::<Operation>(),
Some(&Operation("step12_run"))
);
assert!(debug_message(&report).contains("step04_load"));
assert!(debug_message(&report).contains("step08_bootstrap"));
assert!(verbose_message(&report).contains("dependency failure"));
}