1use std::{collections::HashMap, sync::Arc};
5
6use serde::{Deserialize, Serialize};
7
8use crate::{CmdShape, ContextFeature, EnvShape};
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct PackageSpec {
13 pub name: String,
15 pub spec: CommandSpecRepr,
17}
18
19#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct PackagesVec(pub Vec<PackageSpec>);
24
25#[derive(Debug, Clone, Serialize, Deserialize)]
30#[serde(tag = "kind")]
31pub enum CommandSpecItem {
32 #[serde(rename = "cmd")]
34 Cmd(CmdShape),
35 #[serde(rename = "env")]
37 Env(EnvShape),
38 #[serde(rename = "sym")]
41 Symbol,
42 #[serde(rename = "cmd0")]
44 Command0,
45 #[serde(rename = "cmd1")]
47 Command1,
48 #[serde(rename = "cmd2")]
50 Command2,
51 #[serde(rename = "left1-cmd")]
53 CmdLeft1,
54 #[serde(rename = "matrix-env")]
56 EnvMatrix,
57 #[serde(rename = "normal-env")]
59 EnvNormal,
60 #[serde(rename = "glob-env")]
62 EnvGlob {
63 pattern: String,
65 alias: String,
67 ctx_feature: ContextFeature,
69 },
70
71 #[serde(rename = "alias-sym")]
73 SymAlias {
74 alias: String,
76 },
77 #[serde(rename = "greedy-cmd")]
79 CmdGreedy {
80 alias: String,
82 },
83 #[serde(rename = "infix-cmd")]
85 CmdInfix {
86 alias: String,
88 },
89 #[serde(rename = "glob-cmd")]
92 CmdGlob {
93 pattern: String,
95 alias: String,
97 },
98}
99
100impl From<CommandSpecItem> for crate::CommandSpecItem {
101 fn from(item: CommandSpecItem) -> Self {
102 use crate::preludes::command::*;
103 match item {
104 CommandSpecItem::Cmd(shape) => Self::Cmd(shape),
105 CommandSpecItem::Env(shape) => Self::Env(shape),
106 CommandSpecItem::Symbol => TEX_SYMBOL,
107 CommandSpecItem::Command0 => TEX_CMD0,
108 CommandSpecItem::Command1 => TEX_CMD1,
109 CommandSpecItem::Command2 => TEX_CMD2,
110 CommandSpecItem::CmdLeft1 => TEX_LEFT1_OPEARTOR,
111 CommandSpecItem::EnvMatrix => TEX_MATRIX_ENV,
112 CommandSpecItem::EnvNormal => TEX_NORMAL_ENV,
113 CommandSpecItem::EnvGlob {
114 pattern,
115 alias,
116 ctx_feature,
117 } => define_glob_env(&pattern, &alias, ctx_feature),
118 CommandSpecItem::SymAlias { alias } => define_symbol(&alias),
119 CommandSpecItem::CmdGreedy { alias } => define_greedy_command(&alias),
120 CommandSpecItem::CmdInfix { alias } => crate::CommandSpecItem::Cmd(crate::CmdShape {
121 args: crate::ArgShape::InfixGreedy,
122 alias: Some(alias.to_owned()),
123 }),
124 CommandSpecItem::CmdGlob { pattern, alias } => define_glob_command(&pattern, &alias),
125 }
126 }
127}
128
129#[derive(Default, Debug, Clone, Serialize, Deserialize)]
137pub struct CommandSpecRepr {
138 pub commands: HashMap<String, CommandSpecItem>,
140}
141
142impl From<CommandSpecRepr> for crate::CommandSpec {
143 fn from(repr: CommandSpecRepr) -> Self {
144 Self(Arc::new(repr.into()))
145 }
146}
147
148impl From<CommandSpecRepr> for crate::CommandSpecRepr {
149 fn from(repr: CommandSpecRepr) -> Self {
150 Self {
151 commands: repr
152 .commands
153 .into_iter()
154 .map(|(k, v)| (k, v.into()))
155 .collect(),
156 }
157 }
158}