diff --git a/.idea/bank-server.iml b/.idea/bank-server.iml index c254557..750846a 100644 --- a/.idea/bank-server.iml +++ b/.idea/bank-server.iml @@ -2,7 +2,8 @@ - + + diff --git a/Cargo.lock b/Cargo.lock index 5950b41..b8a592f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,9 +13,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.55" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "159bb86af3a200e19a068f4224eae4c8bb2d0fa054c7e5d1cacd5cef95e684cd" +checksum = "4361135be9122e0870de935d7c439aef945b9f9ddd4199a553b5270b49c82a27" [[package]] name = "atty" @@ -29,12 +29,10 @@ dependencies = [ ] [[package]] -name = "bank-server" +name = "bank" version = "0.1.0" dependencies = [ "anyhow", - "log", - "pretty_env_logger", "thiserror", "uuid", ] @@ -89,9 +87,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.119" +version = "0.2.120" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bf2e165bb3457c8e098ea76f3e3bc9db55f87aa90d52d0e6be741470916aaa4" +checksum = "ad5c14e80759d0939d013e6ca49930e59fc53dd8e5009132f76240c179380c09" [[package]] name = "log" @@ -144,9 +142,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.5.4" +version = "1.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" +checksum = "1a11647b6b25ff05a515cb92c365cec08801e83423a235b51e231e1808747286" dependencies = [ "aho-corasick", "memchr", @@ -159,11 +157,21 @@ version = "0.6.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" +[[package]] +name = "socket-server" +version = "0.1.0" +dependencies = [ + "anyhow", + "bank", + "log", + "pretty_env_logger", +] + [[package]] name = "syn" -version = "1.0.86" +version = "1.0.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" +checksum = "ebd69e719f31e88618baa1eaa6ee2de5c9a1c004f1e9ecdb58e8352a13f20a01" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 5380d1f..b198635 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,11 +1,6 @@ -[package] -name = "bank-server" -version = "0.1.0" -edition = "2021" +[workspace] -[dependencies] -anyhow = "1.0.55" -log = "0.4.14" -pretty_env_logger = "0.4.0" -thiserror = "1.0.30" -uuid = { version = "0.8.2", features = ["v4"] } \ No newline at end of file +members = [ + "bank", + "socket-server" +] \ No newline at end of file diff --git a/bank/Cargo.lock b/bank/Cargo.lock new file mode 100644 index 0000000..5950b41 --- /dev/null +++ b/bank/Cargo.lock @@ -0,0 +1,252 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "0.7.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" +dependencies = [ + "memchr", +] + +[[package]] +name = "anyhow" +version = "1.0.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "159bb86af3a200e19a068f4224eae4c8bb2d0fa054c7e5d1cacd5cef95e684cd" + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "bank-server" +version = "0.1.0" +dependencies = [ + "anyhow", + "log", + "pretty_env_logger", + "thiserror", + "uuid", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "env_logger" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + +[[package]] +name = "getrandom" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d39cd93900197114fa1fcb7ae84ca742095eed9442088988ae74fa744e930e77" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "humantime" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" +dependencies = [ + "quick-error", +] + +[[package]] +name = "libc" +version = "0.2.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bf2e165bb3457c8e098ea76f3e3bc9db55f87aa90d52d0e6be741470916aaa4" + +[[package]] +name = "log" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "memchr" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" + +[[package]] +name = "pretty_env_logger" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "926d36b9553851b8b0005f1275891b392ee4d2d833852c417ed025477350fb9d" +dependencies = [ + "env_logger", + "log", +] + +[[package]] +name = "proc-macro2" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quick-error" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" + +[[package]] +name = "quote" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex" +version = "1.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" + +[[package]] +name = "syn" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "thiserror" +version = "1.0.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unicode-xid" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" + +[[package]] +name = "uuid" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/bank/Cargo.toml b/bank/Cargo.toml new file mode 100644 index 0000000..27dd125 --- /dev/null +++ b/bank/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "bank" +version = "0.1.0" +edition = "2021" + +[dependencies] +anyhow = "1.0.55" +thiserror = "1.0.30" +uuid = { version = "0.8.2", features = ["v4"] } \ No newline at end of file diff --git a/src/account.rs b/bank/src/account.rs similarity index 99% rename from src/account.rs rename to bank/src/account.rs index ab0bcaa..3fad577 100644 --- a/src/account.rs +++ b/bank/src/account.rs @@ -1,8 +1,9 @@ use std::hash::{Hash, Hasher}; -use thiserror::Error; use anyhow::{bail, Result}; +use thiserror::Error; use uuid::Uuid; + use crate::account::AccountError::{Inactive, InvalidAmount, Overdraw}; #[derive(Error, Debug)] @@ -139,4 +140,4 @@ mod tests { assert!(acc.passivate()); assert!(!acc.passivate()); } -} \ No newline at end of file +} diff --git a/src/bank.rs b/bank/src/bank.rs similarity index 97% rename from src/bank.rs rename to bank/src/bank.rs index ca38097..8505e50 100644 --- a/src/bank.rs +++ b/bank/src/bank.rs @@ -50,7 +50,7 @@ mod tests { let nr = bank.create_account("Rohaja".into()); assert!(bank.accounts.get(&nr).is_some()); - assert!(bank.accounts.get("thisisnotanuuid").is_none()); + assert!(bank.accounts.get("thisisnotauuid").is_none()); } #[test] diff --git a/bank/src/lib.rs b/bank/src/lib.rs new file mode 100644 index 0000000..0e75320 --- /dev/null +++ b/bank/src/lib.rs @@ -0,0 +1,2 @@ +pub mod account; +pub mod bank; \ No newline at end of file diff --git a/socket-server/Cargo.toml b/socket-server/Cargo.toml new file mode 100644 index 0000000..571ad38 --- /dev/null +++ b/socket-server/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "socket-server" +version = "0.1.0" +edition = "2021" + +[dependencies] +bank = { path = "../bank" } +anyhow = "1.0.55" +log = "0.4.14" +pretty_env_logger = "0.4.0" \ No newline at end of file diff --git a/src/commands/close_account.rs b/socket-server/src/commands/close_account.rs similarity index 97% rename from src/commands/close_account.rs rename to socket-server/src/commands/close_account.rs index 18c7688..dcae949 100644 --- a/src/commands/close_account.rs +++ b/socket-server/src/commands/close_account.rs @@ -4,7 +4,8 @@ use std::sync::{Arc, RwLock}; use anyhow::Result; -use crate::bank::Bank; +use bank::bank::Bank; + use crate::commands::Command; use crate::protocol; diff --git a/src/commands/create_account.rs b/socket-server/src/commands/create_account.rs similarity index 96% rename from src/commands/create_account.rs rename to socket-server/src/commands/create_account.rs index 1b7d054..1f7f5a6 100644 --- a/src/commands/create_account.rs +++ b/socket-server/src/commands/create_account.rs @@ -4,7 +4,8 @@ use std::sync::{Arc, RwLock}; use anyhow::Result; -use crate::bank::Bank; +use bank::bank::Bank; + use crate::commands::Command; use crate::protocol; diff --git a/src/commands/deposit.rs b/socket-server/src/commands/deposit.rs similarity index 97% rename from src/commands/deposit.rs rename to socket-server/src/commands/deposit.rs index 823f435..3014bff 100644 --- a/src/commands/deposit.rs +++ b/socket-server/src/commands/deposit.rs @@ -4,7 +4,8 @@ use std::sync::{Arc, RwLock}; use anyhow::Result; -use crate::bank::Bank; +use bank::bank::Bank; + use crate::commands::Command; use crate::protocol; diff --git a/src/commands/fail.rs b/socket-server/src/commands/fail.rs similarity index 95% rename from src/commands/fail.rs rename to socket-server/src/commands/fail.rs index c23a7b4..8bbec29 100644 --- a/src/commands/fail.rs +++ b/socket-server/src/commands/fail.rs @@ -4,7 +4,8 @@ use std::sync::{Arc, RwLock}; use anyhow::Result; -use crate::bank::Bank; +use bank::bank::Bank; + use crate::commands::Command; use crate::protocol; diff --git a/src/commands/get_account.rs b/socket-server/src/commands/get_account.rs similarity index 97% rename from src/commands/get_account.rs rename to socket-server/src/commands/get_account.rs index 028131a..9ec584c 100644 --- a/src/commands/get_account.rs +++ b/socket-server/src/commands/get_account.rs @@ -4,7 +4,8 @@ use std::sync::{Arc, RwLock}; use anyhow::Result; -use crate::bank::Bank; +use bank::bank::Bank; + use crate::commands::Command; use crate::protocol; diff --git a/src/commands/get_account_nrs.rs b/socket-server/src/commands/get_account_nrs.rs similarity index 96% rename from src/commands/get_account_nrs.rs rename to socket-server/src/commands/get_account_nrs.rs index f28735f..e83319d 100644 --- a/src/commands/get_account_nrs.rs +++ b/socket-server/src/commands/get_account_nrs.rs @@ -4,7 +4,8 @@ use std::sync::{Arc, RwLock}; use anyhow::Result; -use crate::bank::Bank; +use bank::bank::Bank; + use crate::commands::Command; use crate::protocol; diff --git a/src/commands/mod.rs b/socket-server/src/commands/mod.rs similarity index 81% rename from src/commands/mod.rs rename to socket-server/src/commands/mod.rs index 32e7188..07b4252 100644 --- a/src/commands/mod.rs +++ b/socket-server/src/commands/mod.rs @@ -3,9 +3,10 @@ use std::net::TcpStream; use std::sync::{Arc, RwLock}; use anyhow::Result; -use crate::account::AccountError; -use crate::bank::Bank; +use bank::account::AccountError; +use bank::bank::Bank; + use crate::commands::close_account::CloseAccount; use crate::commands::create_account::CreateAccount; use crate::commands::deposit::Deposit; @@ -32,9 +33,11 @@ pub struct Commands { cmds: HashMap>, } -impl From<&AccountError> for u8 { - fn from(error: &AccountError) -> Self { - match error { +struct WebAccountError(AccountError); + +impl From<&WebAccountError> for u8 { + fn from(error: &WebAccountError) -> Self { + match error.0 { AccountError::Overdraw() => 11, AccountError::Inactive() => 12, AccountError::InvalidAmount() => 13, @@ -42,6 +45,14 @@ impl From<&AccountError> for u8 { } } +pub fn error_to_code(error: &AccountError) -> u8 { + match error { + AccountError::Overdraw() => 11, + AccountError::Inactive() => 12, + AccountError::InvalidAmount() => 13, + } +} + impl Commands { pub fn new() -> Self { let mut cmds = HashMap::new(); diff --git a/src/commands/pong.rs b/socket-server/src/commands/pong.rs similarity index 94% rename from src/commands/pong.rs rename to socket-server/src/commands/pong.rs index e9d2cf6..cb04404 100644 --- a/src/commands/pong.rs +++ b/socket-server/src/commands/pong.rs @@ -4,7 +4,8 @@ use std::sync::{Arc, RwLock}; use anyhow::Result; -use crate::bank::Bank; +use bank::bank::Bank; + use crate::commands::Command; use crate::protocol; diff --git a/src/commands/withdraw.rs b/socket-server/src/commands/withdraw.rs similarity index 97% rename from src/commands/withdraw.rs rename to socket-server/src/commands/withdraw.rs index e80164a..a90eb82 100644 --- a/src/commands/withdraw.rs +++ b/socket-server/src/commands/withdraw.rs @@ -4,7 +4,8 @@ use std::sync::{Arc, RwLock}; use anyhow::Result; -use crate::bank::Bank; +use bank::bank::Bank; + use crate::commands::Command; use crate::protocol; diff --git a/src/main.rs b/socket-server/src/main.rs similarity index 91% rename from src/main.rs rename to socket-server/src/main.rs index 831e5b3..28694bd 100644 --- a/src/main.rs +++ b/socket-server/src/main.rs @@ -1,8 +1,6 @@ #[macro_use] extern crate log; -mod account; -mod bank; mod protocol; mod server; mod threadpool; diff --git a/src/protocol.rs b/socket-server/src/protocol.rs similarity index 95% rename from src/protocol.rs rename to socket-server/src/protocol.rs index d697688..76797e6 100644 --- a/src/protocol.rs +++ b/socket-server/src/protocol.rs @@ -1,12 +1,15 @@ -use crate::account::{Account, AccountError}; use anyhow::Result; +use bank::account::{Account, AccountError}; + +use crate::commands::error_to_code; + 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::() { - Some(e) => e.into(), + Some(e) => error_to_code(e), None => default, } } diff --git a/src/server.rs b/socket-server/src/server.rs similarity index 98% rename from src/server.rs rename to socket-server/src/server.rs index a9758f6..f17490d 100644 --- a/src/server.rs +++ b/socket-server/src/server.rs @@ -4,7 +4,8 @@ use std::sync::{Arc, RwLock}; use anyhow::{bail, Result}; -use crate::bank::Bank; +use bank::bank::Bank; + use crate::commands::Commands; use crate::protocol; use crate::threadpool::ThreadPool; diff --git a/src/threadpool.rs b/socket-server/src/threadpool.rs similarity index 100% rename from src/threadpool.rs rename to socket-server/src/threadpool.rs