Output identities as JSON.
(
&self,
identities: &[atomic_identity::Identity],
default_id: Option<&atomic_identity::IdentityId>,
)
| 181 | |
| 182 | /// Output identities as JSON. |
| 183 | fn output_json( |
| 184 | &self, |
| 185 | identities: &[atomic_identity::Identity], |
| 186 | default_id: Option<&atomic_identity::IdentityId>, |
| 187 | ) { |
| 188 | use serde_json::json; |
| 189 | |
| 190 | let json_identities: Vec<_> = identities |
| 191 | .iter() |
| 192 | .map(|identity| { |
| 193 | let is_default = default_id.map(|d| d == &identity.id).unwrap_or(false); |
| 194 | |
| 195 | let mut obj = json!({ |
| 196 | "name": identity.name, |
| 197 | "id": identity.id.to_base32(), |
| 198 | "type": super::format_identity_type(&identity.identity_type), |
| 199 | "usage": super::format_usage(&identity.usage), |
| 200 | "public_key": identity.public_key_base32(), |
| 201 | "is_default": is_default, |
| 202 | }); |
| 203 | |
| 204 | if let Some(email) = &identity.email { |
| 205 | obj["email"] = json!(email); |
| 206 | } |
| 207 | |
| 208 | if let Some(desc) = &identity.metadata.description { |
| 209 | obj["description"] = json!(desc); |
| 210 | } |
| 211 | |
| 212 | obj |
| 213 | }) |
| 214 | .collect(); |
| 215 | |
| 216 | let output = json!({ |
| 217 | "identities": json_identities, |
| 218 | "count": identities.len(), |
| 219 | }); |
| 220 | |
| 221 | println!("{}", serde_json::to_string_pretty(&output).unwrap()); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | // Tests |