|
| 1 | +use core::cmp::Ordering; |
| 2 | +use std::collections::HashMap; |
| 3 | + |
| 4 | +#[derive(Debug)] |
| 5 | +pub struct Document { |
| 6 | + /// Name of the action |
| 7 | + action_name: String, |
| 8 | + /// The time complexity of the action |
| 9 | + complexity: String, |
| 10 | + /// The syntax for the action |
| 11 | + syntax: Vec<String>, |
| 12 | + /// Return(s) with example(s) |
| 13 | + accept_ty: Vec<String>, |
| 14 | + /// the return type for the action |
| 15 | + return_ty: Vec<String>, |
| 16 | + /// longform description action |
| 17 | + description: String, |
| 18 | +} |
| 19 | + |
| 20 | +impl Document { |
| 21 | + pub const fn new( |
| 22 | + action_name: String, |
| 23 | + complexity: String, |
| 24 | + accept_ty: Vec<String>, |
| 25 | + return_ty: Vec<String>, |
| 26 | + description: String, |
| 27 | + syntax: Vec<String>, |
| 28 | + ) -> Self { |
| 29 | + Self { |
| 30 | + action_name, |
| 31 | + complexity, |
| 32 | + syntax, |
| 33 | + accept_ty, |
| 34 | + return_ty, |
| 35 | + description, |
| 36 | + } |
| 37 | + } |
| 38 | + /// Returns (path, contents of markdown file) |
| 39 | + pub fn into_md(self, linklist: &HashMap<&'static str, &'static str>) -> (String, String) { |
| 40 | + let Self { |
| 41 | + action_name, |
| 42 | + complexity, |
| 43 | + syntax, |
| 44 | + accept_ty, |
| 45 | + return_ty, |
| 46 | + description, |
| 47 | + } = self; |
| 48 | + let path = format!("docs/actions/{}.md", action_name); |
| 49 | + let syntax_rendered = render_list(syntax); |
| 50 | + let returns_rendered = render_link_list(return_ty, linklist); |
| 51 | + let accept_rendered = render_link_list(accept_ty, linklist); |
| 52 | + let body = format!( |
| 53 | + "\ |
| 54 | +--- |
| 55 | +id: {action_name} |
| 56 | +title: {title} |
| 57 | +--- |
| 58 | +
|
| 59 | +:::note About |
| 60 | +**Time complexity**: {complexity} |
| 61 | +**Accept type**: |
| 62 | +
|
| 63 | +{accept_ty} |
| 64 | +**Return type**: |
| 65 | +
|
| 66 | +{return_ty} |
| 67 | +**Syntax**: |
| 68 | +
|
| 69 | +{syntax} |
| 70 | +::: |
| 71 | +
|
| 72 | +{description} |
| 73 | +", |
| 74 | + action_name = action_name.to_lowercase(), |
| 75 | + title = action_name, |
| 76 | + complexity = complexity, |
| 77 | + accept_ty = accept_rendered, |
| 78 | + return_ty = returns_rendered, |
| 79 | + syntax = syntax_rendered, |
| 80 | + description = description |
| 81 | + ) |
| 82 | + .to_string(); |
| 83 | + (path, body) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +fn render_list(inp: Vec<String>) -> String { |
| 88 | + inp.into_iter() |
| 89 | + .map(|v| format!("- {}\n", v).chars().collect::<Vec<_>>()) |
| 90 | + .flatten() |
| 91 | + .collect() |
| 92 | +} |
| 93 | + |
| 94 | +fn render_link_list(inp: Vec<String>, linklist: &HashMap<&'static str, &'static str>) -> String { |
| 95 | + inp.into_iter() |
| 96 | + .map(|v| { |
| 97 | + println!("looking for: {}", v); |
| 98 | + format!("- [{}]({})\n", v, linklist.get(v.as_str()).unwrap()) |
| 99 | + .chars() |
| 100 | + .collect::<Vec<_>>() |
| 101 | + }) |
| 102 | + .flatten() |
| 103 | + .collect() |
| 104 | +} |
| 105 | + |
| 106 | +impl PartialEq for Document { |
| 107 | + fn eq(&self, other: &Self) -> bool { |
| 108 | + self.action_name == other.action_name |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +impl Eq for Document {} |
| 113 | + |
| 114 | +impl PartialOrd for Document { |
| 115 | + fn partial_cmp(&self, other: &Document) -> std::option::Option<std::cmp::Ordering> { |
| 116 | + self.action_name.partial_cmp(&other.action_name) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +impl Ord for Document { |
| 121 | + fn cmp(&self, other: &Self) -> Ordering { |
| 122 | + self.action_name.cmp(&other.action_name) |
| 123 | + } |
| 124 | +} |
0 commit comments