aboutsummaryrefslogtreecommitdiff
path: root/mling/src/cli/read.rs
blob: e51e78fb099c5245b741a4e5b5085ae95d73abda (plain) (blame)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use colored::Colorize;
use std::path::PathBuf;

use mingling::{
    Groupped,
    macros::{chain, dispatcher, pack, r_println, renderer},
};
use serde::Serialize;

use crate::{
    Next,
    project_solver::{BinaryItem, solve_current_dir},
};

dispatcher!("show-target-dir", ReadTargetDirCommand => ReadTargetDirEntry);
dispatcher!("show-workspace-root", ReadWorkspaceRootCommand => ReadWorkspaceRootEntry);
dispatcher!("show-binaries", ReadBinariesCommand => ReadBinariesEntry);

pack!(ResultDir = PathBuf);
pack!(ResultTargetDirNotFound = ());

#[derive(Debug, Serialize, Default, Groupped)]
pub(crate) struct ResultBinaries {
    bin: Vec<BinaryItem>,
}

#[chain]
#[allow(unused_variables)]
pub(crate) fn handle_target_dir_entry(entry: ReadTargetDirEntry) -> Next {
    match solve_current_dir() {
        Ok(solved) => {
            let dir = solved.target_dir;
            ResultDir::new(dir).to_render()
        }
        Err(_) => ResultTargetDirNotFound::new(()).to_render(),
    }
}

#[chain]
#[allow(unused_variables)]
pub(crate) fn handle_workspace_root_entry(entry: ReadWorkspaceRootEntry) -> Next {
    match solve_current_dir() {
        Ok(solved) => {
            let dir = solved.workspace_root;
            ResultDir::new(dir).to_render()
        }
        Err(_) => ResultTargetDirNotFound::new(()).to_render(),
    }
}

#[chain]
#[allow(unused_variables)]
pub(crate) fn handle_binaries_entry(entry: ReadBinariesEntry) -> Next {
    match solve_current_dir() {
        Ok(solved) => {
            let binaries = solved.binaries;
            ResultBinaries { bin: binaries }.to_render()
        }
        Err(_) => ResultTargetDirNotFound::new(()).to_render(),
    }
}

#[renderer]
pub(crate) fn render_dir(prev: ResultDir) {
    r_println!("{}", prev.inner.display())
}

#[renderer]
pub(crate) fn render_binaries(prev: ResultBinaries) {
    for (i, item) in (1..).zip(prev.bin.iter()) {
        r_println!(
            "{}. {} ({})",
            i.to_string(),
            item.name.bold(),
            item.path.to_string_lossy().underline().bright_cyan()
        );
    }
}