Skip to main content

spec_driven_docs/commands/
read.rs

1//! The embedded-document readers: runtime-shape.
2//!
3//! One handler serves `method`, `spec`, and `template` — the shelf is the
4//! only difference. Shelf semantics live in `services::reader`.
5
6use crate::cli::read::ReadArgs;
7use crate::context::AppContext;
8use crate::error::AppError;
9use crate::output;
10use crate::services::reader::{Shelf, get, list};
11
12/// Read one document from a shelf, or list it.
13///
14/// # Errors
15///
16/// [`AppError::Usage`] when the name resolves to nothing.
17pub fn run(_ctx: &AppContext, shelf: &Shelf, args: ReadArgs) -> Result<(), AppError> {
18    if args.list {
19        for name in list(shelf) {
20            output::line(name);
21        }
22        return Ok(());
23    }
24    let Some(name) = args.name else {
25        return Err(AppError::Usage(
26            "a document name or --list is required".to_string(),
27        ));
28    };
29    let Some(text) = get(shelf, &name) else {
30        return Err(AppError::Usage(format!("no such document: {name}")));
31    };
32    output::line(text.trim_end_matches('\n'));
33    Ok(())
34}