cartulary 0.3.0-alpha.1

The knowledge layer of your project — decisions, issues, docs, all in one place.
Documentation
use clap::{ArgMatches, Command};

use crate::domain::usecases::issue::IssueRepository;
use crate::infra::driving::cli::commands::generic::created as generic;
use crate::infra::driving::cli::errors::{die1, CliError};
use crate::infra::driving::cli::helpers::required_str;
use crate::infra::driving::cli::id_parsing::parse_issue_id;
use crate::infra::driving::cli::Context;

const NOUN: &str = "issue";

pub(super) fn subcommand() -> Command {
    generic::subcommand(NOUN, "Issue ID (e.g. ISSUE-01H8MSQGXYZ12)")
}

pub(super) fn execute(matches: &ArgMatches, ctx: &Context<'_>) {
    match matches.subcommand() {
        Some(("show", sub)) => execute_show(sub, ctx),
        _ => unreachable!(),
    }
}

fn execute_show(sub: &ArgMatches, ctx: &Context<'_>) {
    let output_fmt = ctx.output_fmt;
    let id_str = required_str(sub, "id");
    let id = parse_issue_id(id_str, ctx.issues_id_prefix()).unwrap_or_else(|e| {
        die1(
            CliError::new(format!("invalid issue ID '{id_str}': {e}")).kind("validation"),
            output_fmt,
        );
    });
    let repo = ctx.issue_repository();
    let view = match repo.find_by_id(&id) {
        Ok(Some(issue)) => generic::View::Shown {
            date: issue.date.to_string(),
        },
        Ok(None) => generic::View::NotFound,
        Err(e) => die1(CliError::new(e.to_string()), output_fmt),
    };
    generic::render(view, id_str, &id.to_string(), NOUN, output_fmt);
}