|
2 | 2 |
|
3 | 3 | //! Implements deserialization of the `Entry` struct. |
4 | 4 |
|
| 5 | +use serde::de::{self, Deserialize, Deserializer, MapAccess, Visitor}; |
5 | 6 | use std::fmt; |
6 | 7 | use std::path; |
7 | 8 |
|
8 | | -use serde::de::{self, Deserialize, Deserializer, MapAccess, Visitor}; |
9 | | - |
10 | 9 | use super::Entry; |
11 | 10 |
|
12 | 11 | impl<'de> Deserialize<'de> for Entry { |
13 | 12 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
14 | 13 | where |
15 | 14 | D: Deserializer<'de>, |
16 | 15 | { |
17 | | - enum Field { |
18 | | - Directory, |
19 | | - File, |
20 | | - Command, |
21 | | - Arguments, |
22 | | - Output, |
23 | | - } |
24 | | - const FIELDS: &[&str] = &["directory", "file", "command", "arguments", "output"]; |
25 | | - |
26 | | - impl<'de> Deserialize<'de> for Field { |
27 | | - fn deserialize<D>(deserializer: D) -> Result<Field, D::Error> |
28 | | - where |
29 | | - D: Deserializer<'de>, |
30 | | - { |
31 | | - struct FieldVisitor; |
32 | | - |
33 | | - impl Visitor<'_> for FieldVisitor { |
34 | | - type Value = Field; |
35 | | - |
36 | | - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
37 | | - formatter |
38 | | - .write_str("`directory`, `file`, `command`, `arguments`, or `output`") |
39 | | - } |
40 | | - |
41 | | - fn visit_str<E>(self, value: &str) -> Result<Field, E> |
42 | | - where |
43 | | - E: de::Error, |
44 | | - { |
45 | | - match value { |
46 | | - "directory" => Ok(Field::Directory), |
47 | | - "file" => Ok(Field::File), |
48 | | - "command" => Ok(Field::Command), |
49 | | - "arguments" => Ok(Field::Arguments), |
50 | | - "output" => Ok(Field::Output), |
51 | | - _ => Err(de::Error::unknown_field(value, FIELDS)), |
52 | | - } |
53 | | - } |
54 | | - } |
55 | | - |
56 | | - deserializer.deserialize_identifier(FieldVisitor) |
57 | | - } |
| 16 | + deserializer.deserialize_struct("Entry", FIELDS, EntryVisitor) |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +enum Field { |
| 21 | + Directory, |
| 22 | + File, |
| 23 | + Command, |
| 24 | + Arguments, |
| 25 | + Output, |
| 26 | +} |
| 27 | + |
| 28 | +const FIELDS: &[&str] = &["directory", "file", "command", "arguments", "output"]; |
| 29 | + |
| 30 | +impl<'de> Deserialize<'de> for Field { |
| 31 | + fn deserialize<D>(deserializer: D) -> Result<Field, D::Error> |
| 32 | + where |
| 33 | + D: Deserializer<'de>, |
| 34 | + { |
| 35 | + deserializer.deserialize_identifier(FieldVisitor) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +struct FieldVisitor; |
| 40 | + |
| 41 | +impl Visitor<'_> for FieldVisitor { |
| 42 | + type Value = Field; |
| 43 | + |
| 44 | + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 45 | + write!(formatter, "one of {:?}", FIELDS) |
| 46 | + } |
| 47 | + |
| 48 | + fn visit_str<E>(self, value: &str) -> Result<Field, E> |
| 49 | + where |
| 50 | + E: de::Error, |
| 51 | + { |
| 52 | + match value { |
| 53 | + "directory" => Ok(Field::Directory), |
| 54 | + "file" => Ok(Field::File), |
| 55 | + "command" => Ok(Field::Command), |
| 56 | + "arguments" => Ok(Field::Arguments), |
| 57 | + "output" => Ok(Field::Output), |
| 58 | + _ => Err(de::Error::unknown_field(value, FIELDS)), |
58 | 59 | } |
| 60 | + } |
| 61 | +} |
59 | 62 |
|
60 | | - struct EntryVisitor; |
| 63 | +struct EntryVisitor; |
61 | 64 |
|
62 | | - impl<'de> Visitor<'de> for EntryVisitor { |
63 | | - type Value = Entry; |
| 65 | +impl<'de> Visitor<'de> for EntryVisitor { |
| 66 | + type Value = Entry; |
64 | 67 |
|
65 | | - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
66 | | - formatter.write_str("struct Entry") |
67 | | - } |
| 68 | + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 69 | + formatter.write_str("object Entry") |
| 70 | + } |
68 | 71 |
|
69 | | - fn visit_map<V>(self, mut map: V) -> Result<Entry, V::Error> |
70 | | - where |
71 | | - V: MapAccess<'de>, |
72 | | - { |
73 | | - let mut directory: Option<path::PathBuf> = None; |
74 | | - let mut file: Option<path::PathBuf> = None; |
75 | | - let mut command: Option<String> = None; |
76 | | - let mut arguments: Option<Vec<String>> = None; |
77 | | - let mut output: Option<path::PathBuf> = None; |
78 | | - |
79 | | - while let Some(key) = map.next_key()? { |
80 | | - match key { |
81 | | - Field::Directory => { |
82 | | - if directory.is_some() { |
83 | | - return Err(de::Error::duplicate_field("directory")); |
84 | | - } |
85 | | - directory = Some(map.next_value()?); |
86 | | - } |
87 | | - Field::File => { |
88 | | - if file.is_some() { |
89 | | - return Err(de::Error::duplicate_field("file")); |
90 | | - } |
91 | | - file = Some(map.next_value()?); |
92 | | - } |
93 | | - Field::Command => { |
94 | | - if command.is_some() { |
95 | | - return Err(de::Error::duplicate_field("command")); |
96 | | - } |
97 | | - command = Some(map.next_value()?); |
98 | | - } |
99 | | - Field::Arguments => { |
100 | | - if arguments.is_some() { |
101 | | - return Err(de::Error::duplicate_field("arguments")); |
102 | | - } |
103 | | - arguments = Some(map.next_value()?); |
104 | | - } |
105 | | - Field::Output => { |
106 | | - if output.is_some() { |
107 | | - return Err(de::Error::duplicate_field("output")); |
108 | | - } |
109 | | - output = Some(map.next_value()?); |
110 | | - } |
111 | | - } |
112 | | - } |
113 | | - let directory = directory.ok_or_else(|| de::Error::missing_field("directory"))?; |
114 | | - let file = file.ok_or_else(|| de::Error::missing_field("file"))?; |
115 | | - if arguments.is_some() && command.is_some() { |
116 | | - return Err(de::Error::custom( |
117 | | - "Either `command` or `arguments` field need to be specified, but not both.", |
118 | | - )); |
119 | | - } |
120 | | - let arguments = arguments.map_or_else( |
121 | | - || { |
122 | | - command |
123 | | - .ok_or_else(|| de::Error::missing_field("`command` or `arguments`")) |
124 | | - .and_then(|cmd| { |
125 | | - shell_words::split(cmd.as_str()).map_err(|_| { |
126 | | - de::Error::invalid_value( |
127 | | - de::Unexpected::Str(cmd.as_str()), |
128 | | - &"quotes needs to be matched", |
129 | | - ) |
130 | | - }) |
131 | | - }) |
132 | | - }, |
133 | | - Ok, |
134 | | - )?; |
135 | | - Ok(Entry { |
136 | | - directory, |
137 | | - file, |
138 | | - arguments, |
139 | | - output, |
140 | | - }) |
| 72 | + fn visit_map<V>(self, mut map: V) -> Result<Entry, V::Error> |
| 73 | + where |
| 74 | + V: MapAccess<'de>, |
| 75 | + { |
| 76 | + let mut directory_opt: Option<path::PathBuf> = None; |
| 77 | + let mut file_opt: Option<path::PathBuf> = None; |
| 78 | + let mut command_opt: Option<String> = None; |
| 79 | + let mut arguments_opt: Option<Vec<String>> = None; |
| 80 | + let mut output: Option<path::PathBuf> = None; |
| 81 | + |
| 82 | + while let Some(key) = map.next_key()? { |
| 83 | + match key { |
| 84 | + Field::Directory => directory_opt = Some(map.next_value()?), |
| 85 | + Field::File => file_opt = Some(map.next_value()?), |
| 86 | + Field::Command => command_opt = Some(map.next_value()?), |
| 87 | + Field::Arguments => arguments_opt = Some(map.next_value()?), |
| 88 | + Field::Output => output = Some(map.next_value()?), |
141 | 89 | } |
142 | 90 | } |
143 | 91 |
|
144 | | - deserializer.deserialize_struct("Entry", FIELDS, EntryVisitor) |
| 92 | + // Validate if the mandatory fields are present. |
| 93 | + let arguments = match (arguments_opt, command_opt) { |
| 94 | + (None, None) => Err(de::Error::missing_field("`command` or `arguments`")), |
| 95 | + (Some(_), Some(_)) => Err(de::Error::custom( |
| 96 | + "Either `command` or `arguments` field need to be specified, but not both.", |
| 97 | + )), |
| 98 | + (Some(args), None) => Ok(args), |
| 99 | + (None, Some(cmd)) => shell_words::split(cmd.as_str()).map_err(|_| { |
| 100 | + de::Error::invalid_value( |
| 101 | + de::Unexpected::Str(cmd.as_str()), |
| 102 | + &"valid shell command with proper escaping", |
| 103 | + ) |
| 104 | + }), |
| 105 | + }?; |
| 106 | + |
| 107 | + Ok(Entry { |
| 108 | + directory: directory_opt.ok_or_else(|| de::Error::missing_field("directory"))?, |
| 109 | + file: file_opt.ok_or_else(|| de::Error::missing_field("file"))?, |
| 110 | + arguments, |
| 111 | + output, |
| 112 | + }) |
145 | 113 | } |
146 | 114 | } |
0 commit comments