github_actions_autodocs/
models.rs

1use std::{collections::HashMap, path::Path};
2
3use serde::{self, Deserialize};
4
5#[derive(Deserialize, Debug, Clone)]
6pub struct Output {
7    pub description: String,
8    pub value: Option<String>,
9}
10
11#[derive(Deserialize, Debug, Clone)]
12pub struct Input {
13    pub description: String,
14    pub default: Option<String>,
15    pub required: Option<bool>,
16
17    #[serde(alias = "deprecationMessage")]
18    pub deprecation_message: Option<String>,
19}
20
21#[derive(Deserialize, Debug, Clone)]
22pub struct Action {
23    pub name: Option<String>,
24    pub description: Option<String>,
25
26    pub outputs: Option<Outputs>,
27    pub inputs: Option<Inputs>,
28}
29
30#[derive(Deserialize, Debug, Clone)]
31pub struct Outputs(pub HashMap<String, Output>);
32
33#[derive(Deserialize, Debug, Clone)]
34pub struct Inputs(pub HashMap<String, Input>);
35
36pub trait SortedKeys {
37    fn sorted_keys(&self) -> Vec<&str>;
38}
39
40impl SortedKeys for Inputs {
41    fn sorted_keys(&self) -> Vec<&str> {
42        let mut sorted: Vec<&str> = self.0.keys().map(|k| k.as_str()).collect();
43        sorted.sort();
44
45        sorted
46    }
47}
48
49impl SortedKeys for Outputs {
50    fn sorted_keys(&self) -> Vec<&str> {
51        let mut sorted: Vec<&str> = self.0.keys().map(|k| k.as_str()).collect();
52        sorted.sort();
53
54        sorted
55    }
56}
57
58impl Action {
59    pub fn read_from_file(path: &Path) -> Result<Self, Box<dyn std::error::Error>> {
60        let file = std::fs::File::open(path)?;
61        let reader = std::io::BufReader::new(file);
62        let action: Action = serde_yaml::from_reader(reader)?;
63
64        Ok(action)
65    }
66}
67
68impl Default for Input {
69    fn default() -> Self {
70        Self {
71            description: String::new(),
72            default: None,
73            required: Some(false),
74            deprecation_message: None,
75        }
76    }
77}
78
79impl Input {
80    pub fn to_markdown(&self, name: &str) -> String {
81        let def = self.default.clone().unwrap_or(String::from("`nd`"));
82        let is_required = self.required.unwrap_or(false);
83
84        let required_as_str = is_required.to_string();
85
86        format!(
87            "| `{}` | {} | {} | `{}` |",
88            name, self.description, def, required_as_str
89        )
90    }
91}
92
93impl Output {
94    pub fn to_markdown(&self, name: &str) -> String {
95        format!("| `{}` | {} |", name, self.description)
96    }
97}