Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gva-rust-client
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
1
Issues
1
List
Boards
Labels
Service Desk
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
clients
Rust
gva-rust-client
Commits
af26d6e0
Commit
af26d6e0
authored
Dec 18, 2020
by
Éloïs
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ref: separate each command in a dedicated file
parent
715c6390
Pipeline
#10520
passed with stages
in 12 minutes and 10 seconds
Changes
5
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
222 additions
and
160 deletions
+222
-160
src/commands.rs
src/commands.rs
+4
-159
src/commands/balance.rs
src/commands/balance.rs
+96
-0
src/commands/current_ud.rs
src/commands/current_ud.rs
+61
-0
src/commands/members_count.rs
src/commands/members_count.rs
+60
-0
src/main.rs
src/main.rs
+1
-1
No files found.
src/commands.rs
View file @
af26d6e0
...
...
@@ -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
(())
}
}
src/commands/balance.rs
0 → 100644
View file @
af26d6e0
// 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
(())
}
}
src/commands/current_ud.rs
0 → 100644
View file @
af26d6e0
// 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
(())
}
}
src/commands/members_count.rs
0 → 100644
View file @
af26d6e0
// 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
(())
}
}
src/main.rs
View file @
af26d6e0
...
...
@@ -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
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment