diff --git a/src/commands.rs b/src/commands.rs index f734346eb74f8003d86a83fd61fa185096200bb2..f59775c0e73f5a5af79bdbb6b26590df2f137b6a 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -13,6 +13,10 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see <https://www.gnu.org/licenses/>. +pub mod balance; +pub mod current_ud; +pub mod members_count; + use crate::*; #[derive(StructOpt)] @@ -28,162 +32,3 @@ pub(crate) enum Command { /// Get current number of WoT members MembersCount, } - -pub(crate) fn balance<W: Write>( - client: &Client, - out: &mut W, - pubkey_or_script: &str, - ud_unit: bool, -) -> anyhow::Result<()> { - let request_body = Balance::build_query(balance::Variables { - script: pubkey_or_script.to_owned(), - with_ud: ud_unit, - }); - - let balance::ResponseData { - balance: balance::BalanceBalance { amount }, - current_ud: current_ud_opt, - } = client.send_gql_query(&request_body)?; - - if let Some(balance::BalanceCurrentUd { amount: ud_amount }) = current_ud_opt { - writeln!( - out, - "The balance of account '{}' is {:.2} UDĞ1 !", - pubkey_or_script, - amount as f64 / ud_amount as f64, - )?; - } else { - writeln!( - out, - "The balance of account '{}' is {}.{:02} Ğ1 !", - pubkey_or_script, - amount / 100, - amount % 100 - )?; - } - Ok(()) -} - -pub(crate) fn current_ud<W: Write>(client: &Client, out: &mut W) -> anyhow::Result<()> { - let request_body = CurrentUd::build_query(current_ud::Variables); - - if let current_ud::ResponseData { - current_ud: Some(current_ud::CurrentUdCurrentUd { amount }), - } = client.send_gql_query(&request_body)? - { - let int_part = amount / 100; - let dec_part = amount % 100; - writeln!( - out, - "The current UD value is {}.{:02} Ğ1 !", - int_part, dec_part - )?; - } else { - writeln!(out, "server with empty blockchain")?; - } - - Ok(()) -} - -pub(crate) fn members_count<W: Write>(client: &Client, out: &mut W) -> anyhow::Result<()> { - let request_body = MembersCount::build_query(members_count::Variables); - - let members_count::ResponseData { - current_block: members_count::MembersCountCurrentBlock { members_count }, - } = client.send_gql_query(&request_body)?; - - writeln!( - out, - "There is currently {} members in Ğ1 WoT!", - members_count - )?; - - Ok(()) -} - -// below are tests - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_balance() -> anyhow::Result<()> { - let mut client = Client::default(); - client - .expect_send_gql_query::<graphql_client::QueryBody<balance::Variables>, _>() - .returning(|_| { - Ok(balance::ResponseData { - balance: balance::BalanceBalance { amount: 2_046 }, - current_ud: None, - }) - }); - let mut out = Vec::new(); - balance(&client, &mut out, "toto", false)?; - let output = std::str::from_utf8(&out)?; - - assert_eq!(output, "The balance of account 'toto' is 20.46 Ğ1 !\n"); - - Ok(()) - } - - #[test] - fn test_balance_with_ud_unit() -> anyhow::Result<()> { - let mut client = Client::default(); - client - .expect_send_gql_query::<graphql_client::QueryBody<balance::Variables>, _>() - .returning(|_| { - Ok(balance::ResponseData { - balance: balance::BalanceBalance { amount: 2_046 }, - current_ud: Some(balance::BalanceCurrentUd { amount: 1_023 }), - }) - }); - let mut out = Vec::new(); - balance(&client, &mut out, "toto", true)?; - let output = std::str::from_utf8(&out)?; - - assert_eq!(output, "The balance of account 'toto' is 2.00 UDĞ1 !\n"); - - Ok(()) - } - - #[test] - fn test_current_ud() -> anyhow::Result<()> { - let mut client = Client::default(); - client - .expect_send_gql_query::<graphql_client::QueryBody<current_ud::Variables>, _>() - .returning(|_| { - Ok(current_ud::ResponseData { - current_ud: Some(current_ud::CurrentUdCurrentUd { amount: 1_023 }), - }) - }); - let mut out = Vec::new(); - current_ud(&client, &mut out)?; - let output = std::str::from_utf8(&out)?; - - assert_eq!(output, "The current UD value is 10.23 Ğ1 !\n"); - - Ok(()) - } - - #[test] - fn test_member_count() -> anyhow::Result<()> { - let mut client = Client::default(); - client - .expect_send_gql_query::<graphql_client::QueryBody<members_count::Variables>, _>() - .returning(|_| { - Ok(members_count::ResponseData { - current_block: members_count::MembersCountCurrentBlock { - members_count: 10_000, - }, - }) - }); - let mut out = Vec::new(); - members_count(&client, &mut out)?; - let output = std::str::from_utf8(&out)?; - - assert_eq!(output, "There is currently 10000 members in Ğ1 WoT!\n"); - - Ok(()) - } -} diff --git a/src/commands/balance.rs b/src/commands/balance.rs new file mode 100644 index 0000000000000000000000000000000000000000..37e15b97c5ebe70ab50278d650f9a2bf16ed9d86 --- /dev/null +++ b/src/commands/balance.rs @@ -0,0 +1,96 @@ +// Copyright (C) 2020 Éloïs SANCHEZ. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +use crate::*; + +pub(crate) fn balance<W: Write>( + client: &Client, + out: &mut W, + pubkey_or_script: &str, + ud_unit: bool, +) -> anyhow::Result<()> { + let request_body = Balance::build_query(balance::Variables { + script: pubkey_or_script.to_owned(), + with_ud: ud_unit, + }); + + let balance::ResponseData { + balance: balance::BalanceBalance { amount }, + current_ud: current_ud_opt, + } = client.send_gql_query(&request_body)?; + + if let Some(balance::BalanceCurrentUd { amount: ud_amount }) = current_ud_opt { + writeln!( + out, + "The balance of account '{}' is {:.2} UDĞ1 !", + pubkey_or_script, + amount as f64 / ud_amount as f64, + )?; + } else { + writeln!( + out, + "The balance of account '{}' is {}.{:02} Ğ1 !", + pubkey_or_script, + amount / 100, + amount % 100 + )?; + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_balance() -> anyhow::Result<()> { + let mut client = Client::default(); + client + .expect_send_gql_query::<graphql_client::QueryBody<balance::Variables>, _>() + .returning(|_| { + Ok(balance::ResponseData { + balance: balance::BalanceBalance { amount: 2_046 }, + current_ud: None, + }) + }); + let mut out = Vec::new(); + balance(&client, &mut out, "toto", false)?; + let output = std::str::from_utf8(&out)?; + + assert_eq!(output, "The balance of account 'toto' is 20.46 Ğ1 !\n"); + + Ok(()) + } + + #[test] + fn test_balance_with_ud_unit() -> anyhow::Result<()> { + let mut client = Client::default(); + client + .expect_send_gql_query::<graphql_client::QueryBody<balance::Variables>, _>() + .returning(|_| { + Ok(balance::ResponseData { + balance: balance::BalanceBalance { amount: 2_046 }, + current_ud: Some(balance::BalanceCurrentUd { amount: 1_023 }), + }) + }); + let mut out = Vec::new(); + balance(&client, &mut out, "toto", true)?; + let output = std::str::from_utf8(&out)?; + + assert_eq!(output, "The balance of account 'toto' is 2.00 UDĞ1 !\n"); + + Ok(()) + } +} diff --git a/src/commands/current_ud.rs b/src/commands/current_ud.rs new file mode 100644 index 0000000000000000000000000000000000000000..401431e09e125b93dec842731343878d9b767f77 --- /dev/null +++ b/src/commands/current_ud.rs @@ -0,0 +1,61 @@ +// Copyright (C) 2020 Éloïs SANCHEZ. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +use crate::*; + +pub(crate) fn current_ud<W: Write>(client: &Client, out: &mut W) -> anyhow::Result<()> { + let request_body = CurrentUd::build_query(current_ud::Variables); + + if let current_ud::ResponseData { + current_ud: Some(current_ud::CurrentUdCurrentUd { amount }), + } = client.send_gql_query(&request_body)? + { + let int_part = amount / 100; + let dec_part = amount % 100; + writeln!( + out, + "The current UD value is {}.{:02} Ğ1 !", + int_part, dec_part + )?; + } else { + writeln!(out, "server with empty blockchain")?; + } + + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_current_ud() -> anyhow::Result<()> { + let mut client = Client::default(); + client + .expect_send_gql_query::<graphql_client::QueryBody<current_ud::Variables>, _>() + .returning(|_| { + Ok(current_ud::ResponseData { + current_ud: Some(current_ud::CurrentUdCurrentUd { amount: 1_023 }), + }) + }); + let mut out = Vec::new(); + current_ud(&client, &mut out)?; + let output = std::str::from_utf8(&out)?; + + assert_eq!(output, "The current UD value is 10.23 Ğ1 !\n"); + + Ok(()) + } +} diff --git a/src/commands/members_count.rs b/src/commands/members_count.rs new file mode 100644 index 0000000000000000000000000000000000000000..462597ccfa711d7ee43f03468557b8dd5f59798b --- /dev/null +++ b/src/commands/members_count.rs @@ -0,0 +1,60 @@ +// Copyright (C) 2020 Éloïs SANCHEZ. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +use crate::*; + +pub(crate) fn members_count<W: Write>(client: &Client, out: &mut W) -> anyhow::Result<()> { + let request_body = MembersCount::build_query(members_count::Variables); + + let members_count::ResponseData { + current_block: members_count::MembersCountCurrentBlock { members_count }, + } = client.send_gql_query(&request_body)?; + + writeln!( + out, + "There is currently {} members in Ğ1 WoT!", + members_count + )?; + + Ok(()) +} + +// below are tests + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_member_count() -> anyhow::Result<()> { + let mut client = Client::default(); + client + .expect_send_gql_query::<graphql_client::QueryBody<members_count::Variables>, _>() + .returning(|_| { + Ok(members_count::ResponseData { + current_block: members_count::MembersCountCurrentBlock { + members_count: 10_000, + }, + }) + }); + let mut out = Vec::new(); + members_count(&client, &mut out)?; + let output = std::str::from_utf8(&out)?; + + assert_eq!(output, "There is currently 10000 members in Ğ1 WoT!\n"); + + Ok(()) + } +} diff --git a/src/main.rs b/src/main.rs index e23963da15744d7c5e4bb6b1dccb43aad3c555c6..9958830eaf390436c4a8fae03e5d288c1d8fa35c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,7 +32,7 @@ use crate::client::Client; #[cfg(test)] use crate::client::MockClient as Client; use crate::commands::Command; -use commands::{balance, current_ud, members_count}; +use commands::{balance::balance, current_ud::current_ud, members_count::members_count}; use graphql_client::GraphQLQuery; use graphql_client::Response; use std::io::Write;