Parse a `serde_json::Value` representing a Avro enum type into a `Schema`.
(&self, complex: &Map<String, Value>)
| 1191 | /// Parse a `serde_json::Value` representing a Avro enum type into a |
| 1192 | /// `Schema`. |
| 1193 | fn parse_enum(&self, complex: &Map<String, Value>) -> Result<SchemaPiece, AvroError> { |
| 1194 | let symbols: Vec<String> = complex |
| 1195 | .get("symbols") |
| 1196 | .and_then(|v| v.as_array()) |
| 1197 | .ok_or_else(|| ParseSchemaError::new("No `symbols` field in enum")) |
| 1198 | .and_then(|symbols| { |
| 1199 | symbols |
| 1200 | .iter() |
| 1201 | .map(|symbol| symbol.as_str().map(|s| s.to_string())) |
| 1202 | .collect::<Option<_>>() |
| 1203 | .ok_or_else(|| ParseSchemaError::new("Unable to parse `symbols` in enum")) |
| 1204 | })?; |
| 1205 | |
| 1206 | let mut unique_symbols: BTreeSet<&String> = BTreeSet::new(); |
| 1207 | for symbol in symbols.iter() { |
| 1208 | if unique_symbols.contains(symbol) { |
| 1209 | return Err(ParseSchemaError::new(format!( |
| 1210 | "Enum symbols must be unique, found multiple: {}", |
| 1211 | symbol |
| 1212 | )) |
| 1213 | .into()); |
| 1214 | } else { |
| 1215 | unique_symbols.insert(symbol); |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | let default_idx = if let Some(default) = complex.get("default") { |
| 1220 | let default_str = default.as_str().ok_or_else(|| { |
| 1221 | ParseSchemaError::new(format!( |
| 1222 | "Enum default should be a string, got: {:?}", |
| 1223 | default |
| 1224 | )) |
| 1225 | })?; |
| 1226 | let default_idx = symbols |
| 1227 | .iter() |
| 1228 | .position(|x| x == default_str) |
| 1229 | .ok_or_else(|| { |
| 1230 | ParseSchemaError::new(format!( |
| 1231 | "Enum default not found in list of symbols: {}", |
| 1232 | default_str |
| 1233 | )) |
| 1234 | })?; |
| 1235 | Some(default_idx) |
| 1236 | } else { |
| 1237 | None |
| 1238 | }; |
| 1239 | |
| 1240 | Ok(SchemaPiece::Enum { |
| 1241 | doc: complex.doc(), |
| 1242 | symbols, |
| 1243 | default_idx, |
| 1244 | }) |
| 1245 | } |
| 1246 | |
| 1247 | /// Parse a `serde_json::Value` representing a Avro array type into a |
| 1248 | /// `Schema`. |