wini_cli/just/
args_from_file.rs

1use {
2    crate::just::{MinimalJustfile, MinimalParam, MinimalRecipe},
3    pub_just::{Compiler, Loader},
4    std::{collections::HashMap, ffi::OsString, path::Path},
5};
6
7pub fn arguments_from_justfile_path(path: &Path) -> Result<MinimalJustfile, ()> {
8    let loader = Loader::new();
9
10    match Compiler::compile(&loader, path) {
11        Ok(compilation_res) => {
12            let justfile = compilation_res.justfile;
13
14            let mut aliases: HashMap<String, Vec<String>> = HashMap::new();
15
16            for (alias, to) in justfile.aliases {
17                aliases
18                    .entry(to.target.name.to_string())
19                    .or_default()
20                    .push(alias.to_owned())
21            }
22
23            let recipes = justfile
24                .recipes
25                .into_iter()
26                .map(|(name, recipe)| {
27                    let name = name.to_string();
28                    MinimalRecipe {
29                        doc: recipe.doc.clone(),
30                        aliases: aliases.remove(&name),
31                        name,
32                        params: recipe
33                            .parameters
34                            .iter()
35                            .map(|p| {
36                                MinimalParam {
37                                    name: p.name.to_string(),
38                                    kind: p.kind,
39                                }
40                            })
41                            .collect(),
42                    }
43                })
44                .collect();
45
46
47            Ok(MinimalJustfile { recipes })
48        },
49        Err(_) => {
50            eprintln!("You have an error in your justfile:");
51            let _ = pub_just::run(vec![OsString::new()].into_iter());
52
53            Err(())
54        },
55    }
56}