Implement the http variant of the bank server.
During that process, many shortcomings with the socket server and the bank lib were fixed. I am aware a massive commit like this is not ideal.
This commit is contained in:
parent
c69654a924
commit
dac95b7dae
34 changed files with 1797 additions and 140 deletions
|
@ -2,18 +2,11 @@ use anyhow::Result;
|
|||
|
||||
use bank::account::{Account, AccountError};
|
||||
|
||||
use crate::commands::error_to_code;
|
||||
use crate::commands::error::SocketAccountError;
|
||||
|
||||
pub const START: [u8; 2] = [0xde, 0xad];
|
||||
pub const PONG: u8 = 0b0010_0000;
|
||||
|
||||
fn to_error_code(error: anyhow::Error, default: u8) -> u8 {
|
||||
match error.root_cause().downcast_ref::<AccountError>() {
|
||||
Some(e) => error_to_code(e),
|
||||
None => default,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn account_nr(nr: &str) -> Vec<u8> {
|
||||
let mut response = vec![PONG];
|
||||
response.append(&mut nr.as_bytes().to_vec());
|
||||
|
@ -26,17 +19,17 @@ pub fn account_passivate(was_passivated: bool) -> Vec<u8> {
|
|||
vec![PONG | is_active_byte]
|
||||
}
|
||||
|
||||
pub fn deposit(result: Result<()>) -> Vec<u8> {
|
||||
pub fn deposit(result: Result<f64, AccountError>) -> Vec<u8> {
|
||||
match result {
|
||||
Err(e) => error(to_error_code(e, 10)).to_vec(),
|
||||
Ok(_) => vec![PONG]
|
||||
Err(e) => error(SocketAccountError(e).into()).to_vec(),
|
||||
Ok(_) => vec![PONG],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn withdraw(result: Result<()>) -> Vec<u8> {
|
||||
pub fn withdraw(result: Result<f64, AccountError>) -> Vec<u8> {
|
||||
match result {
|
||||
Err(e) => error(to_error_code(e, 10)).to_vec(),
|
||||
Ok(_) => vec![PONG]
|
||||
Err(e) => error(SocketAccountError(e).into()).to_vec(),
|
||||
Ok(_) => vec![PONG],
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -85,4 +78,4 @@ pub fn account(account: &Account) -> Vec<u8> {
|
|||
|
||||
pub fn error(code: u8) -> [u8; 1] {
|
||||
[0b0100_0000 | code]
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue