1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use cod_cli::milestone::create::CreateMilestoneArgs;
use cod_client::CodebergClient;
use cod_endpoints::endpoint_generator::EndpointGenerator;
use cod_types::api::create_options::create_milestone_option::CreateMilestoneOption;
use cod_types::api::milestone::Milestone;
use strum::Display;

use crate::text_manipulation::{edit_prompt_for, input_prompt_for};

pub async fn create_milestone(
    args: CreateMilestoneArgs,
    client: &CodebergClient,
) -> anyhow::Result<()> {
    let options = fill_in_mandatory_values(&args)?;
    let options = fill_in_optional_values(options, args)?;
    let api_endpoint = EndpointGenerator::repo_milestones()?;
    let response: Milestone = client.post_body(api_endpoint, options).await?;
    tracing::debug!("{response:?}");
    Ok(())
}

fn fill_in_mandatory_values(args: &CreateMilestoneArgs) -> anyhow::Result<CreateMilestoneOption> {
    let title = match args.title.clone() {
        Some(title) => title,
        None => inquire::Text::new(input_prompt_for("Milestone Title").as_str()).prompt()?,
    };
    Ok(CreateMilestoneOption::new(title))
}

fn fill_in_optional_values(
    mut options: CreateMilestoneOption,
    args: CreateMilestoneArgs,
) -> anyhow::Result<CreateMilestoneOption> {
    options = options.with_description(args.body.clone().unwrap_or_default());

    #[derive(Debug, Clone, Copy, Display, PartialEq, Eq)]
    enum PossiblyMissing {
        Description,
    }

    use PossiblyMissing::*;

    let missing_options = [args.body.is_none().then_some(Description)]
        .into_iter()
        .flatten()
        .collect::<Vec<_>>();

    if missing_options.is_empty() {
        return Ok(options);
    }

    if missing_options.contains(&Description) {
        let new_description =
            inquire::Editor::new(edit_prompt_for("a milestone description").as_str()).prompt()?;
        options = options.with_description(new_description);
    }

    Ok(options)
}