32 lines
861 B
Rust
32 lines
861 B
Rust
use std::io::Write;
|
|
use std::net::TcpStream;
|
|
use std::sync::{Arc, RwLock};
|
|
|
|
use anyhow::Result;
|
|
|
|
use crate::bank::Bank;
|
|
use crate::commands::Command;
|
|
use crate::protocol;
|
|
|
|
pub struct CloseAccount;
|
|
|
|
impl Command for CloseAccount {
|
|
fn execute(&self, bank: Arc<RwLock<Bank>>, data: &[u8], mut stream: &TcpStream) -> Result<usize> {
|
|
debug!("account nr bytes {:?}", data);
|
|
|
|
let nr = String::from_utf8_lossy(data).to_string();
|
|
info!("closing account {}...", nr);
|
|
|
|
let bank = bank.read().unwrap();
|
|
|
|
let written = match bank.accounts.get(&nr) {
|
|
Some(acc) => {
|
|
let mut acc = acc.write().unwrap();
|
|
stream.write(&protocol::account_passivate(acc.passivate()))?
|
|
}
|
|
None => stream.write(&protocol::account_passivate(false))?
|
|
};
|
|
|
|
Ok(written)
|
|
}
|
|
} |