MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / parse_response

Function parse_response

atomic-repository/src/change_source/watchman.rs:135–177  ·  view source on GitHub ↗
(
    bytes: &[u8],
    had_previous: bool,
    limit: usize,
)

Source from the content-addressed store, hash-verified

133}
134
135fn 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
179fn unavailable(operation: &str, output: &CommandOutput) -> ChangeSourceError {
180 let detail = String::from_utf8_lossy(&output.stderr);

Callers 1

changesMethod · 0.85

Calls 5

getMethod · 0.65
lenMethod · 0.45
iterMethod · 0.45
as_strMethod · 0.45
as_bytesMethod · 0.45

Tested by

no test coverage detected