From 43af6d371cfdad419f32e564aedf07f63e7cb428 Mon Sep 17 00:00:00 2001 From: Hugo Trentesaux <hugo@trentesaux.fr> Date: Thu, 18 Jan 2024 15:11:27 +0100 Subject: [PATCH] refac identity to display status --- src/commands/identity.rs | 69 ++++++++++++++++------------------------ src/commands/runtime.rs | 8 +++-- 2 files changed, 33 insertions(+), 44 deletions(-) diff --git a/src/commands/identity.rs b/src/commands/identity.rs index 52abae5..1448357 100644 --- a/src/commands/identity.rs +++ b/src/commands/identity.rs @@ -71,7 +71,6 @@ pub enum Subcommand { pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliError> { let mut data = data.build_client().await?; match command { - // TODO remove indexer where not necessary when BlakeConcat will be there Subcommand::Show => { data = data.build_indexer().await?; get_identity(&data, Some(data.address()), None, None).await? @@ -150,41 +149,30 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE /// get identity pub async fn get_identity( data: &Data, - mut account_id: Option<AccountId>, - mut identity_id: Option<IdtyId>, - mut username: Option<String>, + account_id: Option<AccountId>, + identity_id: Option<IdtyId>, + username: Option<String>, ) -> Result<(), anyhow::Error> { let client = data.client(); let indexer = data.indexer.clone(); // fetch reachable information using Duniter only (no indexer) - match (&account_id, identity_id, &username) { + let (idty, value) = match (&account_id, identity_id, &username) { // idty_id → account_id - (None, Some(identity_id), None) => { - account_id = get_identity_by_index(client, identity_id) - .await? - .map(|idty| idty.owner_key); - if account_id.is_none() { - return Err(anyhow!("no identity for this account id")); - } - } + (None, Some(idty), None) => (idty, get_identity_by_index(client, idty).await?), // account_id → idty_id (Some(account_id), None, None) => { - identity_id = get_idty_index_by_account_id(client, account_id).await?; - if identity_id.is_none() { - return Err(anyhow!("no identity for this identity index")); - } + let idty = get_idty_index_by_account_id(client, account_id) + .await? + .ok_or_else(|| anyhow!("no identity for this account id"))?; + (idty, get_identity_by_index(client, idty).await?) } // username → idty_id and account_id (None, None, Some(username)) => { - identity_id = get_idty_index_by_name(client, username).await?; - if let Some(identity_id) = identity_id { - account_id = get_identity_by_index(client, identity_id) - .await? - .map(|idty| idty.owner_key); - } else { - return Err(anyhow!("no identity found for this username")); - } + let idty = get_idty_index_by_name(client, username) + .await? + .ok_or_else(|| anyhow!("no identity found for this username"))?; + (idty, get_identity_by_index(client, idty).await?) } _ => { return Err(anyhow!( @@ -192,28 +180,25 @@ pub async fn get_identity( )); } }; + let value = value.ok_or_else(|| anyhow!("no identity value"))?; // print result // 1. identity index - println!( - "Identity index: {}", - identity_id.map_or(String::new(), |identity_id| format!("{identity_id}")) - ); + println!("Identity index: {idty}",); // 2. username (indexer needed if not provided) - if let (Some(indexer), Some(identity_id), None) = (&indexer, identity_id, &username) { - username = indexer.username_by_index(identity_id).await?; - } - println!( - "Username: {}", - username.unwrap_or("<no indexer>".to_string()) - ); + let username = username.unwrap_or(if let Some(indexer) = &indexer { + indexer + .username_by_index(idty) + .await? + .ok_or_else(|| anyhow!("indexer does not have username for this index"))? + } else { + "<no indexer>".to_string() + }); + println!("Username: {username}",); // 3. address - println!( - "Address: {}", - account_id - .as_ref() - .map_or(String::new(), AccountId::to_string) - ); + println!("Address: {}", AccountId::to_string(&value.owner_key)); + // 4. status + println!("Status: {:?}", value.status); Ok(()) } diff --git a/src/commands/runtime.rs b/src/commands/runtime.rs index 0d75ea8..ed3b96a 100644 --- a/src/commands/runtime.rs +++ b/src/commands/runtime.rs @@ -50,7 +50,11 @@ pub async fn runtime_info(data: Data) { ); println!( "min received cert to issue cert: {}", - getu32(consts.certification().min_received_cert_to_be_able_to_issue_cert()) + getu32( + consts + .certification() + .min_received_cert_to_be_able_to_issue_cert() + ) ); println!( "certification validity: {} blocks", @@ -153,4 +157,4 @@ pub async fn runtime_info(data: Data) { ); // todo treasury, technical committee, transaction payment, authority members // consts.system().ss58_prefix() -} \ No newline at end of file +} -- GitLab