| 133 | } |
| 134 | |
| 135 | fn parse_response( |
| 136 | bytes: &[u8], |
| 137 | had_previous: bool, |
| 138 | limit: usize, |
| 139 | ) -> Result<(String, Vec<Vec<u8>>), ChangeSourceError> { |
| 140 | let response: Value = serde_json::from_slice(bytes) |
| 141 | .map_err(|error| ChangeSourceError::MalformedResponse(error.to_string()))?; |
| 142 | if let Some(error) = response.get("error").and_then(Value::as_str) { |
| 143 | return Err(ChangeSourceError::Unavailable(error.to_owned())); |
| 144 | } |
| 145 | if had_previous |
| 146 | && response |
| 147 | .get("is_fresh_instance") |
| 148 | .and_then(Value::as_bool) |
| 149 | .unwrap_or(false) |
| 150 | { |
| 151 | return Err(ChangeSourceError::UnknownToken); |
| 152 | } |
| 153 | let clock = response |
| 154 | .get("clock") |
| 155 | .and_then(Value::as_str) |
| 156 | .ok_or_else(|| ChangeSourceError::MalformedResponse("missing Watchman clock".into()))?; |
| 157 | let files = response |
| 158 | .get("files") |
| 159 | .and_then(Value::as_array) |
| 160 | .ok_or_else(|| ChangeSourceError::MalformedResponse("missing Watchman files".into()))?; |
| 161 | if files.len() > limit { |
| 162 | return Err(ChangeSourceError::Overflow { limit }); |
| 163 | } |
| 164 | let paths = files |
| 165 | .iter() |
| 166 | .map(|file| { |
| 167 | file.as_str() |
| 168 | .map(|path| path.as_bytes().to_vec()) |
| 169 | .ok_or_else(|| { |
| 170 | ChangeSourceError::MalformedResponse( |
| 171 | "Watchman file entry is not a string".into(), |
| 172 | ) |
| 173 | }) |
| 174 | }) |
| 175 | .collect::<Result<Vec<_>, _>>()?; |
| 176 | Ok((clock.to_owned(), paths)) |
| 177 | } |
| 178 | |
| 179 | fn unavailable(operation: &str, output: &CommandOutput) -> ChangeSourceError { |
| 180 | let detail = String::from_utf8_lossy(&output.stderr); |