use capsec::prelude::*;
#[capsec::context]
struct IngestCtx {
fs: FsRead,
}
#[capsec::context]
struct OutputCtx {
fs: FsWrite,
net: NetConnect,
}
fn ingest(path: &str, ctx: &IngestCtx) -> Result<String, CapSecError> {
capsec::fs::read_to_string(path, ctx)
}
fn publish(path: &str, data: &str, ctx: &OutputCtx) -> Result<(), CapSecError> {
capsec::fs::write(path, data.as_bytes(), ctx)
}
fn transform(data: &str) -> String {
data.lines()
.filter(|l| !l.trim().is_empty())
.map(|l| format!(" > {l}"))
.collect::<Vec<_>>()
.join("\n")
}
#[capsec::main]
fn main(root: CapRoot) -> Result<(), Box<dyn std::error::Error>> {
let ingest_ctx = IngestCtx::new(&root);
let output_ctx = OutputCtx::new(&root);
let raw = ingest("/etc/hostname", &ingest_ctx)?;
let processed = transform(&raw);
publish("/tmp/capsec-context-demo.txt", &processed, &output_ctx)?;
println!("Processed {} bytes -> {} bytes", raw.len(), processed.len());
Ok(())
}