1use crate::{Error, Result};
4
5#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct PluginOptions {
8 name: String,
9 arguments: Vec<String>,
10}
11
12impl PluginOptions {
13 pub fn new(
15 name: impl Into<String>,
16 arguments: impl IntoIterator<Item = impl Into<String>>,
17 ) -> Self {
18 Self {
19 name: name.into(),
20 arguments: arguments.into_iter().map(Into::into).collect(),
21 }
22 }
23
24 pub fn without_arguments(name: impl Into<String>) -> Self {
26 Self::new(name, std::iter::empty::<String>())
27 }
28
29 pub(crate) fn append_to(&self, values: &mut Vec<String>) {
30 values.push(self.name.clone());
31 values.extend(self.arguments.iter().cloned());
32 }
33
34 pub(crate) fn validate_not_memory(&self, field: &'static str) -> Result<()> {
35 if self.name.eq_ignore_ascii_case("memory") {
36 return Err(Error::InvalidArgument {
37 field,
38 message: "use the typed memory endpoint variant for the memory plugin".to_owned(),
39 });
40 }
41 Ok(())
42 }
43}
44
45#[derive(Debug, Clone, Default, PartialEq, Eq)]
47pub struct PluginChain(Vec<PluginOptions>);
48
49impl PluginChain {
50 pub fn new() -> Self {
52 Self::default()
53 }
54
55 pub fn push(&mut self, plugin: PluginOptions) {
57 self.0.push(plugin);
58 }
59
60 pub fn iter(&self) -> impl ExactSizeIterator<Item = &PluginOptions> {
62 self.0.iter()
63 }
64}
65
66impl From<Vec<PluginOptions>> for PluginChain {
67 fn from(value: Vec<PluginOptions>) -> Self {
68 Self(value)
69 }
70}
71
72pub(crate) fn processor_plugins(
73 application: &str,
74 input: Option<&PluginOptions>,
75 processors: &PluginChain,
76 output: Option<&PluginOptions>,
77) -> Vec<u8> {
78 let mut values = vec![application.to_owned()];
79 if let Some(input) = input {
80 values.push("-I".to_owned());
81 input.append_to(&mut values);
82 }
83 for plugin in processors.iter() {
84 values.push("-P".to_owned());
85 plugin.append_to(&mut values);
86 }
87 if let Some(output) = output {
88 values.push("-O".to_owned());
89 output.append_to(&mut values);
90 }
91 encode_multistring(&values)
92}
93
94pub(crate) fn encode_multistring(values: &[String]) -> Vec<u8> {
95 let mut units = Vec::new();
96 for (index, value) in values.iter().enumerate() {
97 if index != 0 {
98 units.push(0xffff);
99 }
100 units.push(0xfeff);
101 units.extend(value.encode_utf16());
102 }
103 units.into_iter().flat_map(u16::to_ne_bytes).collect()
104}
105
106#[cfg(test)]
107mod tests {
108 use super::*;
109
110 #[test]
111 fn serializer_preserves_plugin_order() {
112 let bytes = processor_plugins(
113 "rustduck",
114 Some(&PluginOptions::new("file", ["input.ts"])),
115 &PluginChain::from(vec![PluginOptions::new("filter", ["--service", "1"])]),
116 Some(&PluginOptions::without_arguments("drop")),
117 );
118 let units = bytes
119 .chunks_exact(2)
120 .map(|bytes| u16::from_ne_bytes([bytes[0], bytes[1]]))
121 .collect::<Vec<_>>();
122 assert_eq!(units[0], 0xfeff);
123 assert!(units.contains(&0xffff));
124 assert!(units.iter().filter(|&&unit| unit == 0xfeff).count() > 1);
125 }
126}