Compare commits

...

110 Commits

Author SHA1 Message Date
filipriec
060cff3f0f compiled 2025-03-31 17:10:51 +02:00
filipriec
07c48985e4 proper pass of the arguments 2025-03-31 16:25:11 +02:00
filipriec
3d266c2ad0 switching of the views done 2025-03-31 16:23:25 +02:00
filipriec
e2c326bf1e login gRPC 2025-03-31 16:16:07 +02:00
filipriec
7f5b671084 switch between login or form in the save request 2025-03-31 15:55:54 +02:00
filipriec
3ccf71ed0f switcher, have bugs 2025-03-31 15:42:48 +02:00
filipriec
efb59c5571 split config in modes/canvas is fixed and working properly well 2025-03-31 15:12:50 +02:00
filipriec
26cf06df14 needs fixes of the errors 2025-03-31 14:58:10 +02:00
filipriec
c82b62f72b moving modes/canvas/common.rs into the functions 2025-03-31 13:22:08 +02:00
filipriec
87cdb8048d we compiled fully, time to move grpc calls now 2025-03-31 12:07:01 +02:00
filipriec
f71498703a implementation of login 2025-03-31 10:40:34 +02:00
filipriec
94eea47b76 cargo fix 2025-03-31 10:24:20 +02:00
filipriec
ac833294c4 moved login from grpc_client 2025-03-31 10:23:23 +02:00
filipriec
81f0527085 ui.rs gRPC code removed 2025-03-31 09:54:16 +02:00
filipriec
dd4d9e88c6 ready to move gRPC into a single services folder, time to do the changes now 2025-03-31 09:39:47 +02:00
filipriec
f66d67c238 grpc_service moved 2025-03-31 07:13:24 +02:00
filipriec
9cf25afa52 moving grpc_client, needs import fixes 2025-03-31 07:08:51 +02:00
filipriec
2ed2419f9e Add auth service client and auth state fields 2025-03-30 21:44:45 +02:00
filipriec
e19b30f4f4 auth dialog implemented 2025-03-30 20:38:09 +02:00
filipriec
f6e21b6a61 is edit mode passing properly 2025-03-30 19:03:58 +02:00
filipriec
2180d8decf step2 compiled 2025-03-30 18:41:27 +02:00
filipriec
12aec72141 added fields to the traits 2025-03-30 18:33:21 +02:00
filipriec
0568441e46 minor ui stuff 2025-03-30 18:29:33 +02:00
filipriec
301189bd85 login is not dependent on form/form.rs and only on canvas now 2025-03-30 17:00:51 +02:00
filipriec
81767f376f button sizes 2025-03-30 16:35:38 +02:00
filipriec
e36b1817bc working, split config where functions for each page are defined for this page, if the functions are not general for each page. Huge update, works for tui/functions/form and fui/functions/login. Working, time to move to other things 2025-03-30 15:46:49 +02:00
filipriec
13d4db6bdc properly handling up and down in the form and login, login needs logic implementation 2025-03-30 15:11:02 +02:00
filipriec
b5a5ebd7c0 working exactly as i want, now making the login up and down to work properly well 2025-03-30 15:03:12 +02:00
filipriec
6e04c1f267 original form fully working in the tui functions is the logic for the form 2025-03-30 14:24:12 +02:00
filipriec
a0d96cb87a moving to tui/functions/form.rs read_only functions 2025-03-30 14:13:07 +02:00
filipriec
4052a5b81b fixed unused imports 2025-03-30 14:01:08 +02:00
filipriec
ed99ebf541 compiled if statement in the read_only mode 2025-03-30 13:57:22 +02:00
filipriec
0a4f59cf8e reverting back fully 2025-03-30 13:15:51 +02:00
filipriec
fd6a9b73be reverting back 2025-03-30 13:11:04 +02:00
filipriec
dc994f6ee1 login in the functions 2025-03-30 13:04:48 +02:00
filipriec
e234dd1785 down and up now added to work in the original form 2025-03-30 12:09:20 +02:00
filipriec
6e0943f0cc compiled 2025-03-30 00:51:11 +01:00
filipriec
9622e0bd3c moving read_only functions for the form specific 2025-03-29 22:36:03 +01:00
filipriec
ee666e91ed adjusted login not working yet, wrong changes 2025-03-29 00:09:06 +01:00
filipriec
4f8f2f4a40 step4 2025-03-28 20:48:06 +01:00
filipriec
f21953147b step 3 compiled 2025-03-28 14:35:16 +01:00
filipriec
48b2658b55 step2 2025-03-28 14:29:36 +01:00
filipriec
37b08fdd10 implementation of canvas for multiple pages step 1 2025-03-28 14:26:18 +01:00
filipriec
d4efdf4833 login improvements 2025-03-27 12:27:43 +01:00
filipriec
b408abe8c6 quick prod check .sqlx 2025-03-26 00:50:25 +01:00
filipriec
722c63af54 fixed the navigation previous function, therefore we are now moving into the login properly, lets implement the auth now 2025-03-26 00:46:59 +01:00
filipriec
4601ba4094 unused stuff removed 2025-03-26 00:43:42 +01:00
filipriec
3e2b8a36df fixing warnings 2025-03-26 00:39:27 +01:00
filipriec
11214734ae functions are now in the file dedicated to the functions belonging to the page 2025-03-26 00:26:36 +01:00
filipriec
92045a4e67 unused stuff 2025-03-26 00:16:38 +01:00
filipriec
9564bd8524 login page is now being properly displayed 2025-03-26 00:04:30 +01:00
filipriec
ad64df2ec3 nothing is fixed, it doesnt work, i cant see a login page 2025-03-25 23:23:47 +01:00
filipriec
aab11c1cba login logic in the components 2025-03-25 22:56:24 +01:00
filipriec
1fe139e0c5 we compiled 2025-03-25 22:01:09 +01:00
filipriec
4ced1a36d4 moved form.rs into the state where it really belongs to 2025-03-25 21:23:52 +01:00
filipriec
45fff34c4c login page being implemented slowly 2025-03-25 16:02:23 +01:00
filipriec
c84fa4a692 frontend implementing login 2025-03-25 15:57:45 +01:00
filipriec
eba3f56ba3 indefinite jwt expiration set 2025-03-25 13:17:13 +01:00
filipriec
71ab588c16 tonic rbac to tower 2025-03-25 12:36:31 +01:00
filipriec
195375c083 temp disable of the rbac 2025-03-25 12:35:10 +01:00
filipriec
34dafcc23e rbac using tonic 2025-03-25 11:33:14 +01:00
filipriec
507f86fcf1 docs 2025-03-25 10:35:22 +01:00
filipriec
f40654d2c4 it compiled 2025-03-25 10:28:29 +01:00
filipriec
cd32c175a4 jwt implementation and login, not working yet 2025-03-25 10:15:17 +01:00
filipriec
9393294af8 docs, auth starting officially 0.2.0 version 2025-03-24 22:17:02 +01:00
filipriec
24c426229c registration works perfectly well 2025-03-24 22:15:17 +01:00
filipriec
3ed6fd4ee8 we compiled 2025-03-24 22:03:04 +01:00
filipriec
70d83c284a broken only push user data 2025-03-24 21:46:04 +01:00
filipriec
8a248cab58 working finally 2025-03-24 18:57:59 +01:00
filipriec
e6851e1fe4 small error to fix 2025-03-24 17:14:12 +01:00
filipriec
65ff1256aa canvas common needs redesign, sidebar displaying even when no profile selected 2025-03-24 16:46:02 +01:00
filipriec
36dc4302a0 sidebar if there is no profile selected 2025-03-24 16:15:55 +01:00
filipriec
938a1f16f1 form component is now in the separate component 2025-03-24 15:57:41 +01:00
filipriec
355aff3032 working perfectly well right now 2025-03-24 15:13:57 +01:00
filipriec
3bb771187a admin bug fixed, time for intro also 2025-03-24 15:09:35 +01:00
filipriec
aa3ff18f9c now for common in canvas 2025-03-24 14:54:02 +01:00
filipriec
1fc9a0e1ff reorganization, preserved functionality 2025-03-24 14:29:28 +01:00
filipriec
cdac78c1bc needs to reset the render but it finally works 2025-03-24 14:17:44 +01:00
filipriec
bdb6cd4069 modes needs more progress 2025-03-24 11:46:33 +01:00
filipriec
ec5802b3a2 works 2025-03-24 11:17:59 +01:00
filipriec
1eb2edc1df mode manager finally 2025-03-24 11:03:52 +01:00
filipriec
2da009eede marked bloat 2025-03-24 00:06:44 +01:00
filipriec
b2fd44df49 working select setting the form to true in state state.rs 2025-03-23 22:41:18 +01:00
filipriec
78e8cce08b moved to common modes 2025-03-23 22:14:24 +01:00
filipriec
2cf4cd6748 appstate for form 2025-03-23 22:08:33 +01:00
filipriec
7caa4d8c3c general mode 2025-03-23 21:00:53 +01:00
filipriec
9917195fc4 disabling modes where they shouldnt be enabled BIG UPDATE 2025-03-23 20:07:47 +01:00
filipriec
fbcea1b270 trying to make the intro and admin with general keybindings 2025-03-23 19:17:42 +01:00
filipriec
87b07db26a completely broken intro or admin 2025-03-23 15:35:57 +01:00
filipriec
4481560025 HUGE CHANGES TO MODESA 2025-03-23 15:11:43 +01:00
filipriec
d1d33b5752 project redesign 2025-03-23 13:50:47 +01:00
filipriec
c6c6c5ed81 restored rendering 2025-03-23 12:59:36 +01:00
filipriec
4ddcb34205 nothing 2025-03-23 12:56:27 +01:00
filipriec
83393a20e2 fix of the error 2025-03-23 12:44:33 +01:00
filipriec
13d501e6d7 not working 2025-03-23 12:30:00 +01:00
filipriec
993febd204 admin panel keyindings 2025-03-23 11:28:39 +01:00
filipriec
49fe2aa793 edit mode is now perfectly working 2025-03-23 10:55:05 +01:00
filipriec
87a572783a i think its a step in the right direction, needs to export other functions now 2025-03-23 10:03:59 +01:00
filipriec
ca8dea53fd VERY SUSPICIOUS BREAKING FUNCTIONALITY CHECK LATER 2025-03-23 00:49:19 +01:00
filipriec
fef2f12c9a :disabled in the edit mode, cant type it tho, needs fix 2025-03-23 00:28:51 +01:00
filipriec
1a529a70bf gamechanging, commands works only on their windows properly well 2025-03-22 23:32:33 +01:00
filipriec
8da29376ab moved key sequences also 2025-03-22 23:03:13 +01:00
filipriec
8ad5fedcea config was moved successfully 2025-03-22 22:58:58 +01:00
filipriec
16a7fa0bcc working change of the themes 2025-03-22 22:55:43 +01:00
filipriec
5f6858251c moved themes out of the config 2025-03-22 22:51:53 +01:00
filipriec
73567ae5cf moved everything up 2025-03-22 22:40:38 +01:00
filipriec
fe2d1e4684 working perfectly well, needs bug fixes to proper shortcuts to proper windows 2025-03-22 20:10:39 +01:00
filipriec
7d4b043d63 still one error missing 2025-03-22 20:05:13 +01:00
filipriec
04b4220c76 needs bug fixing 2025-03-22 19:37:41 +01:00
filipriec
841418759b last changes 2025-03-22 16:26:38 +01:00
95 changed files with 3146 additions and 681 deletions

View File

@@ -4,3 +4,5 @@ RUST_DB_USER=multi_psql_dev
RUST_DB_PASSWORD=3
RUST_DB_HOST=localhost
RUST_DB_PORT=5432
JWT_SECRET=<YOUR-JWT-HERE>

194
Cargo.lock generated
View File

@@ -170,9 +170,9 @@ dependencies = [
[[package]]
name = "async-trait"
version = "0.1.87"
version = "0.1.88"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d556ec1359574147ec0c4fc5eb525f3f23263a592b1a9c07e0a75b427de55c97"
checksum = "e539d3fca749fcee5236ab05e93a52867dd549cc157c8cb7f99595f3cedffdb5"
dependencies = [
"proc-macro2",
"quote",
@@ -274,6 +274,19 @@ version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b"
[[package]]
name = "bcrypt"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92758ad6077e4c76a6cadbce5005f666df70d4f13b19976b1a8062eef880040f"
dependencies = [
"base64",
"blowfish",
"getrandom 0.3.1",
"subtle",
"zeroize",
]
[[package]]
name = "bigdecimal"
version = "0.4.7"
@@ -323,6 +336,16 @@ dependencies = [
"generic-array",
]
[[package]]
name = "blowfish"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e412e2cd0f2b2d93e02543ceae7917b3c70331573df19ee046bcbc35e45e87d7"
dependencies = [
"byteorder",
"cipher",
]
[[package]]
name = "bumpalo"
version = "3.17.0"
@@ -387,9 +410,20 @@ dependencies = [
]
[[package]]
name = "client"
version = "0.1.0"
name = "cipher"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad"
dependencies = [
"crypto-common",
"inout",
]
[[package]]
name = "client"
version = "0.2.5"
dependencies = [
"async-trait",
"common",
"crossterm",
"dirs 6.0.0",
@@ -424,7 +458,7 @@ dependencies = [
[[package]]
name = "common"
version = "0.1.0"
version = "0.2.5"
dependencies = [
"prost",
"serde",
@@ -782,7 +816,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d"
dependencies = [
"libc",
"windows-sys 0.52.0",
"windows-sys 0.59.0",
]
[[package]]
@@ -991,8 +1025,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
dependencies = [
"cfg-if",
"js-sys",
"libc",
"wasi 0.11.0+wasi-snapshot-preview1",
"wasm-bindgen",
]
[[package]]
@@ -1452,6 +1488,15 @@ version = "2.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c7245a08504955605670dbf141fceab975f15ca21570696aebe9d2e71576bd"
[[package]]
name = "inout"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01"
dependencies = [
"generic-array",
]
[[package]]
name = "instability"
version = "0.3.7"
@@ -1499,6 +1544,21 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "jsonwebtoken"
version = "9.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a87cc7a48537badeae96744432de36f4be2b4a34a05a5ef32e9dd8a1c169dde"
dependencies = [
"base64",
"js-sys",
"pem",
"ring",
"serde",
"serde_json",
"simple_asn1",
]
[[package]]
name = "lasso"
version = "0.7.3"
@@ -1886,6 +1946,16 @@ version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
[[package]]
name = "pem"
version = "3.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38af38e8470ac9dee3ce1bae1af9c1671fffc44ddfd8bd1d0a3445bf349a8ef3"
dependencies = [
"base64",
"serde",
]
[[package]]
name = "pem-rfc7468"
version = "0.7.0"
@@ -2030,6 +2100,28 @@ dependencies = [
"toml_edit",
]
[[package]]
name = "proc-macro-error-attr2"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96de42df36bb9bba5542fe9f1a054b8cc87e172759a1868aa05c1f3acc89dfc5"
dependencies = [
"proc-macro2",
"quote",
]
[[package]]
name = "proc-macro-error2"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "11ec05c52be0a07b08061f7dd003e7d7092e0472bc731b4af7bb1ef876109802"
dependencies = [
"proc-macro-error-attr2",
"proc-macro2",
"quote",
"syn 2.0.99",
]
[[package]]
name = "proc-macro2"
version = "1.0.94"
@@ -2281,6 +2373,20 @@ dependencies = [
"tstr",
]
[[package]]
name = "ring"
version = "0.17.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7"
dependencies = [
"cc",
"cfg-if",
"getrandom 0.2.15",
"libc",
"untrusted",
"windows-sys 0.52.0",
]
[[package]]
name = "rsa"
version = "0.9.7"
@@ -2356,7 +2462,7 @@ dependencies = [
"errno",
"libc",
"linux-raw-sys 0.4.15",
"windows-sys 0.52.0",
"windows-sys 0.59.0",
]
[[package]]
@@ -2369,7 +2475,7 @@ dependencies = [
"errno",
"libc",
"linux-raw-sys 0.9.2",
"windows-sys 0.52.0",
"windows-sys 0.59.0",
]
[[package]]
@@ -2483,12 +2589,14 @@ dependencies = [
[[package]]
name = "server"
version = "0.1.0"
version = "0.2.5"
dependencies = [
"bcrypt",
"chrono",
"common",
"dashmap",
"dotenvy",
"jsonwebtoken",
"lazy_static",
"prost",
"regex",
@@ -2504,6 +2612,8 @@ dependencies = [
"tonic",
"tonic-reflection",
"tracing",
"uuid",
"validator",
]
[[package]]
@@ -2574,6 +2684,18 @@ dependencies = [
"rand_core 0.6.4",
]
[[package]]
name = "simple_asn1"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "297f631f50729c8c99b84667867963997ec0b50f32b2a7dbcab828ef0541e8bb"
dependencies = [
"num-bigint",
"num-traits",
"thiserror 2.0.12",
"time",
]
[[package]]
name = "sized-chunks"
version = "0.6.5"
@@ -2678,6 +2800,7 @@ dependencies = [
"tokio-stream",
"tracing",
"url",
"uuid",
]
[[package]]
@@ -2760,6 +2883,7 @@ dependencies = [
"thiserror 2.0.12",
"time",
"tracing",
"uuid",
"whoami",
]
@@ -2799,6 +2923,7 @@ dependencies = [
"thiserror 2.0.12",
"time",
"tracing",
"uuid",
"whoami",
]
@@ -2825,6 +2950,7 @@ dependencies = [
"time",
"tracing",
"url",
"uuid",
]
[[package]]
@@ -3029,7 +3155,7 @@ dependencies = [
"getrandom 0.3.1",
"once_cell",
"rustix 1.0.1",
"windows-sys 0.52.0",
"windows-sys 0.59.0",
]
[[package]]
@@ -3450,6 +3576,12 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fc81956842c57dac11422a97c3b8195a1ff727f06e85c84ed2e8aa277c9a0fd"
[[package]]
name = "untrusted"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1"
[[package]]
name = "url"
version = "2.5.4"
@@ -3473,6 +3605,46 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
[[package]]
name = "uuid"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "458f7a779bf54acc9f347480ac654f68407d3aab21269a6e3c9f922acd9e2da9"
dependencies = [
"getrandom 0.3.1",
"serde",
]
[[package]]
name = "validator"
version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43fb22e1a008ece370ce08a3e9e4447a910e92621bb49b85d6e48a45397e7cfa"
dependencies = [
"idna",
"once_cell",
"regex",
"serde",
"serde_derive",
"serde_json",
"url",
"validator_derive",
]
[[package]]
name = "validator_derive"
version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7df16e474ef958526d1205f6dda359fdfab79d9aa6d54bafcb92dcd07673dca"
dependencies = [
"darling",
"once_cell",
"proc-macro-error2",
"proc-macro2",
"quote",
"syn 2.0.99",
]
[[package]]
name = "vcpkg"
version = "0.2.15"
@@ -3623,7 +3795,7 @@ version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
dependencies = [
"windows-sys 0.52.0",
"windows-sys 0.59.0",
]
[[package]]

View File

@@ -5,7 +5,7 @@ resolver = "2"
[workspace.package]
# TODO: idk how to do the name, fix later
# name = "Multieko2"
version = "0.1.0"
version = "0.2.5"
edition = "2021"
license = "GPL-3.0-or-later"
authors = ["Filip Priečinský <filippriec@gmail.com>"]

View File

@@ -5,6 +5,7 @@ edition.workspace = true
license.workspace = true
[dependencies]
async-trait = "0.1.88"
common = { path = "../common" }
crossterm = "0.28.1"

View File

@@ -1,6 +1,18 @@
# config.toml
[keybindings]
enter_command_mode = [":", "ctrl+;"]
[keybindings.general]
move_up = ["k", "Up"]
move_down = ["j", "Down"]
next_option = ["l", "Right"]
previous_option = ["h", "Left"]
select = ["Enter"]
toggle_sidebar = ["ctrl+t"]
next_field = ["Tab"]
prev_field = ["Shift+Tab"]
[keybindings.common]
save = ["ctrl+s"]
quit = ["ctrl+q"]
@@ -33,7 +45,6 @@ move_line_start = ["0"]
move_line_end = ["$"]
move_first_line = ["gg"]
move_last_line = ["x"]
enter_command_mode = [":", "ctrl+;"]
[keybindings.edit]
exit_edit_mode = ["esc","ctrl+e"]

View File

@@ -1,21 +1,49 @@
// src/components/admin/admin_panel.rs
use ratatui::{
layout::{Alignment, Constraint, Direction, Layout, Rect},
widgets::{Block, BorderType, Borders, List, ListItem, ListState, Paragraph},
style::Style,
text::{Line, Span},
widgets::{Block, BorderType, Borders, Paragraph},
text::{Line, Span, Text},
layout::{Alignment, Constraint, Direction, Layout, Rect},
Frame,
};
use crate::config::colors::Theme;
use common::proto::multieko2::table_definition::ProfileTreeResponse;
use crate::config::colors::themes::Theme;
pub struct AdminPanelState;
pub struct AdminPanelState {
pub list_state: ListState,
pub profiles: Vec<String>,
}
impl AdminPanelState {
pub fn new() -> Self {
Self
pub fn new(profiles: Vec<String>) -> Self {
let mut list_state = ListState::default();
if !profiles.is_empty() {
list_state.select(Some(0));
}
Self { list_state, profiles }
}
pub fn render(&self, f: &mut Frame, area: Rect, theme: &Theme) {
pub fn next(&mut self) {
let i = self.list_state.selected().map_or(0, |i|
if i >= self.profiles.len() - 1 { 0 } else { i + 1 });
self.list_state.select(Some(i));
}
pub fn previous(&mut self) {
let i = self.list_state.selected().map_or(0, |i|
if i == 0 { self.profiles.len() - 1 } else { i - 1 });
self.list_state.select(Some(i));
}
pub fn render(
&mut self,
f: &mut Frame,
area: Rect,
theme: &Theme,
profile_tree: &ProfileTreeResponse,
selected_profile: &Option<String>,
) {
let block = Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
@@ -27,18 +55,64 @@ impl AdminPanelState {
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(3), // Title
Constraint::Min(1), // Content
])
.constraints([Constraint::Length(3), Constraint::Min(1)])
.split(inner_area);
// Title
let title = Line::from(Span::styled("Admin Panel", Style::default().fg(theme.highlight)));
let title_para = Paragraph::new(title)
.alignment(Alignment::Center);
f.render_widget(title_para, chunks[0]);
let title_widget = Paragraph::new(title).alignment(Alignment::Center);
f.render_widget(title_widget, chunks[0]);
let content = Paragraph::new("Admin panel content goes here");
f.render_widget(content, chunks[1]);
// Content
let content_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(30), Constraint::Percentage(70)])
.split(chunks[1]);
// Profile list
let items: Vec<ListItem> = self.profiles.iter()
.map(|p| ListItem::new(Line::from(vec![
Span::styled(
if Some(p) == selected_profile.as_ref() { "" } else { " " },
Style::default().fg(theme.accent)
),
Span::styled(p, Style::default().fg(theme.fg)),
])))
.collect();
let list = List::new(items)
.block(Block::default().title("Profiles"))
.highlight_style(Style::default().bg(theme.highlight).fg(theme.bg));
f.render_stateful_widget(list, content_chunks[0], &mut self.list_state);
// Profile details
if let Some(profile) = self.list_state.selected()
.and_then(|i| profile_tree.profiles.get(i))
{
let mut text = Text::default();
text.lines.push(Line::from(vec![
Span::styled("Profile: ", Style::default().fg(theme.accent)),
Span::styled(&profile.name, Style::default().fg(theme.highlight)),
]));
text.lines.push(Line::from(""));
text.lines.push(Line::from(Span::styled("Tables:", Style::default().fg(theme.accent))));
for table in &profile.tables {
let mut line = vec![Span::styled(format!("├─ {}", table.name), theme.fg)];
if !table.depends_on.is_empty() {
line.push(Span::styled(
format!("{}", table.depends_on.join(", ")),
Style::default().fg(theme.secondary)
));
}
text.lines.push(Line::from(line));
}
let details_widget = Paragraph::new(text)
.block(Block::default().title("Details"));
f.render_widget(details_widget, content_chunks[1]);
}
}
}

View File

@@ -0,0 +1,5 @@
// src/components/form.rs
pub mod login;
pub mod register;
pub use login::*;

View File

@@ -0,0 +1,146 @@
// src/components/auth/login.rs
use crate::{
config::colors::themes::Theme,
state::pages::auth::AuthState,
components::common::dialog,
state::state::AppState, // Add this import
};
use ratatui::{
layout::{Alignment, Constraint, Direction, Layout, Rect, Margin},
style::{Style, Modifier, Color}, // Removed unused Color import
widgets::{Block, BorderType, Borders, Paragraph},
Frame, // Removed unused Span import
};
pub fn render_login(
f: &mut Frame,
area: Rect,
theme: &Theme,
state: &AuthState,
app_state: &AppState, // Add AppState parameter
is_edit_mode: bool,
) {
// Main container
let block = Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Plain)
.border_style(Style::default().fg(theme.border))
.title(" Login ")
.style(Style::default().bg(theme.bg));
f.render_widget(block, area);
let inner_area = area.inner(Margin {
horizontal: 1,
vertical: 1,
});
// Layout chunks
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(4), // Form (2 fields + padding)
Constraint::Length(1), // Error message
Constraint::Length(3), // Buttons
])
.split(inner_area);
// --- FORM RENDERING ---
let input_block = Block::default()
.borders(Borders::ALL)
.border_style(if is_edit_mode {
Style::default().fg(theme.accent)
} else {
Style::default().fg(theme.border)
})
.style(Style::default().bg(theme.bg));
// Calculate inner area BEFORE rendering
let input_area = input_block.inner(chunks[0]);
f.render_widget(input_block, chunks[0]);
// Use the canvas renderer for fields
crate::components::handlers::canvas::render_canvas(
f,
input_area, // Use the pre-calculated area
state,
&["Username/Email", "Password"],
&state.current_field,
&[&state.username, &state.password],
theme,
is_edit_mode,
);
// --- BUTTONS --- (Keep this unchanged)
let button_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
.split(chunks[2]);
// Login Button
let login_active = !state.return_selected;
let mut login_style = Style::default().fg(theme.fg);
let mut login_border = Style::default().fg(theme.border);
if login_active {
login_style = login_style.fg(theme.highlight).add_modifier(Modifier::BOLD);
login_border = login_border.fg(theme.accent);
}
f.render_widget(
Paragraph::new("Login")
.style(login_style)
.alignment(Alignment::Center)
.block(
Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Plain)
.border_style(login_border),
),
button_chunks[0],
);
// Return Button
let return_active = state.return_selected;
let mut return_style = Style::default().fg(theme.fg);
let mut return_border = Style::default().fg(theme.border);
if return_active {
return_style = return_style.fg(theme.highlight).add_modifier(Modifier::BOLD);
return_border = return_border.fg(theme.accent);
}
f.render_widget(
Paragraph::new("Return")
.style(return_style)
.alignment(Alignment::Center)
.block(
Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Plain)
.border_style(return_border),
),
button_chunks[1],
);
// Error message
if let Some(err) = &state.error_message {
f.render_widget(
Paragraph::new(err.as_str())
.style(Style::default().fg(Color::Red))
.alignment(Alignment::Center),
chunks[1],
);
}
if app_state.ui.dialog.show_dialog {
dialog::render_dialog(
f,
f.area(), // Use area() instead of deprecated size()
theme,
&app_state.ui.dialog.dialog_title,
&app_state.ui.dialog.dialog_message,
app_state.ui.dialog.dialog_button_active,
);
}
}

View File

@@ -2,7 +2,9 @@
pub mod command_line;
pub mod status_line;
pub mod background;
pub mod dialog;
pub use command_line::*;
pub use status_line::*;
pub use background::*;
pub use dialog::*;

View File

@@ -5,7 +5,7 @@ use ratatui::{
style::Style,
Frame,
};
use crate::config::colors::Theme;
use crate::config::colors::themes::Theme;
pub fn render_background(f: &mut Frame, area: Rect, theme: &Theme) {
let background = Block::default()

View File

@@ -5,7 +5,7 @@ use ratatui::{
layout::Rect,
Frame,
};
use crate::config::colors::Theme;
use crate::config::colors::themes::Theme;
pub fn render_command_line(f: &mut Frame, area: Rect, input: &str, active: bool, theme: &Theme, message: &str) {
let prompt = if active {

View File

@@ -0,0 +1,101 @@
// src/components/common/dialog.rs
use ratatui::{
layout::{Constraint, Direction, Layout, Rect, Margin},
style::{Modifier, Style},
widgets::{Block, BorderType, Borders, Paragraph},
Frame,
text::{Text, Line, Span}
};
use ratatui::prelude::Alignment;
use crate::config::colors::themes::Theme;
pub fn render_dialog(
f: &mut Frame,
area: Rect,
theme: &Theme,
title: &str,
message: &str,
is_active: bool,
) {
// Create a centered rect for the dialog
let dialog_area = centered_rect(60, 25, area);
// Main dialog container
let block = Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(Style::default().fg(theme.accent))
.title(title)
.style(Style::default().bg(theme.bg));
f.render_widget(&block, dialog_area);
// Inner content area
let inner_area = block.inner(dialog_area).inner(Margin {
horizontal: 2,
vertical: 1,
});
// Split into message and button areas
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Min(3), // Message content
Constraint::Length(3), // Button
])
.split(inner_area);
// Message text
let message_text = Text::from(message.lines().map(|l| Line::from(Span::styled(
l,
Style::default().fg(theme.fg)
))).collect::<Vec<_>>());
let message_paragraph = Paragraph::new(message_text)
.alignment(Alignment::Center);
f.render_widget(message_paragraph, chunks[0]);
// OK Button
let button_style = if is_active {
Style::default()
.fg(theme.highlight)
.add_modifier(Modifier::BOLD)
} else {
Style::default().fg(theme.fg)
};
let button_block = Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Plain)
.border_style(Style::default().fg(theme.accent))
.style(Style::default().bg(theme.bg));
f.render_widget(
Paragraph::new("OK")
.block(button_block)
.style(button_style)
.alignment(Alignment::Center),
chunks[1],
);
}
/// Helper function to center a rect with given percentage values
fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
let popup_layout = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Percentage((100 - percent_y) / 2),
Constraint::Percentage(percent_y),
Constraint::Percentage((100 - percent_y) / 2),
])
.split(r);
Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Percentage((100 - percent_x) / 2),
Constraint::Percentage(percent_x),
Constraint::Percentage((100 - percent_x) / 2),
])
.split(popup_layout[1])[1]
}

View File

@@ -6,7 +6,7 @@ use ratatui::{
Frame,
text::{Line, Span},
};
use crate::config::colors::Theme;
use crate::config::colors::themes::Theme;
use std::path::Path;
pub fn render_status_line(

View File

@@ -0,0 +1,4 @@
// src/components/form.rs
pub mod form;
pub use form::*;

View File

@@ -1,18 +1,18 @@
// src/components/handlers/form.rs
// src/components/form/form.rs
use ratatui::{
widgets::{Paragraph, Block, Borders},
layout::{Layout, Constraint, Direction, Rect, Margin, Alignment},
style::Style,
Frame,
};
use crate::config::colors::Theme;
use crate::ui::form::FormState;
use super::canvas::render_canvas; // Changed to canvas
use crate::config::colors::themes::Theme;
use crate::state::canvas_state::CanvasState;
use crate::components::handlers::canvas::render_canvas;
pub fn render_form(
f: &mut Frame,
area: Rect,
form_state: &FormState,
form_state: &impl CanvasState,
fields: &[&str],
current_field: &usize,
inputs: &[&String],

View File

@@ -1,8 +1,6 @@
// src/components/handlers.rs
pub mod form;
pub mod canvas;
pub mod sidebar;
pub use form::*;
pub use canvas::*;
pub use sidebar::*;

View File

@@ -7,13 +7,13 @@ use ratatui::{
Frame,
prelude::Alignment,
};
use crate::config::colors::Theme;
use crate::ui::form::FormState;
use crate::config::colors::themes::Theme;
use crate::state::canvas_state::CanvasState;
pub fn render_canvas(
f: &mut Frame,
area: Rect,
form_state: &FormState,
form_state: &impl CanvasState,
fields: &[&str],
current_field: &usize,
inputs: &[&String],
@@ -30,7 +30,7 @@ pub fn render_canvas(
let input_container = Block::default()
.borders(Borders::ALL)
.border_style(if is_edit_mode {
form_state.has_unsaved_changes.then(|| theme.warning).unwrap_or(theme.accent)
form_state.has_unsaved_changes().then(|| theme.warning).unwrap_or(theme.accent)
} else {
theme.secondary
})
@@ -81,7 +81,7 @@ pub fn render_canvas(
f.render_widget(input_display, input_rows[i]);
if is_active {
let cursor_x = input_rows[i].x + form_state.current_cursor_pos as u16;
let cursor_x = input_rows[i].x + form_state.current_cursor_pos() as u16;
let cursor_y = input_rows[i].y;
f.set_cursor_position((cursor_x, cursor_y));
}

View File

@@ -5,11 +5,12 @@ use ratatui::{
style::Style,
Frame,
};
use crate::config::colors::Theme;
use crate::config::colors::themes::Theme;
use common::proto::multieko2::table_definition::{ProfileTreeResponse};
use ratatui::text::{Span, Line};
const SIDEBAR_WIDTH: u16 = 16;
// Reduced sidebar width
const SIDEBAR_WIDTH: u16 = 12;
pub fn calculate_sidebar_layout(show_sidebar: bool, main_content_area: Rect) -> (Option<Rect>, Rect) {
if show_sidebar {
@@ -26,47 +27,83 @@ pub fn calculate_sidebar_layout(show_sidebar: bool, main_content_area: Rect) ->
}
}
pub fn render_sidebar(f: &mut Frame, area: Rect, theme: &Theme, profile_tree: &ProfileTreeResponse) {
let sidebar_block = Block::default()
.style(Style::default().bg(theme.bg));
pub fn render_sidebar(
f: &mut Frame,
area: Rect,
theme: &Theme,
profile_tree: &ProfileTreeResponse,
selected_profile: &Option<String>,
) {
let sidebar_block = Block::default().style(Style::default().bg(theme.bg));
let mut items = Vec::new();
for profile in &profile_tree.profiles {
// Profile header
items.push(ListItem::new(Line::from(vec![
Span::styled("📁 ", Style::default().fg(theme.accent)),
Span::styled(&profile.name, Style::default().fg(theme.highlight)),
])));
// Profile tables
for (table_idx, table) in profile.tables.iter().enumerate() {
let is_last_table = table_idx == profile.tables.len() - 1;
let tree_prefix = if is_last_table { "└─ " } else { "├─ " };
// Table name
if let Some(profile_name) = selected_profile {
// Existing code for when a profile is selected...
} else {
// Show full profile tree when no profile is selected (compact version)
for (profile_idx, profile) in profile_tree.profiles.iter().enumerate() {
// Profile header - more compact
items.push(ListItem::new(Line::from(vec![
Span::styled(format!(" {}", tree_prefix), Style::default().fg(theme.fg)),
Span::styled(&table.name, Style::default().fg(theme.fg)),
Span::styled("", Style::default().fg(theme.accent)),
Span::styled(&profile.name, Style::default().fg(theme.highlight)),
])));
// Dependencies
if !table.depends_on.is_empty() {
let dep_prefix = if is_last_table { " " } else { "" };
let deps = table.depends_on.join(", ");
// Tables with compact prefixes
for (table_idx, table) in profile.tables.iter().enumerate() {
let is_last_table = table_idx == profile.tables.len() - 1;
let is_last_profile = profile_idx == profile_tree.profiles.len() - 1;
items.push(ListItem::new(Line::from(vec![
Span::styled(format!(" {} └─ ", dep_prefix), Style::default().fg(theme.secondary)),
Span::styled(format!("{}", deps), Style::default().fg(theme.secondary)),
])));
// Shorter prefix characters
let prefix = match (is_last_profile, is_last_table) {
(true, true) => "",
(true, false) => "",
(false, true) => "│└",
(false, false) => "│├",
};
// Get table name without year prefix to save space
let display_name = if table.name.starts_with("2025_") {
&table.name[5..] // Skip "2025_" prefix
} else {
&table.name
};
let mut line = vec![
Span::styled(prefix, Style::default().fg(theme.fg)),
Span::styled(display_name, Style::default().fg(theme.fg)),
];
// Show a simple indicator for dependencies instead of listing them
if !table.depends_on.is_empty() {
line.push(Span::styled(
"",
Style::default().fg(theme.secondary)
));
}
items.push(ListItem::new(Line::from(line)));
}
// Compact separator between profiles
if profile_idx < profile_tree.profiles.len() - 1 {
items.push(ListItem::new(Line::from(
Span::styled("", Style::default().fg(theme.secondary))
)));
}
}
if profile_tree.profiles.is_empty() {
items.push(ListItem::new(Span::styled(
"No profiles",
Style::default().fg(theme.secondary)
)));
}
}
let list = List::new(items)
.block(sidebar_block)
.highlight_style(Style::default().fg(theme.highlight))
.highlight_symbol(">>");
.highlight_symbol(">");
f.render_widget(list, area);
}

View File

@@ -1,4 +1,4 @@
// src/components/handlers/intro.rs
// src/components/intro/intro.rs
use ratatui::{
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::Style,
@@ -7,7 +7,7 @@ use ratatui::{
prelude::Margin,
Frame,
};
use crate::config::colors::Theme;
use crate::config::colors::themes::Theme;
pub struct IntroState {
pub selected_option: usize,
@@ -32,9 +32,9 @@ impl IntroState {
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Percentage(35),
Constraint::Length(5),
Constraint::Percentage(35),
Constraint::Percentage(40),
Constraint::Length(5), // Increased to accommodate 3 buttons
Constraint::Percentage(40),
])
.split(inner_area);
@@ -48,10 +48,14 @@ impl IntroState {
.alignment(Alignment::Center);
f.render_widget(title_para, chunks[1]);
// Buttons
// Buttons - now with 3 options
let button_area = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
.constraints([
Constraint::Percentage(33),
Constraint::Percentage(33),
Constraint::Percentage(33),
])
.split(chunks[1].inner(Margin {
horizontal: 1,
vertical: 1
@@ -71,6 +75,13 @@ impl IntroState {
self.selected_option == 1,
theme,
);
self.render_button(
f,
button_area[2],
"Login",
self.selected_option == 2,
theme,
);
}
fn render_button(&self, f: &mut Frame, area: Rect, text: &str, selected: bool, theme: &Theme) {
@@ -100,11 +111,11 @@ impl IntroState {
f.render_widget(button, area);
}
pub fn next_option(&mut self) {
self.selected_option = (self.selected_option + 1) % 2;
pub fn next_option(&mut self) {
self.selected_option = (self.selected_option + 1) % 3;
}
pub fn previous_option(&mut self) {
self.selected_option = if self.selected_option == 0 { 1 } else { 0 };
self.selected_option = if self.selected_option == 0 { 2 } else { self.selected_option - 1 };
}
}

View File

@@ -3,8 +3,12 @@ pub mod handlers;
pub mod intro;
pub mod admin;
pub mod common;
pub mod form;
pub mod auth;
pub use handlers::*;
pub use intro::*;
pub use admin::*;
pub use common::*;
pub use form::*;
pub use auth::*;

View File

@@ -0,0 +1,7 @@
// src/config/binds.rs
pub mod config;
pub mod key_sequences;
pub use config::*;
pub use key_sequences::*;

View File

@@ -1,4 +1,4 @@
// client/src/config/config.rs
// src/config/binds/config.rs
use serde::Deserialize;
use std::collections::HashMap;
@@ -25,6 +25,8 @@ pub struct Config {
#[derive(Debug, Deserialize)]
pub struct ModeKeybindings {
#[serde(default)]
pub general: HashMap<String, Vec<String>>,
#[serde(default)]
pub read_only: HashMap<String, Vec<String>>,
#[serde(default)]
@@ -33,7 +35,6 @@ pub struct ModeKeybindings {
pub command: HashMap<String, Vec<String>>,
#[serde(default)]
pub common: HashMap<String, Vec<String>>,
// Store top-level keybindings that aren't in a specific mode section
#[serde(flatten)]
pub global: HashMap<String, Vec<String>>,
}
@@ -49,6 +50,17 @@ impl Config {
Ok(config)
}
pub fn get_general_action(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> {
self.get_action_for_key_in_mode(&self.keybindings.general, key, modifiers)
.or_else(|| self.get_action_for_key_in_mode(&self.keybindings.global, key, modifiers))
}
/// Common actions for Edit/Read-only modes
pub fn get_common_action(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> {
self.get_action_for_key_in_mode(&self.keybindings.common, key, modifiers)
}
/// Gets an action for a key in Read-Only mode, also checking common keybindings.
pub fn get_read_only_action_for_key(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> {
self.get_action_for_key_in_mode(&self.keybindings.read_only, key, modifiers)
@@ -70,6 +82,25 @@ impl Config {
.or_else(|| self.get_action_for_key_in_mode(&self.keybindings.global, key, modifiers))
}
/// Context-aware keybinding resolution
pub fn get_action_for_current_context(
&self,
is_edit_mode: bool,
command_mode: bool,
key: KeyCode,
modifiers: KeyModifiers
) -> Option<&str> {
match (command_mode, is_edit_mode) {
(true, _) => self.get_command_action_for_key(key, modifiers),
(_, true) => self.get_edit_action_for_key(key, modifiers)
.or_else(|| self.get_common_action(key, modifiers)),
_ => self.get_read_only_action_for_key(key, modifiers)
.or_else(|| self.get_common_action(key, modifiers))
// Add global bindings check for read-only mode
.or_else(|| self.get_action_for_key_in_mode(&self.keybindings.global, key, modifiers)),
}
}
/// Helper function to get an action for a key in a specific mode.
pub fn get_action_for_key_in_mode<'a>(
&self,
@@ -355,13 +386,13 @@ impl Config {
// Get string representations of the sequence
let sequence_str = sequence.iter()
.map(|k| crate::config::key_sequences::key_to_string(k))
.map(|k| crate::config::binds::key_sequences::key_to_string(k))
.collect::<Vec<String>>()
.join("");
// Add the missing sequence_plus definition
let sequence_plus = sequence.iter()
.map(|k| crate::config::key_sequences::key_to_string(k))
.map(|k| crate::config::binds::key_sequences::key_to_string(k))
.collect::<Vec<String>>()
.join("+");
@@ -414,7 +445,7 @@ impl Config {
// Special case for + format in bindings
if binding.contains('+') {
let normalized_sequence = sequence.iter()
.map(|k| crate::config::key_sequences::key_to_string(k))
.map(|k| crate::config::binds::key_sequences::key_to_string(k))
.collect::<Vec<String>>();
let binding_parts: Vec<&str> = binding.split('+').collect();
@@ -442,7 +473,7 @@ impl Config {
// Get string representation of the sequence
let sequence_str = sequence.iter()
.map(|k| crate::config::key_sequences::key_to_string(k))
.map(|k| crate::config::binds::key_sequences::key_to_string(k))
.collect::<Vec<String>>()
.join("");
@@ -491,7 +522,7 @@ impl Config {
if binding.contains('+') {
let binding_parts: Vec<&str> = binding.split('+').collect();
let sequence_parts = sequence.iter()
.map(|k| crate::config::key_sequences::key_to_string(k))
.map(|k| crate::config::binds::key_sequences::key_to_string(k))
.collect::<Vec<String>>();
if binding_parts.len() > sequence_parts.len() {

View File

@@ -1,68 +1,4 @@
// src/client/colors.rs
use ratatui::style::Color;
// src/config/colors.rs
pub mod themes;
#[derive(Debug, Clone)]
pub struct Theme {
pub bg: Color,
pub fg: Color,
pub accent: Color,
pub secondary: Color,
pub highlight: Color,
pub warning: Color,
pub border: Color,
}
impl Theme {
pub fn from_str(theme_name: &str) -> Self {
match theme_name.to_lowercase().as_str() {
"dark" => Self::dark(),
"high_contrast" => Self::high_contrast(),
_ => Self::light(),
}
}
// Default light theme
pub fn light() -> Self {
Self {
bg: Color::Rgb(245, 245, 245), // Light gray
fg: Color::Rgb(64, 64, 64), // Dark gray
accent: Color::Rgb(173, 216, 230), // Pastel blue
secondary: Color::Rgb(255, 165, 0), // Orange for secondary
highlight: Color::Rgb(152, 251, 152), // Pastel green
warning: Color::Rgb(255, 182, 193), // Pastel pink
border: Color::Rgb(220, 220, 220), // Light gray border
}
}
// High-contrast dark theme
pub fn dark() -> Self {
Self {
bg: Color::Rgb(30, 30, 30), // Dark background
fg: Color::Rgb(255, 255, 255), // White text
accent: Color::Rgb(0, 191, 255), // Bright blue
secondary: Color::Rgb(255, 215, 0), // Gold for secondary
highlight: Color::Rgb(50, 205, 50), // Bright green
warning: Color::Rgb(255, 99, 71), // Bright red
border: Color::Rgb(100, 100, 100), // Medium gray border
}
}
// High-contrast light theme
pub fn high_contrast() -> Self {
Self {
bg: Color::Rgb(255, 255, 255), // White background
fg: Color::Rgb(0, 0, 0), // Black text
accent: Color::Rgb(0, 0, 255), // Blue
secondary: Color::Rgb(255, 140, 0), // Dark orange for secondary
highlight: Color::Rgb(0, 128, 0), // Green
warning: Color::Rgb(255, 0, 0), // Red
border: Color::Rgb(0, 0, 0), // Black border
}
}
}
impl Default for Theme {
fn default() -> Self {
Self::light() // Default to light theme
}
}
pub use themes::*;

View File

@@ -0,0 +1,68 @@
// src/client/themes/colors.rs
use ratatui::style::Color;
#[derive(Debug, Clone)]
pub struct Theme {
pub bg: Color,
pub fg: Color,
pub accent: Color,
pub secondary: Color,
pub highlight: Color,
pub warning: Color,
pub border: Color,
}
impl Theme {
pub fn from_str(theme_name: &str) -> Self {
match theme_name.to_lowercase().as_str() {
"dark" => Self::dark(),
"high_contrast" => Self::high_contrast(),
_ => Self::light(),
}
}
// Default light theme
pub fn light() -> Self {
Self {
bg: Color::Rgb(245, 245, 245), // Light gray
fg: Color::Rgb(64, 64, 64), // Dark gray
accent: Color::Rgb(173, 216, 230), // Pastel blue
secondary: Color::Rgb(255, 165, 0), // Orange for secondary
highlight: Color::Rgb(152, 251, 152), // Pastel green
warning: Color::Rgb(255, 182, 193), // Pastel pink
border: Color::Rgb(220, 220, 220), // Light gray border
}
}
// High-contrast dark theme
pub fn dark() -> Self {
Self {
bg: Color::Rgb(30, 30, 30), // Dark background
fg: Color::Rgb(255, 255, 255), // White text
accent: Color::Rgb(0, 191, 255), // Bright blue
secondary: Color::Rgb(255, 215, 0), // Gold for secondary
highlight: Color::Rgb(50, 205, 50), // Bright green
warning: Color::Rgb(255, 99, 71), // Bright red
border: Color::Rgb(100, 100, 100), // Medium gray border
}
}
// High-contrast light theme
pub fn high_contrast() -> Self {
Self {
bg: Color::Rgb(255, 255, 255), // White background
fg: Color::Rgb(0, 0, 0), // Black text
accent: Color::Rgb(0, 0, 255), // Blue
secondary: Color::Rgb(255, 140, 0), // Dark orange for secondary
highlight: Color::Rgb(0, 128, 0), // Green
warning: Color::Rgb(255, 0, 0), // Red
border: Color::Rgb(0, 0, 0), // Black border
}
}
}
impl Default for Theme {
fn default() -> Self {
Self::light() // Default to light theme
}
}

View File

@@ -1,4 +1,4 @@
// src/config/mod.rs
pub mod binds;
pub mod colors;
pub mod config;
pub mod key_sequences;

View File

@@ -5,6 +5,7 @@ pub mod config;
pub mod state;
pub mod components;
pub mod modes;
pub mod services;
pub use ui::run_ui;

View File

@@ -0,0 +1,4 @@
// src/client/modes/canvas.rs
pub mod edit;
pub mod common;
pub mod read_only;

View File

@@ -0,0 +1,75 @@
// src/modes/canvas/common.rs
use crate::tui::terminal::core::TerminalCore;
use crate::state::pages::{form::FormState, auth::AuthState};
use crate::state::state::AppState;
use crate::services::grpc_client::GrpcClient;
use crate::services::auth::AuthClient;
use crate::tui::functions::common::{
form::{save as form_save, revert},
login::{save as login_save, cancel}
};
pub async fn handle_core_action(
action: &str,
form_state: &mut FormState,
auth_state: &mut AuthState,
grpc_client: &mut GrpcClient,
auth_client: &mut AuthClient,
terminal: &mut TerminalCore,
app_state: &mut AppState,
current_position: &mut u64,
total_count: u64,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
match action {
"save" => {
if app_state.ui.show_login {
let message = login_save(auth_state, auth_client, app_state).await?;
Ok((false, message))
} else {
let message = form_save(
form_state,
grpc_client,
&mut app_state.ui.is_saved,
current_position,
total_count,
).await?;
Ok((false, message))
}
},
"force_quit" => {
terminal.cleanup()?;
Ok((true, "Force exiting without saving.".to_string()))
},
"save_and_quit" => {
let message = if app_state.ui.show_login {
login_save(auth_state, auth_client, app_state).await?
} else {
form_save(
form_state,
grpc_client,
&mut app_state.ui.is_saved,
current_position,
total_count,
).await?
};
terminal.cleanup()?;
Ok((true, format!("{}. Exiting application.", message)))
},
"revert" => {
if app_state.ui.show_login {
let message = cancel(auth_state, app_state).await;
Ok((false, message))
} else {
let message = revert(
form_state,
grpc_client,
current_position,
total_count,
).await?;
Ok((false, message))
}
},
_ => Ok((false, format!("Core action not handled: {}", action))),
}
}

View File

@@ -1,12 +1,11 @@
// src/modes/handlers/edit.rs
// src/modes/canvas/edit.rs
// TODO THIS is freaking bloated with functions it never uses REFACTOR 200 LOC can be gone
use crossterm::event::{KeyEvent, KeyCode, KeyModifiers};
use crate::tui::terminal::{
grpc_client::GrpcClient,
};
use crate::config::config::Config;
use crate::ui::handlers::form::FormState;
use super::common;
use crate::config::binds::config::Config;
use crate::state::pages::form::FormState;
use crate::services::grpc_client::GrpcClient;
use crate::tui::functions::common::form::{save, revert};
pub async fn handle_edit_event_internal(
key: KeyEvent,
@@ -19,6 +18,24 @@ pub async fn handle_edit_event_internal(
total_count: u64,
grpc_client: &mut GrpcClient,
) -> Result<String, Box<dyn std::error::Error>> {
if let Some("enter_command_mode") = config.get_action_for_key_in_mode(&config.keybindings.global, key.code, key.modifiers) {
// Ignore in edit mode and process as normal input
handle_edit_specific_input(key, form_state, ideal_cursor_column);
return Ok(command_message.clone());
}
// Check common actions first
if let Some(action) = config.get_action_for_key_in_mode(&config.keybindings.common, key.code, key.modifiers) {
return execute_common_action(
action,
form_state,
grpc_client,
is_saved,
current_position,
total_count,
).await;
}
if let Some(action) = config.get_edit_action_for_key(key.code, key.modifiers) {
return execute_edit_action(
action,
@@ -40,6 +57,48 @@ pub async fn handle_edit_event_internal(
Ok(command_message.clone())
}
async fn execute_common_action(
action: &str,
form_state: &mut FormState,
grpc_client: &mut GrpcClient,
is_saved: &mut bool,
current_position: &mut u64,
total_count: u64,
) -> Result<String, Box<dyn std::error::Error>> {
match action {
"save" => {
save(
form_state,
grpc_client,
is_saved,
current_position,
total_count,
).await
},
"revert" => {
revert(
form_state,
grpc_client,
current_position,
total_count,
).await
},
"move_up" | "move_down" => {
// Reuse edit mode's existing logic
execute_edit_action(
action,
form_state,
&mut 0, // Dummy ideal_cursor_column (not used here)
grpc_client,
is_saved,
current_position,
total_count,
).await
},
_ => Ok(format!("Common action not handled: {}", action)),
}
}
fn handle_edit_specific_input(
key: KeyEvent,
form_state: &mut FormState,
@@ -118,7 +177,7 @@ async fn execute_edit_action(
) -> Result<String, Box<dyn std::error::Error>> {
match action {
"save" => {
common::save(
save(
form_state,
grpc_client, // Changed from AppTerminal
is_saved,

View File

@@ -1,10 +1,11 @@
// src/modes/handlers/read_only.rs
// src/modes/canvas/read_only.rs
use crossterm::event::{KeyEvent};
use crate::config::config::Config;
use crate::ui::handlers::form::FormState;
use crate::config::key_sequences::KeySequenceTracker;
use crate::tui::terminal::grpc_client::GrpcClient;
use crate::config::binds::config::Config;
use crate::state::pages::form::FormState;
use crate::state::pages::auth::AuthState;
use crate::services::grpc_client::GrpcClient;
use crate::config::binds::key_sequences::KeySequenceTracker;
#[derive(PartialEq)]
enum CharType {
@@ -14,9 +15,11 @@ enum CharType {
}
pub async fn handle_read_only_event(
app_state: &crate::state::state::AppState,
key: KeyEvent,
config: &Config,
form_state: &mut FormState,
auth_state: &mut AuthState,
key_sequence_tracker: &mut KeySequenceTracker,
current_position: &mut u64,
total_count: u64,
@@ -50,16 +53,34 @@ pub async fn handle_read_only_event(
// Try to match the current sequence against Read-Only mode bindings
if let Some(action) = config.matches_key_sequence_generalized(&sequence) {
let result = execute_action(
action,
form_state,
ideal_cursor_column,
key_sequence_tracker,
command_message,
current_position,
total_count,
grpc_client,
).await?;
let result = if (action == "previous_entry" || action == "next_entry" ||
action == "move_up" || action == "move_down") && app_state.ui.show_form {
crate::tui::functions::form::handle_action(
action,
form_state,
grpc_client,
current_position,
total_count,
ideal_cursor_column,
).await?
} else if (action == "move_up" || action == "move_down") && app_state.ui.show_login {
crate::tui::functions::login::handle_action(
action,
auth_state,
ideal_cursor_column,
).await?
} else {
execute_action(
action,
form_state,
ideal_cursor_column,
key_sequence_tracker,
command_message,
current_position,
total_count,
grpc_client,
).await?
};
key_sequence_tracker.reset();
return Ok((false, result));
}
@@ -72,16 +93,27 @@ pub async fn handle_read_only_event(
// Since it's not part of a multi-key sequence, check for a direct action
if sequence.len() == 1 && !config.is_key_sequence_prefix(&sequence) {
if let Some(action) = config.get_read_only_action_for_key(key.code, key.modifiers) {
let result = execute_action(
action,
form_state,
ideal_cursor_column,
key_sequence_tracker,
command_message,
current_position,
total_count,
grpc_client,
).await?;
let result = if action == "previous_entry" && app_state.ui.show_form {
crate::tui::functions::form::handle_action(
action,
form_state,
grpc_client,
current_position,
total_count,
ideal_cursor_column,
).await?
} else {
execute_action(
action,
form_state,
ideal_cursor_column,
key_sequence_tracker,
command_message,
current_position,
total_count,
grpc_client,
).await?
};
key_sequence_tracker.reset();
return Ok((false, result));
}
@@ -91,16 +123,27 @@ pub async fn handle_read_only_event(
key_sequence_tracker.reset();
if let Some(action) = config.get_read_only_action_for_key(key.code, key.modifiers) {
let result = execute_action(
action,
form_state,
ideal_cursor_column,
key_sequence_tracker,
command_message,
current_position,
total_count,
grpc_client,
).await?;
let result = if action == "previous_entry" && app_state.ui.show_form {
crate::tui::functions::form::handle_action(
action,
form_state,
grpc_client,
current_position,
total_count,
ideal_cursor_column,
).await?
} else {
execute_action(
action,
form_state,
ideal_cursor_column,
key_sequence_tracker,
command_message,
current_position,
total_count,
grpc_client,
).await?
};
return Ok((false, result));
}
}
@@ -128,72 +171,15 @@ async fn execute_action(
grpc_client: &mut GrpcClient,
) -> Result<String, Box<dyn std::error::Error>> {
match action {
"previous_entry" => {
let new_position = current_position.saturating_sub(1);
if new_position >= 1 {
*current_position = new_position;
match grpc_client.get_adresar_by_position(*current_position).await {
Ok(response) => {
form_state.id = response.id;
form_state.values = vec![
response.firma, response.kz, response.drc,
response.ulica, response.psc, response.mesto,
response.stat, response.banka, response.ucet,
response.skladm, response.ico, response.kontakt,
response.telefon, response.skladu, response.fax,
];
let current_input = form_state.get_current_input();
let max_cursor_pos = if !current_input.is_empty() {
current_input.len() - 1
} else { 0 };
form_state.current_cursor_pos = std::cmp::min(*ideal_cursor_column, max_cursor_pos);
form_state.has_unsaved_changes = false;
*command_message = format!("Loaded entry {}", *current_position);
}
Err(e) => {
*command_message = format!("Error loading entry: {}", e);
}
}
key_sequence_tracker.reset();
}
Ok(command_message.clone())
"previous_entry" | "next_entry" => {
// This will only be called when no component is active
key_sequence_tracker.reset();
Ok(format!("Navigation prev/next only available in form mode"))
}
"next_entry" => {
if *current_position <= total_count {
*current_position += 1;
if *current_position <= total_count {
match grpc_client.get_adresar_by_position(*current_position).await {
Ok(response) => {
form_state.id = response.id;
form_state.values = vec![
response.firma, response.kz, response.drc,
response.ulica, response.psc, response.mesto,
response.stat, response.banka, response.ucet,
response.skladm, response.ico, response.kontakt,
response.telefon, response.skladu, response.fax,
];
let current_input = form_state.get_current_input();
let max_cursor_pos = if !current_input.is_empty() {
current_input.len() - 1
} else { 0 };
form_state.current_cursor_pos = std::cmp::min(*ideal_cursor_column, max_cursor_pos);
form_state.has_unsaved_changes = false;
*command_message = format!("Loaded entry {}", *current_position);
}
Err(e) => {
*command_message = format!("Error loading entry: {}", e);
}
}
} else {
form_state.reset_to_empty();
form_state.current_field = 0;
form_state.current_cursor_pos = 0;
*ideal_cursor_column = 0;
*command_message = "New entry mode".to_string();
}
key_sequence_tracker.reset();
}
Ok(command_message.clone())
"move_up" | "move_down" => {
// This will only be called when no component is active
key_sequence_tracker.reset();
Ok(format!("Navigation up/down only available in form mode"))
}
"exit_edit_mode" => {
key_sequence_tracker.reset();
@@ -215,38 +201,6 @@ async fn execute_action(
}
Ok("".to_string())
}
"move_up" => {
// Change field first
if form_state.current_field == 0 {
form_state.current_field = form_state.fields.len() - 1;
} else {
form_state.current_field = form_state.current_field.saturating_sub(1);
}
// Get current input AFTER changing field
let current_input = form_state.get_current_input();
let max_cursor_pos = if !current_input.is_empty() {
current_input.len() - 1
} else {
0
};
form_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
Ok("".to_string())
}
"move_down" => {
// Change field first
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
// Get current input AFTER changing field
let current_input = form_state.get_current_input();
let max_cursor_pos = if !current_input.is_empty() {
current_input.len() - 1
} else {
0
};
form_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
Ok("".to_string())
}
"move_word_next" => {
let current_input = form_state.get_current_input();
if !current_input.is_empty() {

View File

@@ -0,0 +1,3 @@
// src/client/modes/common.rs
pub mod command_mode;
pub mod highlight;

View File

@@ -1,10 +1,12 @@
// src/modes/handlers/command_mode.rs
use crossterm::event::{KeyEvent, KeyCode, KeyModifiers};
use crate::tui::terminal::grpc_client::GrpcClient;
use crate::config::config::Config;
use crate::ui::handlers::form::FormState;
use super::common;
use crate::config::binds::config::Config;
use crate::services::grpc_client::GrpcClient;
use crate::state::pages::form::FormState;
use crate::tui::functions::common::commands::CommandHandler;
use crate::tui::terminal::core::TerminalCore;
use crate::tui::functions::common::form::{save, revert};
pub async fn handle_command_event(
key: KeyEvent,
@@ -13,7 +15,8 @@ pub async fn handle_command_event(
command_input: &mut String,
command_message: &mut String,
grpc_client: &mut GrpcClient,
is_saved: &mut bool,
command_handler: &mut CommandHandler,
terminal: &mut TerminalCore,
current_position: &mut u64,
total_count: u64,
) -> Result<(bool, String, bool), Box<dyn std::error::Error>> {
@@ -35,7 +38,8 @@ pub async fn handle_command_event(
command_input,
command_message,
grpc_client,
is_saved,
command_handler,
terminal,
current_position,
total_count,
).await;
@@ -66,7 +70,8 @@ async fn process_command(
command_input: &mut String,
command_message: &mut String,
grpc_client: &mut GrpcClient,
is_saved: &mut bool,
command_handler: &mut CommandHandler,
terminal: &mut TerminalCore,
current_position: &mut u64,
total_count: u64,
) -> Result<(bool, String, bool), Box<dyn std::error::Error>> {
@@ -82,34 +87,26 @@ async fn process_command(
.unwrap_or("unknown");
match action {
"force_quit" | "save_and_quit" | "quit" => {
let (should_exit, message) = command_handler
.handle_command(action, terminal)
.await?;
command_input.clear();
Ok((should_exit, message, true))
},
"save" => {
let message = common::save(
let message = save(
form_state,
grpc_client,
is_saved,
&mut command_handler.is_saved,
current_position,
total_count,
).await?;
command_input.clear();
return Ok((false, message, true));
},
"force_quit" => {
let (should_exit, message) = common::force_quit();
command_input.clear();
return Ok((should_exit, message, true));
},
"save_and_quit" => {
let (should_exit, message) = common::save_and_quit(
form_state,
grpc_client,
current_position,
total_count,
).await?;
command_input.clear();
return Ok((should_exit, message, true));
},
"revert" => {
let message = common::revert(
let message = revert(
form_state,
grpc_client,
current_position,

View File

View File

@@ -0,0 +1,2 @@
// src/client/modes/general.rs
pub mod navigation;

View File

@@ -0,0 +1,156 @@
// src/modes/general/navigation.rs
use crossterm::event::KeyEvent;
use crate::config::binds::config::Config;
use crate::state::state::AppState;
use crate::state::pages::form::FormState;
use crate::tui::functions::{intro, admin};
pub async fn handle_navigation_event(
key: KeyEvent,
config: &Config,
form_state: &mut FormState,
app_state: &mut AppState,
command_mode: &mut bool,
command_input: &mut String,
command_message: &mut String,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
if let Some(action) = config.get_general_action(key.code, key.modifiers) {
match action {
"move_up" => {
move_up(app_state);
return Ok((false, String::new()));
}
"move_down" => {
move_down(app_state);
return Ok((false, String::new()));
}
"next_option" => {
next_option(app_state); // Intro has 2 options
return Ok((false, String::new()));
}
"previous_option" => {
previous_option(app_state);
return Ok((false, String::new()));
}
"select" => {
select(app_state);
return Ok((false, "Selected".to_string()));
}
"toggle_sidebar" => {
toggle_sidebar(app_state);
return Ok((false, format!("Sidebar {}",
if app_state.ui.show_sidebar { "shown" } else { "hidden" }
)));
}
"next_field" => {
next_field(form_state);
return Ok((false, String::new()));
}
"prev_field" => {
prev_field(form_state);
return Ok((false, String::new()));
}
"enter_command_mode" => {
handle_enter_command_mode(command_mode, command_input, command_message);
return Ok((false, String::new()));
}
_ => {}
}
}
Ok((false, String::new()))
}
pub fn move_up(app_state: &mut AppState) {
if app_state.ui.show_intro {
app_state.ui.intro_state.previous_option();
} else if app_state.ui.show_admin {
// Assuming profile_tree.profiles is the list we're navigating
let profile_count = app_state.profile_tree.profiles.len();
if profile_count == 0 {
return;
}
// Use general state for tracking selection in admin panel
if app_state.general.selected_item == 0 {
app_state.general.selected_item = profile_count - 1;
} else {
app_state.general.selected_item = app_state.general.selected_item.saturating_sub(1);
}
}
}
pub fn move_down(app_state: &mut AppState) {
if app_state.ui.show_intro {
app_state.ui.intro_state.next_option();
} else if app_state.ui.show_admin {
// Assuming profile_tree.profiles is the list we're navigating
let profile_count = app_state.profile_tree.profiles.len();
if profile_count == 0 {
return;
}
app_state.general.selected_item = (app_state.general.selected_item + 1) % profile_count;
}
}
pub fn next_option(app_state: &mut AppState) { // Remove option_count parameter
if app_state.ui.show_intro {
app_state.ui.intro_state.next_option();
} else {
// Get option count from state instead of parameter
let option_count = app_state.profile_tree.profiles.len();
app_state.general.current_option = (app_state.general.current_option + 1) % option_count;
}
}
pub fn previous_option(app_state: &mut AppState) {
if app_state.ui.show_intro {
app_state.ui.intro_state.previous_option();
} else {
let option_count = app_state.profile_tree.profiles.len();
app_state.general.current_option = if app_state.general.current_option == 0 {
option_count.saturating_sub(1) // Wrap to last option
} else {
app_state.general.current_option - 1
};
}
}
pub fn select(app_state: &mut AppState) {
if app_state.ui.show_intro {
intro::handle_intro_selection(app_state);
} else if app_state.ui.show_admin {
admin::handle_admin_selection(app_state);
}
}
pub fn toggle_sidebar(app_state: &mut AppState) {
app_state.ui.show_sidebar = !app_state.ui.show_sidebar;
}
pub fn next_field(form_state: &mut FormState) {
if !form_state.fields.is_empty() {
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
}
}
pub fn prev_field(form_state: &mut FormState) {
if !form_state.fields.is_empty() {
if form_state.current_field == 0 {
form_state.current_field = form_state.fields.len() - 1;
} else {
form_state.current_field -= 1;
}
}
}
pub fn handle_enter_command_mode(
command_mode: &mut bool,
command_input: &mut String,
command_message: &mut String
) {
*command_mode = true;
command_input.clear();
command_message.clear();
}

View File

@@ -1,6 +1,3 @@
// src/client/modes/handlers.rs
pub mod event;
pub mod edit;
pub mod common;
pub mod command_mode;
pub mod read_only;
pub mod mode_manager;

View File

@@ -1,17 +1,23 @@
// src/modes/handlers/event.rs
use crossterm::event::{Event, KeyCode};
use crossterm::event::Event;
use crossterm::cursor::SetCursorStyle;
use crate::tui::terminal::{
core::TerminalCore,
grpc_client::GrpcClient,
commands::CommandHandler,
};
use crate::config::config::Config;
use crate::ui::handlers::form::FormState;
use crate::services::grpc_client::GrpcClient;
use crate::services::auth::AuthClient;
use crate::tui::functions::common::commands::CommandHandler;
use crate::config::binds::config::Config;
use crate::state::pages::form::FormState;
use crate::state::pages::auth::AuthState;
use crate::ui::handlers::rat_state::UiStateHandler;
use crate::modes::handlers::{edit, command_mode, read_only};
use crate::config::key_sequences::KeySequenceTracker;
use super::common;
use crate::modes::{
common::{command_mode},
canvas::{edit, read_only, common},
general::navigation,
};
use crate::config::binds::key_sequences::KeySequenceTracker;
use crate::modes::handlers::mode_manager::{ModeManager, AppMode};
pub struct EventHandler {
pub command_mode: bool,
@@ -21,11 +27,13 @@ pub struct EventHandler {
pub edit_mode_cooldown: bool,
pub ideal_cursor_column: usize,
pub key_sequence_tracker: KeySequenceTracker,
pub auth_state: AuthState,
pub auth_client: AuthClient,
}
impl EventHandler {
pub fn new() -> Self {
EventHandler {
pub async fn new() -> Result<Self, Box<dyn std::error::Error>> {
Ok(EventHandler {
command_mode: false,
command_input: String::new(),
command_message: String::new(),
@@ -33,7 +41,9 @@ impl EventHandler {
edit_mode_cooldown: false,
ideal_cursor_column: 0,
key_sequence_tracker: KeySequenceTracker::new(800),
}
auth_state: AuthState::new(),
auth_client: AuthClient::new().await?,
})
}
pub async fn handle_event(
@@ -47,180 +57,202 @@ impl EventHandler {
app_state: &mut crate::state::state::AppState,
total_count: u64,
current_position: &mut u64,
intro_state: &mut crate::components::intro::intro::IntroState,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
if app_state.ui.show_intro {
if let Event::Key(key) = event {
match key.code {
KeyCode::Left => intro_state.previous_option(),
KeyCode::Right => intro_state.next_option(),
KeyCode::Enter => {
if intro_state.selected_option == 0 {
app_state.ui.show_intro = false;
} else {
app_state.ui.show_intro = false;
app_state.ui.show_admin = true;
}
return Ok((false, String::new()));
}
_ => {}
}
}
return Ok((false, String::new()));
}
// Determine current mode based on app state and event handler state
let current_mode = ModeManager::derive_mode(app_state, self);
app_state.update_mode(current_mode);
if let Event::Key(key) = event {
let key_code = key.code;
let modifiers = key.modifiers;
if UiStateHandler::toggle_sidebar(
&mut app_state.ui,
config,
key_code,
modifiers,
) {
// Handle common actions across all modes
if UiStateHandler::toggle_sidebar(&mut app_state.ui, config, key_code, modifiers) {
return Ok((false, format!("Sidebar {}",
if app_state.ui.show_sidebar { "shown" } else { "hidden" }
)));
}
if let Some(action) = config.get_action_for_key_in_mode(
&config.keybindings.common,
key_code,
modifiers
) {
match action {
"save" => {
let message = common::save(
form_state,
grpc_client,
&mut app_state.ui.is_saved,
current_position,
total_count,
).await?;
return Ok((false, message));
},
"force_quit" => {
let (should_exit, message) = command_handler.handle_command("force_quit", terminal).await?;
return Ok((should_exit, message));
},
"save_and_quit" => {
let (should_exit, message) = command_handler.handle_command("save_and_quit", terminal).await?;
return Ok((should_exit, message));
},
"revert" => {
let message = common::revert(
form_state,
grpc_client,
current_position,
total_count,
).await?;
return Ok((false, message));
},
_ => {}
}
}
// Mode-specific handling
match current_mode {
AppMode::General => {
return navigation::handle_navigation_event(
key,
config,
form_state,
app_state,
&mut self.command_mode,
&mut self.command_input,
&mut self.command_message,
).await;
},
if self.command_mode {
let (should_exit, message, exit_command_mode) = command_mode::handle_command_event(
key,
config,
form_state,
&mut self.command_input,
&mut self.command_message,
grpc_client,
&mut app_state.ui.is_saved,
current_position,
total_count,
).await?;
if exit_command_mode {
self.command_mode = false;
}
return Ok((should_exit, message));
}
if self.is_edit_mode {
if config.is_exit_edit_mode(key_code, modifiers) {
if form_state.has_unsaved_changes {
self.command_message = "Unsaved changes! Use :w to save or :q! to discard".to_string();
AppMode::ReadOnly => {
// Check for mode transitions first
if config.is_enter_edit_mode_before(key_code, modifiers) &&
ModeManager::can_enter_edit_mode(current_mode) {
self.is_edit_mode = true;
self.edit_mode_cooldown = true;
self.command_message = "Edit mode".to_string();
terminal.set_cursor_style(SetCursorStyle::BlinkingBar)?;
return Ok((false, self.command_message.clone()));
}
self.is_edit_mode = false;
self.edit_mode_cooldown = true;
self.command_message = "Read-only mode".to_string();
terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?;
let current_input = form_state.get_current_input();
if !current_input.is_empty() && form_state.current_cursor_pos >= current_input.len() {
form_state.current_cursor_pos = current_input.len() - 1;
self.ideal_cursor_column = form_state.current_cursor_pos;
if config.is_enter_edit_mode_after(key_code, modifiers) &&
ModeManager::can_enter_edit_mode(current_mode) {
let current_input = form_state.get_current_input();
if !current_input.is_empty() && form_state.current_cursor_pos < current_input.len() {
form_state.current_cursor_pos += 1;
self.ideal_cursor_column = form_state.current_cursor_pos;
}
self.is_edit_mode = true;
self.edit_mode_cooldown = true;
self.command_message = "Edit mode (after cursor)".to_string();
terminal.set_cursor_style(SetCursorStyle::BlinkingBar)?;
return Ok((false, self.command_message.clone()));
}
return Ok((false, self.command_message.clone()));
}
let result = edit::handle_edit_event_internal(
key,
config,
form_state,
&mut self.ideal_cursor_column,
&mut self.command_message,
&mut app_state.ui.is_saved,
current_position,
total_count,
grpc_client,
).await?;
self.key_sequence_tracker.reset();
return Ok((false, result));
} else {
if let Some(action) = config.get_read_only_action_for_key(key_code, modifiers) {
if action == "enter_command_mode" {
self.command_mode = true;
self.command_input.clear();
self.command_message.clear();
return Ok((false, String::new()));
// Check for entering command mode
if let Some(action) = config.get_read_only_action_for_key(key_code, modifiers) {
if action == "enter_command_mode" && ModeManager::can_enter_command_mode(current_mode) {
self.command_mode = true;
self.command_input.clear();
self.command_message.clear();
return Ok((false, String::new()));
}
}
}
if config.is_enter_edit_mode_before(key_code, modifiers) {
self.is_edit_mode = true;
self.edit_mode_cooldown = true;
self.command_message = "Edit mode".to_string();
terminal.set_cursor_style(SetCursorStyle::BlinkingBar)?;
return Ok((false, self.command_message.clone()));
}
if config.is_enter_edit_mode_after(key_code, modifiers) {
let current_input = form_state.get_current_input();
if !current_input.is_empty() && form_state.current_cursor_pos < current_input.len() {
form_state.current_cursor_pos += 1;
self.ideal_cursor_column = form_state.current_cursor_pos;
// Check for core application actions (save, quit, etc.)
// ONLY handle a limited subset of core actions here
if let Some(action) = config.get_action_for_key_in_mode(
&config.keybindings.common,
key_code,
modifiers
) {
match action {
"save" | "force_quit" | "save_and_quit" | "revert" => {
return common::handle_core_action(
action,
form_state,
&mut self.auth_state,
grpc_client,
&mut self.auth_client,
terminal,
app_state,
current_position,
total_count,
).await;
},
_ => {} // For other actions, let the mode-specific handler take care of it
}
}
self.is_edit_mode = true;
self.edit_mode_cooldown = true;
self.command_message = "Edit mode (after cursor)".to_string();
terminal.set_cursor_style(SetCursorStyle::BlinkingBar)?;
return Ok((false, self.command_message.clone()));
}
return read_only::handle_read_only_event(
key,
config,
form_state,
&mut self.key_sequence_tracker,
current_position,
total_count,
grpc_client,
&mut self.command_message,
&mut self.edit_mode_cooldown,
&mut self.ideal_cursor_column,
).await;
// Let read_only mode handle its own actions (including navigation from common bindings)
return read_only::handle_read_only_event(
&app_state,
key,
config,
form_state,
&mut self.auth_state,
&mut self.key_sequence_tracker,
current_position,
total_count,
grpc_client,
&mut self.command_message,
&mut self.edit_mode_cooldown,
&mut self.ideal_cursor_column,
).await;
},
AppMode::Edit => {
// Check for exiting edit mode
if config.is_exit_edit_mode(key_code, modifiers) {
if form_state.has_unsaved_changes {
self.command_message = "Unsaved changes! Use :w to save or :q! to discard".to_string();
return Ok((false, self.command_message.clone()));
}
self.is_edit_mode = false;
self.edit_mode_cooldown = true;
self.command_message = "Read-only mode".to_string();
terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?;
let current_input = form_state.get_current_input();
if !current_input.is_empty() && form_state.current_cursor_pos >= current_input.len() {
form_state.current_cursor_pos = current_input.len() - 1;
self.ideal_cursor_column = form_state.current_cursor_pos;
}
return Ok((false, self.command_message.clone()));
}
// Check for core application actions (save, quit, etc.)
// ONLY handle a limited subset of core actions here
if let Some(action) = config.get_action_for_key_in_mode(
&config.keybindings.common,
key_code,
modifiers
) {
match action {
"save" | "force_quit" | "save_and_quit" | "revert" => {
return common::handle_core_action(
action,
form_state,
&mut self.auth_state,
grpc_client,
&mut self.auth_client,
terminal,
app_state,
current_position,
total_count,
).await;
},
_ => {} // For other actions, let the mode-specific handler take care of it
}
}
// Let edit mode handle its own actions (including navigation from common bindings)
let result = edit::handle_edit_event_internal(
key,
config,
form_state,
&mut self.ideal_cursor_column,
&mut self.command_message,
&mut app_state.ui.is_saved,
current_position,
total_count,
grpc_client,
).await?;
self.key_sequence_tracker.reset();
return Ok((false, result));
},
AppMode::Command => {
let (should_exit, message, exit_command_mode) = command_mode::handle_command_event(
key,
config,
form_state,
&mut self.command_input,
&mut self.command_message,
grpc_client,
command_handler,
terminal,
current_position,
total_count,
).await?;
if exit_command_mode {
self.command_mode = false;
}
return Ok((should_exit, message));
}
}
}
// Non-key events or if no specific handler was matched
self.edit_mode_cooldown = false;
Ok((false, self.command_message.clone()))
}
}

View File

@@ -0,0 +1,51 @@
// src/modes/handlers/mode_manager.rs
use crate::state::state::AppState;
use crate::modes::handlers::event::EventHandler;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AppMode {
General, // For intro and admin screens
ReadOnly, // Canvas read-only mode
Edit, // Canvas edit mode
Command, // Command mode overlay
}
pub struct ModeManager;
impl ModeManager {
// Determine current mode based on app state
pub fn derive_mode(app_state: &AppState, event_handler: &EventHandler) -> AppMode {
if event_handler.command_mode {
return AppMode::Command;
}
if app_state.ui.show_login { // NEW: Check auth visibility
if event_handler.is_edit_mode {
AppMode::Edit
} else {
AppMode::ReadOnly
}
} else if app_state.ui.show_form {
if event_handler.is_edit_mode {
AppMode::Edit
} else {
AppMode::ReadOnly
}
} else {
AppMode::General
}
}
// Mode transition rules
pub fn can_enter_command_mode(current_mode: AppMode) -> bool {
!matches!(current_mode, AppMode::Edit) // Can't enter from Edit mode
}
pub fn can_enter_edit_mode(current_mode: AppMode) -> bool {
matches!(current_mode, AppMode::ReadOnly) // Only from ReadOnly
}
pub fn can_enter_read_only_mode(current_mode: AppMode) -> bool {
matches!(current_mode, AppMode::Edit | AppMode::Command)
}
}

View File

@@ -1,4 +1,10 @@
// src/client/modes/mod.rs
pub mod handlers;
pub mod canvas;
pub mod general;
pub mod common;
pub use handlers::*;
pub use canvas::*;
pub use general::*;
pub use common::*;

View File

@@ -0,0 +1,23 @@
// src/services/auth.rs
use tonic::transport::Channel;
use common::proto::multieko2::auth::{
auth_service_client::AuthServiceClient,
LoginRequest, LoginResponse
};
pub struct AuthClient {
client: AuthServiceClient<Channel>,
}
impl AuthClient {
pub async fn new() -> Result<Self, Box<dyn std::error::Error>> {
let client = AuthServiceClient::connect("http://[::1]:50051").await?;
Ok(Self { client })
}
pub async fn login(&mut self, identifier: String, password: String) -> Result<LoginResponse, Box<dyn std::error::Error>> {
let request = tonic::Request::new(LoginRequest { identifier, password });
let response = self.client.login(request).await?.into_inner();
Ok(response)
}
}

View File

@@ -1,4 +1,4 @@
// src/tui/terminal/grpc_client.rs
// src/services/grpc_client.rs
use tonic::transport::Channel;
use common::proto::multieko2::adresar::adresar_client::AdresarClient;
@@ -11,6 +11,7 @@ use common::proto::multieko2::table_definition::{
ProfileTreeResponse
};
#[derive(Clone)]
pub struct GrpcClient {
adresar_client: AdresarClient<Channel>,
table_structure_client: TableStructureServiceClient<Channel>,

View File

@@ -0,0 +1,9 @@
// services/mod.rs
pub mod grpc_client;
pub mod auth;
pub mod ui_service;
pub use grpc_client::*;
pub use ui_service::*;
pub use auth::*;

View File

@@ -0,0 +1,89 @@
// src/services/ui_service.rs
use crate::services::grpc_client::GrpcClient;
use crate::state::pages::form::FormState;
use crate::state::state::AppState;
pub struct UiService;
impl UiService {
pub async fn initialize_app_state(
grpc_client: &mut GrpcClient,
app_state: &mut AppState,
) -> Result<Vec<String>, Box<dyn std::error::Error>> {
// Fetch profile tree
let profile_tree = grpc_client.get_profile_tree().await?;
app_state.profile_tree = profile_tree;
// Fetch table structure
let table_structure = grpc_client.get_table_structure().await?;
// Extract the column names from the response
let column_names: Vec<String> = table_structure
.columns
.iter()
.map(|col| col.name.clone())
.collect();
Ok(column_names)
}
pub async fn initialize_adresar_count(
grpc_client: &mut GrpcClient,
app_state: &mut AppState,
) -> Result<(), Box<dyn std::error::Error>> {
let total_count = grpc_client.get_adresar_count().await?;
app_state.update_total_count(total_count);
app_state.update_current_position(total_count.saturating_add(1)); // Start in new entry mode
Ok(())
}
pub async fn update_adresar_count(
grpc_client: &mut GrpcClient,
app_state: &mut AppState,
) -> Result<(), Box<dyn std::error::Error>> {
let total_count = grpc_client.get_adresar_count().await?;
app_state.update_total_count(total_count);
Ok(())
}
pub async fn load_adresar_by_position(
grpc_client: &mut GrpcClient,
app_state: &mut AppState,
form_state: &mut FormState,
position: u64,
) -> Result<String, Box<dyn std::error::Error>> {
match grpc_client.get_adresar_by_position(position).await {
Ok(response) => {
// Set the ID properly
form_state.id = response.id;
// Update form values dynamically
form_state.values = vec![
response.firma,
response.kz,
response.drc,
response.ulica,
response.psc,
response.mesto,
response.stat,
response.banka,
response.ucet,
response.skladm,
response.ico,
response.kontakt,
response.telefon,
response.skladu,
response.fax,
];
form_state.has_unsaved_changes = false;
Ok(format!("Loaded entry {}", position))
}
Err(e) => {
Ok(format!("Error loading entry: {}", e))
}
}
}
}

View File

@@ -0,0 +1,49 @@
// src/state/canvas_state.rs
use crate::state::pages::form::FormState;
pub trait CanvasState {
fn current_field(&self) -> usize;
fn current_cursor_pos(&self) -> usize;
fn has_unsaved_changes(&self) -> bool;
fn inputs(&self) -> Vec<&String>;
fn get_current_input(&self) -> &str;
fn get_current_input_mut(&mut self) -> &mut String;
fn fields(&self) -> Vec<&str>;
}
// Implement for FormState (keep existing form.rs code and add this)
impl CanvasState for FormState {
fn current_field(&self) -> usize {
self.current_field
}
fn current_cursor_pos(&self) -> usize {
self.current_cursor_pos
}
fn has_unsaved_changes(&self) -> bool {
self.has_unsaved_changes
}
fn inputs(&self) -> Vec<&String> {
self.values.iter().collect()
}
fn get_current_input(&self) -> &str {
self.values
.get(self.current_field)
.map(|s| s.as_str())
.unwrap_or("")
}
fn get_current_input_mut(&mut self) -> &mut String {
self.values
.get_mut(self.current_field)
.expect("Invalid current_field index")
}
fn fields(&self) -> Vec<&str> {
self.fields.iter().map(|s| s.as_str()).collect()
}
}

View File

@@ -1,2 +1,4 @@
// src/state/mod.rs
pub mod state;
pub mod pages;
pub mod canvas_state;

View File

@@ -0,0 +1,4 @@
// src/state/pages.rs
pub mod form;
pub mod auth;

View File

@@ -0,0 +1,70 @@
// src/state/pages/auth.rs
use crate::state::canvas_state::CanvasState;
#[derive(Default)]
pub struct AuthState {
pub return_selected: bool,
pub username: String,
pub password: String,
pub error_message: Option<String>,
pub current_field: usize,
pub current_cursor_pos: usize,
pub auth_token: Option<String>,
pub user_id: Option<String>,
pub role: Option<String>,
}
impl AuthState {
pub fn new() -> Self {
Self {
return_selected: false,
username: String::new(),
password: String::new(),
error_message: None,
current_field: 0,
current_cursor_pos: 0,
auth_token: None,
user_id: None,
role: None,
}
}
}
impl CanvasState for AuthState {
fn current_field(&self) -> usize {
self.current_field
}
fn current_cursor_pos(&self) -> usize {
self.current_cursor_pos
}
fn has_unsaved_changes(&self) -> bool {
// Auth form doesn't need unsaved changes tracking
false
}
fn inputs(&self) -> Vec<&String> {
vec![&self.username, &self.password]
}
fn get_current_input(&self) -> &str {
match self.current_field {
0 => &self.username,
1 => &self.password,
_ => "", // Return empty string for invalid index instead of panicking
}
}
fn get_current_input_mut(&mut self) -> &mut String {
match self.current_field {
0 => &mut self.username,
1 => &mut self.password,
_ => panic!("Invalid current_field index in AuthState"),
}
}
fn fields(&self) -> Vec<&str> {
vec!["Username/Email", "Password"]
}
}

View File

@@ -1,5 +1,5 @@
// src/client/ui/handlers/form.rs
use crate::config::colors::Theme;
// src/state/pages/form.rs
use crate::config::colors::themes::Theme;
use ratatui::layout::Rect;
use ratatui::Frame;
@@ -38,7 +38,7 @@ impl FormState {
let fields: Vec<&str> = self.fields.iter().map(|s| s.as_str()).collect();
let values: Vec<&String> = self.values.iter().collect();
crate::components::handlers::form::render_form(
crate::components::form::form::render_form(
f,
area,
self,
@@ -70,4 +70,15 @@ impl FormState {
.get_mut(self.current_field)
.expect("Invalid current_field index")
}
pub fn update_from_response(&mut self, response: common::proto::multieko2::adresar::AdresarResponse) {
self.id = response.id;
self.values = vec![
response.firma, response.kz, response.drc,
response.ulica, response.psc, response.mesto,
response.stat, response.banka, response.ucet,
response.skladm, response.ico, response.kontakt,
response.telefon, response.skladu, response.fax,
];
}
}

View File

@@ -2,12 +2,31 @@
use std::env;
use common::proto::multieko2::table_definition::ProfileTreeResponse;
use crate::components::IntroState;
use crate::modes::handlers::mode_manager::AppMode;
#[derive(Default)]
pub struct DialogState {
pub show_dialog: bool,
pub dialog_title: String,
pub dialog_message: String,
pub dialog_button_active: bool,
}
pub struct UiState {
pub show_sidebar: bool,
pub is_saved: bool,
pub show_intro: bool,
pub show_admin: bool,
pub show_form: bool,
pub show_login: bool,
pub intro_state: IntroState,
pub dialog: DialogState, // Add dialog state here
}
pub struct GeneralState {
pub selected_item: usize,
pub current_option: usize,
}
pub struct AppState {
@@ -16,9 +35,12 @@ pub struct AppState {
pub total_count: u64,
pub current_position: u64,
pub profile_tree: ProfileTreeResponse,
pub selected_profile: Option<String>,
pub current_mode: AppMode,
// UI preferences
pub ui: UiState,
pub general: GeneralState,
}
impl AppState {
@@ -31,7 +53,13 @@ impl AppState {
total_count: 0,
current_position: 0,
profile_tree: ProfileTreeResponse::default(),
selected_profile: None,
current_mode: AppMode::General,
ui: UiState::default(),
general: GeneralState {
selected_item: 0,
current_option: 0,
},
})
}
@@ -43,6 +71,28 @@ impl AppState {
pub fn update_current_position(&mut self, current_position: u64) {
self.current_position = current_position;
}
pub fn update_mode(&mut self, mode: AppMode) {
self.current_mode = mode;
}
// Add dialog helper methods
pub fn show_dialog(&mut self, title: &str, message: &str) {
self.ui.dialog.show_dialog = true;
self.ui.dialog.dialog_title = title.to_string();
self.ui.dialog.dialog_message = message.to_string();
self.ui.dialog.dialog_button_active = true;
}
pub fn hide_dialog(&mut self) {
self.ui.dialog.show_dialog = false;
self.ui.dialog.dialog_title.clear();
self.ui.dialog.dialog_message.clear();
}
pub fn set_dialog_button_active(&mut self, active: bool) {
self.ui.dialog.dialog_button_active = active;
}
}
impl Default for UiState {
@@ -52,6 +102,10 @@ impl Default for UiState {
is_saved: false,
show_intro: true,
show_admin: false,
show_form: false,
show_login: false,
intro_state: IntroState::new(),
dialog: DialogState::default(),
}
}
}

View File

@@ -0,0 +1,12 @@
// src/tui/functions.rs
pub mod admin;
pub mod intro;
pub mod login;
pub mod form;
pub mod common;
pub use admin::*;
pub use intro::*;
pub use form::*;
pub use common::*;

View File

@@ -0,0 +1,8 @@
use crate::state::state::AppState;
pub fn handle_admin_selection(app_state: &mut AppState) {
let profiles = &app_state.profile_tree.profiles;
if !profiles.is_empty() && app_state.general.selected_item < profiles.len() {
app_state.selected_profile = Some(profiles[app_state.general.selected_item].name.clone());
}
}

View File

@@ -0,0 +1,8 @@
// src/tui/functions/common.rs
pub mod commands;
pub mod form;
pub mod login;
pub use commands::*;
pub use form::{revert, save as form_save};
pub use login::{cancel, save as login_save};

View File

@@ -1,9 +1,8 @@
// src/tui/terminal/commands.rs
// src/tui/controls/commands.rs
use crate::tui::terminal::core::TerminalCore;
pub struct CommandHandler {
is_saved: bool,
pub is_saved: bool,
}
impl CommandHandler {
@@ -24,7 +23,10 @@ impl CommandHandler {
}
}
async fn handle_quit(&self, terminal: &mut TerminalCore) -> Result<(bool, String), Box<dyn std::error::Error>> {
async fn handle_quit(
&self,
terminal: &mut TerminalCore,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
if self.is_saved {
terminal.cleanup()?;
Ok((true, "Exiting.".into()))
@@ -33,12 +35,18 @@ impl CommandHandler {
}
}
async fn handle_force_quit(&self, terminal: &mut TerminalCore) -> Result<(bool, String), Box<dyn std::error::Error>> {
async fn handle_force_quit(
&self,
terminal: &mut TerminalCore,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
terminal.cleanup()?;
Ok((true, "Force exiting without saving.".into()))
}
async fn handle_save_quit(&mut self, terminal: &mut TerminalCore) -> Result<(bool, String), Box<dyn std::error::Error>> {
async fn handle_save_quit(
&mut self,
terminal: &mut TerminalCore,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
self.is_saved = true;
terminal.cleanup()?;
Ok((true, "State saved. Exiting.".into()))

View File

@@ -1,7 +1,7 @@
// src/modes/handlers/common.rs
// src/tui/functions/common/form.rs
use crate::tui::terminal::grpc_client::GrpcClient;
use crate::ui::handlers::form::FormState;
use crate::services::grpc_client::GrpcClient;
use crate::state::pages::form::FormState;
use common::proto::multieko2::adresar::{PostAdresarRequest, PutAdresarRequest};
/// Shared logic for saving the current form state
@@ -65,64 +65,6 @@ pub async fn save(
Ok(message)
}
/// Shared logic for force quitting the application
pub fn force_quit() -> (bool, String) {
(true, "Force quitting application".to_string())
}
/// Shared logic for saving and quitting
pub async fn save_and_quit(
form_state: &mut FormState,
grpc_client: &mut GrpcClient,
current_position: &mut u64,
total_count: u64,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
let is_new = *current_position == total_count + 1;
if is_new {
let post_request = PostAdresarRequest {
firma: form_state.values[0].clone(),
kz: form_state.values[1].clone(),
drc: form_state.values[2].clone(),
ulica: form_state.values[3].clone(),
psc: form_state.values[4].clone(),
mesto: form_state.values[5].clone(),
stat: form_state.values[6].clone(),
banka: form_state.values[7].clone(),
ucet: form_state.values[8].clone(),
skladm: form_state.values[9].clone(),
ico: form_state.values[10].clone(),
kontakt: form_state.values[11].clone(),
telefon: form_state.values[12].clone(),
skladu: form_state.values[13].clone(),
fax: form_state.values[14].clone(),
};
let _ = grpc_client.post_adresar(post_request).await?;
} else {
let put_request = PutAdresarRequest {
id: form_state.id,
firma: form_state.values[0].clone(),
kz: form_state.values[1].clone(),
drc: form_state.values[2].clone(),
ulica: form_state.values[3].clone(),
psc: form_state.values[4].clone(),
mesto: form_state.values[5].clone(),
stat: form_state.values[6].clone(),
banka: form_state.values[7].clone(),
ucet: form_state.values[8].clone(),
skladm: form_state.values[9].clone(),
ico: form_state.values[10].clone(),
kontakt: form_state.values[11].clone(),
telefon: form_state.values[12].clone(),
skladu: form_state.values[13].clone(),
fax: form_state.values[14].clone(),
};
let _ = grpc_client.put_adresar(put_request).await?;
}
Ok((true, "Saved and exiting application".to_string()))
}
/// Discard changes since last save
pub async fn revert(
form_state: &mut FormState,

View File

@@ -0,0 +1,45 @@
// src/tui/functions/common/login.rs
use crate::services::auth::AuthClient;
use crate::state::pages::auth::AuthState;
use crate::state::state::AppState;
pub async fn save(
auth_state: &mut AuthState,
auth_client: &mut AuthClient,
app_state: &mut AppState,
) -> Result<String, Box<dyn std::error::Error>> {
let identifier = auth_state.username.clone();
let password = auth_state.password.clone();
match auth_client.login(identifier, password).await {
Ok(response) => {
auth_state.auth_token = Some(response.access_token);
auth_state.user_id = Some(response.user_id);
auth_state.role = Some(response.role);
auth_state.error_message = None;
// Update app state to show main interface
app_state.ui.show_login = false;
app_state.ui.show_form = true;
Ok("Login successful!".to_string())
}
Err(e) => {
let error_message = format!("Login failed: {}", e);
auth_state.error_message = Some(error_message.clone());
Ok(error_message)
}
}
}
pub async fn cancel(
auth_state: &mut AuthState,
app_state: &mut AppState,
) -> String {
auth_state.username.clear();
auth_state.password.clear();
auth_state.error_message = None;
app_state.ui.show_login = false;
app_state.ui.show_intro = true;
"Login canceled".to_string()
}

View File

@@ -0,0 +1,111 @@
// src/tui/functions/form.rs
use crate::state::pages::form::FormState;
use crate::services::grpc_client::GrpcClient;
pub async fn handle_action(
action: &str,
form_state: &mut FormState,
grpc_client: &mut GrpcClient,
current_position: &mut u64,
total_count: u64,
ideal_cursor_column: &mut usize,
) -> Result<String, Box<dyn std::error::Error>> {
match action {
"previous_entry" => {
let new_position = current_position.saturating_sub(1);
if new_position >= 1 {
*current_position = new_position;
let response = grpc_client.get_adresar_by_position(*current_position).await?;
// Direct field assignments
form_state.id = response.id;
form_state.values = vec![
response.firma, response.kz, response.drc,
response.ulica, response.psc, response.mesto,
response.stat, response.banka, response.ucet,
response.skladm, response.ico, response.kontakt,
response.telefon, response.skladu, response.fax,
];
let current_input = form_state.get_current_input();
let max_cursor_pos = if !current_input.is_empty() {
current_input.len() - 1
} else { 0 };
form_state.current_cursor_pos = std::cmp::min(*ideal_cursor_column, max_cursor_pos);
form_state.has_unsaved_changes = false;
Ok(format!("Loaded form entry {}", *current_position))
} else {
Ok("Already at first form entry".into())
}
}
"next_entry" => {
if *current_position <= total_count {
*current_position += 1;
if *current_position <= total_count {
let response = grpc_client.get_adresar_by_position(*current_position).await?;
// Direct field assignments
form_state.id = response.id;
form_state.values = vec![
response.firma, response.kz, response.drc,
response.ulica, response.psc, response.mesto,
response.stat, response.banka, response.ucet,
response.skladm, response.ico, response.kontakt,
response.telefon, response.skladu, response.fax,
];
let current_input = form_state.get_current_input();
let max_cursor_pos = if !current_input.is_empty() {
current_input.len() - 1
} else { 0 };
form_state.current_cursor_pos = std::cmp::min(*ideal_cursor_column, max_cursor_pos);
form_state.has_unsaved_changes = false;
Ok(format!("Loaded form entry {}", *current_position))
} else {
form_state.reset_to_empty();
form_state.current_field = 0;
form_state.current_cursor_pos = 0;
*ideal_cursor_column = 0;
Ok("New form entry mode".into())
}
} else {
Ok("Already at last entry".into())
}
}
"move_up" => {
// Change field first
if form_state.current_field == 0 {
form_state.current_field = form_state.fields.len() - 1;
} else {
form_state.current_field = form_state.current_field.saturating_sub(1);
}
// Get current input AFTER changing field
let current_input = form_state.get_current_input();
let max_cursor_pos = if !current_input.is_empty() {
current_input.len() - 1
} else {
0
};
form_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
Ok("".to_string())
}
"move_down" => {
// Change field first
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
// Get current input AFTER changing field
let current_input = form_state.get_current_input();
let max_cursor_pos = if !current_input.is_empty() {
current_input.len() - 1
} else {
0
};
form_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
Ok("".to_string())
}
_ => Err("Unknown form action".into())
}
}

View File

@@ -0,0 +1,23 @@
use crate::state::state::AppState;
pub fn handle_intro_selection(app_state: &mut AppState) {
match app_state.ui.intro_state.selected_option {
0 => { // Continue
app_state.ui.show_form = true;
app_state.ui.show_admin = false;
app_state.ui.show_login = false;
}
1 => { // Admin
app_state.ui.show_form = false;
app_state.ui.show_admin = true;
app_state.ui.show_login = false;
}
2 => { // Login
app_state.ui.show_form = false;
app_state.ui.show_admin = false;
app_state.ui.show_login = true;
}
_ => {}
}
app_state.ui.show_intro = false;
}

View File

@@ -0,0 +1,64 @@
// src/tui/functions/login.rs
use crate::state::pages::auth::AuthState;
use crate::state::canvas_state::CanvasState;
pub async fn handle_action(
action: &str,
auth_state: &mut AuthState,
ideal_cursor_column: &mut usize,
) -> Result<String, Box<dyn std::error::Error>> {
match action {
"move_up" => {
if auth_state.return_selected {
// From Return button to last field (password)
auth_state.return_selected = false;
auth_state.current_field = 1;
} else if auth_state.current_field == 1 {
// Password -> Username
auth_state.current_field = 0;
} else if auth_state.current_field == 0 {
// Username -> Password (wrap around fields only)
auth_state.current_field = 1;
} else if auth_state.current_field == 2 {
// From Login button to Password field
auth_state.current_field = 1;
}
// Update cursor position only when in a field
if auth_state.current_field < 2 {
let current_input = auth_state.get_current_input();
let max_cursor_pos = current_input.len();
auth_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
}
Ok(format!("Navigation 'up' from functions/login"))
}
"move_down" => {
if auth_state.return_selected {
// From Return button to first field (username)
auth_state.return_selected = false;
auth_state.current_field = 0;
} else if auth_state.current_field == 0 {
// Username -> Password
auth_state.current_field = 1;
} else if auth_state.current_field == 1 {
// Password -> Login button
auth_state.current_field = 2;
auth_state.return_selected = false;
} else if auth_state.current_field == 2 {
// Login button -> Return button
auth_state.return_selected = true;
}
// Update cursor position only when in a field
if auth_state.current_field < 2 {
let current_input = auth_state.get_current_input();
let max_cursor_pos = current_input.len();
auth_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
}
Ok(format!("Navigation 'down' from functions/login"))
}
_ => Err("Unknown login action".into())
}
}

View File

@@ -1,2 +1,5 @@
// src/tui/mod.rs
pub mod terminal;
pub mod functions;
pub use functions::*;

View File

@@ -1,11 +1,7 @@
// src/tui/terminal.rs
pub mod core;
pub mod grpc_client;
pub mod commands;
pub mod event_reader;
pub use core::TerminalCore;
pub use grpc_client::GrpcClient;
pub use commands::CommandHandler;
pub use event_reader::EventReader;

View File

@@ -1,7 +1,6 @@
// src/client/ui/handlers.rs
pub mod ui;
pub mod form;
pub mod render;
pub mod rat_state;

View File

@@ -1,6 +1,6 @@
// src/ui/handlers/rat_state.rs
use crossterm::event::{KeyCode, KeyModifiers};
use crate::config::config::Config;
use crate::config::binds::config::Config;
use crate::state::state::UiState;
pub struct UiStateHandler;

View File

@@ -4,18 +4,22 @@ use crate::components::{
render_background,
render_command_line,
render_status_line,
handlers::{sidebar::{self, calculate_sidebar_layout}, admin_panel::AdminPanelState, form::render_form},
intro::{intro},
handlers::sidebar::{self, calculate_sidebar_layout},
form::form::render_form,
admin::{admin_panel::AdminPanelState},
auth::login::render_login,
};
use crate::config::colors::Theme;
use crate::config::colors::themes::Theme;
use ratatui::layout::{Constraint, Direction, Layout};
use ratatui::Frame;
use super::form::FormState;
use crate::state::pages::form::FormState;
use crate::state::pages::auth::AuthState;
use crate::state::state::AppState;
pub fn render_ui(
f: &mut Frame,
form_state: &mut FormState,
auth_state: &mut AuthState,
theme: &Theme,
is_edit_mode: bool,
total_count: u64,
@@ -25,8 +29,7 @@ pub fn render_ui(
command_mode: bool,
command_message: &str,
app_state: &AppState,
intro_state: &intro::IntroState,
admin_panel_state: &AdminPanelState,
// intro_state parameter removed
) {
render_background(f, f.area(), theme);
@@ -41,17 +44,56 @@ pub fn render_ui(
let main_content_area = root[0];
if app_state.ui.show_intro {
intro_state.render(f, main_content_area, theme);
// Use app_state's intro_state directly
app_state.ui.intro_state.render(f, main_content_area, theme);
}else if app_state.ui.show_login {
render_login(
f,
main_content_area,
theme,
auth_state,
app_state, // Add AppState reference here
auth_state.current_field < 2
);
} else if app_state.ui.show_admin {
admin_panel_state.render(f, main_content_area, theme);
} else {
// Create temporary AdminPanelState for rendering
let mut admin_state = AdminPanelState::new(
app_state.profile_tree.profiles
.iter()
.map(|p| p.name.clone())
.collect()
);
// Set the selected item - FIXED
if !admin_state.profiles.is_empty() {
let selected_index = std::cmp::min(
app_state.general.selected_item,
admin_state.profiles.len() - 1
);
admin_state.list_state.select(Some(selected_index));
}
admin_state.render(
f,
main_content_area,
theme,
&app_state.profile_tree,
&app_state.selected_profile,
);
} else if app_state.ui.show_form {
let (sidebar_area, form_area) = calculate_sidebar_layout(
app_state.ui.show_sidebar,
main_content_area
);
if let Some(sidebar_rect) = sidebar_area {
sidebar::render_sidebar(f, sidebar_rect, theme, &app_state.profile_tree);
sidebar::render_sidebar(
f,
sidebar_rect,
theme,
&app_state.profile_tree,
&app_state.selected_profile
);
}
// This change makes the form stay stationary when toggling sidebar
@@ -94,6 +136,8 @@ pub fn render_ui(
total_count,
current_position,
);
} else{
}
render_status_line(f, root[1], current_dir, theme, is_edit_mode);

View File

@@ -1,62 +1,53 @@
// src/client/ui/handlers/ui.rs
// src/ui/handlers/ui.rs
use crate::tui::terminal::TerminalCore;
use crate::tui::terminal::GrpcClient;
use crate::tui::terminal::CommandHandler;
use crate::services::grpc_client::GrpcClient;
use crate::services::auth::AuthClient;
use crate::services::ui_service::UiService; // Add this import
use crate::tui::terminal::EventReader;
use crate::config::colors::Theme;
use crate::config::config::Config;
use crate::ui::handlers::{form::FormState, render::render_ui};
use crate::tui::functions::common::CommandHandler;
use crate::config::colors::themes::Theme;
use crate::config::binds::config::Config;
use crate::ui::handlers::render::render_ui;
use crate::state::pages::form::FormState;
use crate::state::pages::auth::AuthState;
use crate::modes::handlers::event::EventHandler;
use crate::state::state::AppState;
use crate::components::handlers::{admin_panel::AdminPanelState};
use crate::components::intro::{intro::IntroState};
pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
let config = Config::load()?;
let mut terminal = TerminalCore::new()?;
let mut grpc_client = GrpcClient::new().await?;
let mut auth_client = AuthClient::new().await?;
let mut command_handler = CommandHandler::new();
let theme = Theme::from_str(&config.colors.theme);
let mut intro_state = IntroState::new();
let admin_panel_state = AdminPanelState::new();
let mut auth_state = AuthState::default();
// Fetch table structure at startup (one-time)
// TODO: Later, consider implementing a live update for table structure changes.
let table_structure = grpc_client.get_table_structure().await?;
// Initialize app_state first
let mut app_state = AppState::new()?;
// Extract the column names from the response
let column_names: Vec<String> = table_structure
.columns
.iter()
.map(|col| col.name.clone())
.collect();
// Initialize app state with profile tree and table structure
let column_names = UiService::initialize_app_state(&mut grpc_client, &mut app_state).await?;
// Initialize FormState with dynamic fields
let mut form_state = FormState::new(column_names);
// Fetch profile tree and table structure
let profile_tree = grpc_client.get_profile_tree().await?;
// The rest of your UI initialization remains the same
let mut event_handler = EventHandler::new();
let mut event_handler = EventHandler::new().await?;
let event_reader = EventReader::new();
let mut app_state = AppState::new()?;
app_state.profile_tree = profile_tree;
// Fetch the total count of Adresar entries
let total_count = grpc_client.get_adresar_count().await?;
app_state.update_total_count(total_count);
app_state.update_current_position(total_count.saturating_add(1)); // Start in new entry mode
UiService::initialize_adresar_count(&mut grpc_client, &mut app_state).await?;
form_state.reset_to_empty();
loop {
let total_count = grpc_client.get_adresar_count().await?;
app_state.update_total_count(total_count);
UiService::update_adresar_count(&mut grpc_client, &mut app_state).await?;
terminal.draw(|f| {
render_ui(
f,
&mut form_state,
&mut auth_state,
&theme,
event_handler.is_edit_mode,
app_state.total_count,
@@ -66,8 +57,6 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
event_handler.command_mode,
&event_handler.command_message,
&app_state,
&intro_state,
&admin_panel_state,
);
})?;
@@ -85,7 +74,6 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
&mut app_state,
total_count,
&mut current_position,
&mut intro_state,
).await?;
app_state.current_position = current_position;
@@ -100,7 +88,6 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
};
form_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos);
// Ensure position never exceeds total_count + 1
if app_state.current_position > total_count + 1 {
app_state.current_position = total_count + 1;
@@ -111,44 +98,22 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
form_state.current_field = 0;
} else if app_state.current_position >= 1 && app_state.current_position <= total_count {
// Existing entry - load data
match grpc_client.get_adresar_by_position(app_state.current_position).await {
Ok(response) => {
// Set the ID properly
form_state.id = response.id;
let current_position = app_state.current_position;
let message = UiService::load_adresar_by_position(
&mut grpc_client,
&mut app_state,
&mut form_state,
current_position
).await?;
// Update form values dynamically
form_state.values = vec![
response.firma,
response.kz,
response.drc,
response.ulica,
response.psc,
response.mesto,
response.stat,
response.banka,
response.ucet,
response.skladm,
response.ico,
response.kontakt,
response.telefon,
response.skladu,
response.fax,
];
let current_input = form_state.get_current_input();
let max_cursor_pos = if !event_handler.is_edit_mode && !current_input.is_empty() {
current_input.len() - 1 // In readonly mode, limit to last character
} else {
current_input.len()
};
form_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos);
form_state.has_unsaved_changes = false;
event_handler.command_message = format!("Loaded entry {}", app_state.current_position);
}
Err(e) => {
event_handler.command_message = format!("Error loading entry: {}", e);
}
}
let current_input = form_state.get_current_input();
let max_cursor_pos = if !event_handler.is_edit_mode && !current_input.is_empty() {
current_input.len() - 1 // In readonly mode, limit to last character
} else {
current_input.len()
};
form_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos);
event_handler.command_message = message;
} else {
// Invalid position - reset to first entry
app_state.current_position = 1;
@@ -161,3 +126,4 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
}
}
}

View File

@@ -8,6 +8,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
&[
"proto/common.proto",
"proto/adresar.proto",
"proto/auth.proto",
"proto/uctovnictvo.proto",
"proto/table_structure.proto",
"proto/table_definition.proto",

37
common/proto/auth.proto Normal file
View File

@@ -0,0 +1,37 @@
// proto/auth.proto
syntax = "proto3";
package multieko2.auth;
import "common.proto";
service AuthService {
rpc Register(RegisterRequest) returns (AuthResponse);
rpc Login(LoginRequest) returns (LoginResponse);
}
message RegisterRequest {
string username = 1;
string email = 2;
string password = 3;
string password_confirmation = 4;
}
message AuthResponse {
string id = 1; // UUID in string format
string username = 2; // Registered username
string email = 3; // Registered email (if provided)
string role = 4; // Default role: 'accountant'
}
message LoginRequest {
string identifier = 1; // Can be username or email
string password = 2;
}
message LoginResponse {
string access_token = 1; // JWT token
string token_type = 2; // Usually "Bearer"
int32 expires_in = 3; // Expiration in seconds (86400 for 24 hours)
string user_id = 4; // User's UUID in string format
string role = 5; // User's role
}

View File

@@ -4,6 +4,9 @@ pub mod proto {
pub mod adresar {
include!("proto/multieko2.adresar.rs");
}
pub mod auth {
include!("proto/multieko2.auth.rs");
}
pub mod common {
include!("proto/multieko2.common.rs");
}

Binary file not shown.

View File

@@ -0,0 +1,412 @@
// This file is @generated by prost-build.
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct RegisterRequest {
#[prost(string, tag = "1")]
pub username: ::prost::alloc::string::String,
#[prost(string, tag = "2")]
pub email: ::prost::alloc::string::String,
#[prost(string, tag = "3")]
pub password: ::prost::alloc::string::String,
#[prost(string, tag = "4")]
pub password_confirmation: ::prost::alloc::string::String,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct AuthResponse {
/// UUID in string format
#[prost(string, tag = "1")]
pub id: ::prost::alloc::string::String,
/// Registered username
#[prost(string, tag = "2")]
pub username: ::prost::alloc::string::String,
/// Registered email (if provided)
#[prost(string, tag = "3")]
pub email: ::prost::alloc::string::String,
/// Default role: 'accountant'
#[prost(string, tag = "4")]
pub role: ::prost::alloc::string::String,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct LoginRequest {
/// Can be username or email
#[prost(string, tag = "1")]
pub identifier: ::prost::alloc::string::String,
#[prost(string, tag = "2")]
pub password: ::prost::alloc::string::String,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct LoginResponse {
/// JWT token
#[prost(string, tag = "1")]
pub access_token: ::prost::alloc::string::String,
/// Usually "Bearer"
#[prost(string, tag = "2")]
pub token_type: ::prost::alloc::string::String,
/// Expiration in seconds (86400 for 24 hours)
#[prost(int32, tag = "3")]
pub expires_in: i32,
/// User's UUID in string format
#[prost(string, tag = "4")]
pub user_id: ::prost::alloc::string::String,
/// User's role
#[prost(string, tag = "5")]
pub role: ::prost::alloc::string::String,
}
/// Generated client implementations.
pub mod auth_service_client {
#![allow(
unused_variables,
dead_code,
missing_docs,
clippy::wildcard_imports,
clippy::let_unit_value,
)]
use tonic::codegen::*;
use tonic::codegen::http::Uri;
#[derive(Debug, Clone)]
pub struct AuthServiceClient<T> {
inner: tonic::client::Grpc<T>,
}
impl AuthServiceClient<tonic::transport::Channel> {
/// Attempt to create a new client by connecting to a given endpoint.
pub async fn connect<D>(dst: D) -> Result<Self, tonic::transport::Error>
where
D: TryInto<tonic::transport::Endpoint>,
D::Error: Into<StdError>,
{
let conn = tonic::transport::Endpoint::new(dst)?.connect().await?;
Ok(Self::new(conn))
}
}
impl<T> AuthServiceClient<T>
where
T: tonic::client::GrpcService<tonic::body::BoxBody>,
T::Error: Into<StdError>,
T::ResponseBody: Body<Data = Bytes> + std::marker::Send + 'static,
<T::ResponseBody as Body>::Error: Into<StdError> + std::marker::Send,
{
pub fn new(inner: T) -> Self {
let inner = tonic::client::Grpc::new(inner);
Self { inner }
}
pub fn with_origin(inner: T, origin: Uri) -> Self {
let inner = tonic::client::Grpc::with_origin(inner, origin);
Self { inner }
}
pub fn with_interceptor<F>(
inner: T,
interceptor: F,
) -> AuthServiceClient<InterceptedService<T, F>>
where
F: tonic::service::Interceptor,
T::ResponseBody: Default,
T: tonic::codegen::Service<
http::Request<tonic::body::BoxBody>,
Response = http::Response<
<T as tonic::client::GrpcService<tonic::body::BoxBody>>::ResponseBody,
>,
>,
<T as tonic::codegen::Service<
http::Request<tonic::body::BoxBody>,
>>::Error: Into<StdError> + std::marker::Send + std::marker::Sync,
{
AuthServiceClient::new(InterceptedService::new(inner, interceptor))
}
/// Compress requests with the given encoding.
///
/// This requires the server to support it otherwise it might respond with an
/// error.
#[must_use]
pub fn send_compressed(mut self, encoding: CompressionEncoding) -> Self {
self.inner = self.inner.send_compressed(encoding);
self
}
/// Enable decompressing responses.
#[must_use]
pub fn accept_compressed(mut self, encoding: CompressionEncoding) -> Self {
self.inner = self.inner.accept_compressed(encoding);
self
}
/// Limits the maximum size of a decoded message.
///
/// Default: `4MB`
#[must_use]
pub fn max_decoding_message_size(mut self, limit: usize) -> Self {
self.inner = self.inner.max_decoding_message_size(limit);
self
}
/// Limits the maximum size of an encoded message.
///
/// Default: `usize::MAX`
#[must_use]
pub fn max_encoding_message_size(mut self, limit: usize) -> Self {
self.inner = self.inner.max_encoding_message_size(limit);
self
}
pub async fn register(
&mut self,
request: impl tonic::IntoRequest<super::RegisterRequest>,
) -> std::result::Result<tonic::Response<super::AuthResponse>, tonic::Status> {
self.inner
.ready()
.await
.map_err(|e| {
tonic::Status::unknown(
format!("Service was not ready: {}", e.into()),
)
})?;
let codec = tonic::codec::ProstCodec::default();
let path = http::uri::PathAndQuery::from_static(
"/multieko2.auth.AuthService/Register",
);
let mut req = request.into_request();
req.extensions_mut()
.insert(GrpcMethod::new("multieko2.auth.AuthService", "Register"));
self.inner.unary(req, path, codec).await
}
pub async fn login(
&mut self,
request: impl tonic::IntoRequest<super::LoginRequest>,
) -> std::result::Result<tonic::Response<super::LoginResponse>, tonic::Status> {
self.inner
.ready()
.await
.map_err(|e| {
tonic::Status::unknown(
format!("Service was not ready: {}", e.into()),
)
})?;
let codec = tonic::codec::ProstCodec::default();
let path = http::uri::PathAndQuery::from_static(
"/multieko2.auth.AuthService/Login",
);
let mut req = request.into_request();
req.extensions_mut()
.insert(GrpcMethod::new("multieko2.auth.AuthService", "Login"));
self.inner.unary(req, path, codec).await
}
}
}
/// Generated server implementations.
pub mod auth_service_server {
#![allow(
unused_variables,
dead_code,
missing_docs,
clippy::wildcard_imports,
clippy::let_unit_value,
)]
use tonic::codegen::*;
/// Generated trait containing gRPC methods that should be implemented for use with AuthServiceServer.
#[async_trait]
pub trait AuthService: std::marker::Send + std::marker::Sync + 'static {
async fn register(
&self,
request: tonic::Request<super::RegisterRequest>,
) -> std::result::Result<tonic::Response<super::AuthResponse>, tonic::Status>;
async fn login(
&self,
request: tonic::Request<super::LoginRequest>,
) -> std::result::Result<tonic::Response<super::LoginResponse>, tonic::Status>;
}
#[derive(Debug)]
pub struct AuthServiceServer<T> {
inner: Arc<T>,
accept_compression_encodings: EnabledCompressionEncodings,
send_compression_encodings: EnabledCompressionEncodings,
max_decoding_message_size: Option<usize>,
max_encoding_message_size: Option<usize>,
}
impl<T> AuthServiceServer<T> {
pub fn new(inner: T) -> Self {
Self::from_arc(Arc::new(inner))
}
pub fn from_arc(inner: Arc<T>) -> Self {
Self {
inner,
accept_compression_encodings: Default::default(),
send_compression_encodings: Default::default(),
max_decoding_message_size: None,
max_encoding_message_size: None,
}
}
pub fn with_interceptor<F>(
inner: T,
interceptor: F,
) -> InterceptedService<Self, F>
where
F: tonic::service::Interceptor,
{
InterceptedService::new(Self::new(inner), interceptor)
}
/// Enable decompressing requests with the given encoding.
#[must_use]
pub fn accept_compressed(mut self, encoding: CompressionEncoding) -> Self {
self.accept_compression_encodings.enable(encoding);
self
}
/// Compress responses with the given encoding, if the client supports it.
#[must_use]
pub fn send_compressed(mut self, encoding: CompressionEncoding) -> Self {
self.send_compression_encodings.enable(encoding);
self
}
/// Limits the maximum size of a decoded message.
///
/// Default: `4MB`
#[must_use]
pub fn max_decoding_message_size(mut self, limit: usize) -> Self {
self.max_decoding_message_size = Some(limit);
self
}
/// Limits the maximum size of an encoded message.
///
/// Default: `usize::MAX`
#[must_use]
pub fn max_encoding_message_size(mut self, limit: usize) -> Self {
self.max_encoding_message_size = Some(limit);
self
}
}
impl<T, B> tonic::codegen::Service<http::Request<B>> for AuthServiceServer<T>
where
T: AuthService,
B: Body + std::marker::Send + 'static,
B::Error: Into<StdError> + std::marker::Send + 'static,
{
type Response = http::Response<tonic::body::BoxBody>;
type Error = std::convert::Infallible;
type Future = BoxFuture<Self::Response, Self::Error>;
fn poll_ready(
&mut self,
_cx: &mut Context<'_>,
) -> Poll<std::result::Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: http::Request<B>) -> Self::Future {
match req.uri().path() {
"/multieko2.auth.AuthService/Register" => {
#[allow(non_camel_case_types)]
struct RegisterSvc<T: AuthService>(pub Arc<T>);
impl<
T: AuthService,
> tonic::server::UnaryService<super::RegisterRequest>
for RegisterSvc<T> {
type Response = super::AuthResponse;
type Future = BoxFuture<
tonic::Response<Self::Response>,
tonic::Status,
>;
fn call(
&mut self,
request: tonic::Request<super::RegisterRequest>,
) -> Self::Future {
let inner = Arc::clone(&self.0);
let fut = async move {
<T as AuthService>::register(&inner, request).await
};
Box::pin(fut)
}
}
let accept_compression_encodings = self.accept_compression_encodings;
let send_compression_encodings = self.send_compression_encodings;
let max_decoding_message_size = self.max_decoding_message_size;
let max_encoding_message_size = self.max_encoding_message_size;
let inner = self.inner.clone();
let fut = async move {
let method = RegisterSvc(inner);
let codec = tonic::codec::ProstCodec::default();
let mut grpc = tonic::server::Grpc::new(codec)
.apply_compression_config(
accept_compression_encodings,
send_compression_encodings,
)
.apply_max_message_size_config(
max_decoding_message_size,
max_encoding_message_size,
);
let res = grpc.unary(method, req).await;
Ok(res)
};
Box::pin(fut)
}
"/multieko2.auth.AuthService/Login" => {
#[allow(non_camel_case_types)]
struct LoginSvc<T: AuthService>(pub Arc<T>);
impl<T: AuthService> tonic::server::UnaryService<super::LoginRequest>
for LoginSvc<T> {
type Response = super::LoginResponse;
type Future = BoxFuture<
tonic::Response<Self::Response>,
tonic::Status,
>;
fn call(
&mut self,
request: tonic::Request<super::LoginRequest>,
) -> Self::Future {
let inner = Arc::clone(&self.0);
let fut = async move {
<T as AuthService>::login(&inner, request).await
};
Box::pin(fut)
}
}
let accept_compression_encodings = self.accept_compression_encodings;
let send_compression_encodings = self.send_compression_encodings;
let max_decoding_message_size = self.max_decoding_message_size;
let max_encoding_message_size = self.max_encoding_message_size;
let inner = self.inner.clone();
let fut = async move {
let method = LoginSvc(inner);
let codec = tonic::codec::ProstCodec::default();
let mut grpc = tonic::server::Grpc::new(codec)
.apply_compression_config(
accept_compression_encodings,
send_compression_encodings,
)
.apply_max_message_size_config(
max_decoding_message_size,
max_encoding_message_size,
);
let res = grpc.unary(method, req).await;
Ok(res)
};
Box::pin(fut)
}
_ => {
Box::pin(async move {
let mut response = http::Response::new(empty_body());
let headers = response.headers_mut();
headers
.insert(
tonic::Status::GRPC_STATUS,
(tonic::Code::Unimplemented as i32).into(),
);
headers
.insert(
http::header::CONTENT_TYPE,
tonic::metadata::GRPC_CONTENT_TYPE,
);
Ok(response)
})
}
}
}
}
impl<T> Clone for AuthServiceServer<T> {
fn clone(&self) -> Self {
let inner = self.inner.clone();
Self {
inner,
accept_compression_encodings: self.accept_compression_encodings,
send_compression_encodings: self.send_compression_encodings,
max_decoding_message_size: self.max_decoding_message_size,
max_encoding_message_size: self.max_encoding_message_size,
}
}
}
/// Generated gRPC service name
pub const SERVICE_NAME: &str = "multieko2.auth.AuthService";
impl<T> tonic::server::NamedService for AuthServiceServer<T> {
const NAME: &'static str = SERVICE_NAME;
}
}

View File

@@ -0,0 +1,22 @@
{
"db_name": "PostgreSQL",
"query": "SELECT ltd.table_name\n FROM table_definition_links tdl\n JOIN table_definitions ltd ON tdl.linked_table_id = ltd.id\n WHERE tdl.source_table_id = $1",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "table_name",
"type_info": "Text"
}
],
"parameters": {
"Left": [
"Int8"
]
},
"nullable": [
false
]
},
"hash": "468bb5bb4fdaefcb4b280761d7880a556d40c172568ad3a1ed13156fbef72776"
}

View File

@@ -0,0 +1,42 @@
{
"db_name": "PostgreSQL",
"query": "\n INSERT INTO users (username, email, password_hash, role)\n VALUES ($1, $2, $3, 'accountant')\n RETURNING id, username, email, role\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id",
"type_info": "Uuid"
},
{
"ordinal": 1,
"name": "username",
"type_info": "Varchar"
},
{
"ordinal": 2,
"name": "email",
"type_info": "Varchar"
},
{
"ordinal": 3,
"name": "role",
"type_info": "Varchar"
}
],
"parameters": {
"Left": [
"Varchar",
"Varchar",
"Varchar"
]
},
"nullable": [
false,
false,
true,
false
]
},
"hash": "48d0a6d393dac121bfa4230830a105aede2179b07395f97750ab2fa1970afacd"
}

View File

@@ -0,0 +1,34 @@
{
"db_name": "PostgreSQL",
"query": "\n SELECT id, password_hash, role\n FROM users\n WHERE username = $1 OR email = $1\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id",
"type_info": "Uuid"
},
{
"ordinal": 1,
"name": "password_hash",
"type_info": "Varchar"
},
{
"ordinal": 2,
"name": "role",
"type_info": "Varchar"
}
],
"parameters": {
"Left": [
"Text"
]
},
"nullable": [
false,
true,
false
]
},
"hash": "6be0bb23ad1ba85add309f6e4f55495a5d248901fb0d23fea908815747a0bd50"
}

View File

@@ -0,0 +1,23 @@
{
"db_name": "PostgreSQL",
"query": "SELECT table_name FROM table_definitions\n WHERE profile_id = $1 AND table_name LIKE $2",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "table_name",
"type_info": "Text"
}
],
"parameters": {
"Left": [
"Int8",
"Text"
]
},
"nullable": [
false
]
},
"hash": "80f0f7f9ab12a8fe07ea71b548d27bcd6253b598ac4486b72b3d960f03df47f3"
}

View File

@@ -1,22 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "SELECT ltd.table_name \n FROM table_definition_links tdl\n JOIN table_definitions ltd ON tdl.linked_table_id = ltd.id\n WHERE tdl.source_table_id = $1 AND tdl.is_required = true",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "table_name",
"type_info": "Text"
}
],
"parameters": {
"Left": [
"Int8"
]
},
"nullable": [
false
]
},
"hash": "b097f30f98490b979939759d85327a20ca7ade4866052a5cfdb0451fb76fbf15"
}

View File

@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "INSERT INTO table_scripts\n (table_definitions_id, target_column, target_column_type, script, description)\n VALUES ($1, $2, $3, $4, $5)\n RETURNING id",
"query": "INSERT INTO table_scripts\n (table_definitions_id, target_table, target_column,\n target_column_type, script, description, profile_id)\n VALUES ($1, $2, $3, $4, $5, $6, $7)\n RETURNING id",
"describe": {
"columns": [
{
@@ -15,12 +15,14 @@
"Text",
"Text",
"Text",
"Text"
"Text",
"Text",
"Int8"
]
},
"nullable": [
false
]
},
"hash": "4cd5de4d3332ca35a9975ffb32728041978435e14f97c559e2ffec4a82d567ae"
"hash": "c07a8511e5f32bf230240b28cb292d40f862b6ec58883f21ee8e1937860585d6"
}

View File

@@ -12,7 +12,7 @@ dotenvy = "0.15.7"
prost = "0.13.5"
serde = { version = "1.0.218", features = ["derive"] }
serde_json = "1.0.140"
sqlx = { version = "0.8.3", features = ["chrono", "postgres", "runtime-tokio", "runtime-tokio-native-tls", "time"] }
sqlx = { version = "0.8.3", features = ["chrono", "postgres", "runtime-tokio", "runtime-tokio-native-tls", "time", "uuid"] }
tokio = { version = "1.43.0", features = ["full", "macros"] }
tonic = "0.12.3"
tonic-reflection = "0.12.3"
@@ -24,6 +24,10 @@ thiserror = "2.0.12"
dashmap = "6.1.0"
lazy_static = "1.5.0"
regex = "1.11.1"
bcrypt = "0.17.0"
validator = { version = "0.20.0", features = ["derive"] }
uuid = { version = "1.16.0", features = ["serde", "v4"] }
jsonwebtoken = "9.3.1"
[lib]
name = "server"

View File

@@ -0,0 +1,38 @@
-- Add migration script here
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
username VARCHAR(255) NOT NULL UNIQUE,
email VARCHAR(255) UNIQUE,
password_hash VARCHAR(255),
role VARCHAR(20) NOT NULL DEFAULT 'accountant',
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
);
-- Add an index for faster lookups
CREATE INDEX idx_users_email_username ON users(email, username);
ALTER TABLE users
ADD CONSTRAINT valid_roles CHECK (role IN (
'admin',
'moderator',
'accountant',
'viewer'
));
-- Create JWT sessions table
CREATE TABLE user_sessions (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
jwt_token TEXT NOT NULL UNIQUE,
expires_at TIMESTAMPTZ NOT NULL,
revoked BOOLEAN NOT NULL DEFAULT false,
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- Add indexes
CREATE INDEX idx_sessions_user ON user_sessions(user_id);
CREATE INDEX idx_sessions_expires ON user_sessions(expires_at);

View File

@@ -0,0 +1,10 @@
grpcurl -plaintext -d '{
"username": "testuser2",
"email": "test2@example.com"
}' localhost:50051 multieko2.auth.AuthService/Register
{
"id": "5fa9bbce-85e0-4b06-8364-b561770c2fdd",
"username": "testuser2",
"email": "test2@example.com",
"role": "accountant"
}

View File

@@ -0,0 +1,51 @@
grpcurl -plaintext -d '{
"username": "testuser3",
"email": "test3@example.com",
"password": "your_password",
"password_confirmation": "your_password"
}' localhost:50051 multieko2.auth.AuthService/Register
{
"id": "96d2fd35-b39d-4c05-916a-66134453d34c",
"username": "testuser3",
"email": "test3@example.com",
"role": "accountant"
}
grpcurl -plaintext -d '{
"identifier": "testuser3"
}' localhost:50051 multieko2.auth.AuthService/Login
ERROR:
Code: Unauthenticated
Message: Invalid credentials
grpcurl -plaintext -d '{
"identifier": "testuser3",
"password": "your_password"
}' localhost:50051 multieko2.auth.AuthService/Login
{
"accessToken": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI5NmQyZmQzNS1iMzlkLTRjMDUtOTE2YS02NjEzNDQ1M2QzNGMiLCJleHAiOjE3NDI5ODE2MTAsInJvbGUiOiJhY2NvdW50YW50In0.78VIR3X4QZohzeI5x3xmkmqcICTusOC6PELPohMV-k8",
"tokenType": "Bearer",
"expiresIn": 86400,
"userId": "96d2fd35-b39d-4c05-916a-66134453d34c",
"role": "accountant"
}
grpcurl -plaintext -d '{
"username": "testuser4",
"email": "test4@example.com"
}' localhost:50051 multieko2.auth.AuthService/Register
{
"id": "413d7ecc-f231-48af-8c5a-566b1dc2bf0b",
"username": "testuser4",
"email": "test4@example.com",
"role": "accountant"
}
grpcurl -plaintext -d '{
"identifier": "test4@example.com"
}' localhost:50051 multieko2.auth.AuthService/Login
{
"accessToken": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI0MTNkN2VjYy1mMjMxLTQ4YWYtOGM1YS01NjZiMWRjMmJmMGIiLCJleHAiOjE3NDI5ODE3MDEsInJvbGUiOiJhY2NvdW50YW50In0.4Hzu3tTZRNGHnBSgeCbGy2tFTl8EzpPdXBhcW8kuIc8",
"tokenType": "Bearer",
"expiresIn": 86400,
"userId": "413d7ecc-f231-48af-8c5a-566b1dc2bf0b",
"role": "accountant"
}
╭─    ~/Doc/pr/multieko2/server    auth ······ ✔
╰─

View File

@@ -0,0 +1,7 @@
// src/auth/handlers.rs
pub mod register;
pub mod login;
pub use register::*;
pub use login::*;

View File

@@ -0,0 +1,46 @@
// src/auth/handlers/login.rs
use bcrypt::verify;
use tonic::{Request, Response, Status};
use crate::db::PgPool;
use crate::auth::{models::AuthError, logic::jwt}; // Fixed import path
use common::proto::multieko2::auth::{LoginRequest, LoginResponse};
pub async fn login(
pool: &PgPool,
request: LoginRequest,
) -> Result<Response<LoginResponse>, Status> {
let user = sqlx::query!(
r#"
SELECT id, password_hash, role
FROM users
WHERE username = $1 OR email = $1
"#,
request.identifier
)
.fetch_optional(pool)
.await
.map_err(|e| Status::internal(e.to_string()))?
.ok_or_else(|| Status::unauthenticated("Invalid credentials"))?;
// Handle the optional password_hash
let password_hash = user.password_hash
.ok_or_else(|| Status::internal("User account has no password set"))?;
// Verify the password
if !verify(&request.password, &password_hash)
.map_err(|e| Status::internal(e.to_string()))?
{
return Err(Status::unauthenticated("Invalid credentials"));
}
let token = jwt::generate_token(user.id, &user.role)
.map_err(|e| Status::internal(e.to_string()))?;
Ok(Response::new(LoginResponse {
access_token: token,
token_type: "Bearer".to_string(),
expires_in: 86400, // 24 hours
user_id: user.id.to_string(),
role: user.role,
}))
}

View File

@@ -0,0 +1,48 @@
// src/auth/handlers/register.rs
use bcrypt::{hash, DEFAULT_COST};
use tonic::{Response, Status};
use common::proto::multieko2::auth::{RegisterRequest, AuthResponse};
use crate::db::PgPool;
use crate::auth::models::AuthError;
pub async fn register(
pool: &PgPool,
payload: RegisterRequest,
) -> Result<Response<AuthResponse>, Status> {
// Validate passwords match
if payload.password != payload.password_confirmation {
return Err(Status::invalid_argument(AuthError::PasswordMismatch.to_string()));
}
// Hash password
let password_hash = hash(payload.password, DEFAULT_COST)
.map_err(|e| Status::internal(AuthError::HashingError(e.to_string()).to_string()))?;
// Insert user
let user = sqlx::query!(
r#"
INSERT INTO users (username, email, password_hash, role)
VALUES ($1, $2, $3, 'accountant')
RETURNING id, username, email, role
"#,
payload.username,
payload.email,
password_hash
)
.fetch_one(pool)
.await
.map_err(|e| {
if e.to_string().contains("duplicate key") {
Status::already_exists(AuthError::UserExists.to_string())
} else {
Status::internal(AuthError::DatabaseError(e.to_string()).to_string())
}
})?;
Ok(Response::new(AuthResponse {
id: user.id.to_string(),
username: user.username,
email: user.email.unwrap_or_default(),
role: user.role,
}))
}

9
server/src/auth/logic.rs Normal file
View File

@@ -0,0 +1,9 @@
// src/auth/logic.rs
pub mod jwt;
pub mod middleware;
// TODO implement RBAC on all of the endpoints
// pub mod rbac;
pub use jwt::*;
pub use middleware::*;

View File

@@ -0,0 +1,55 @@
// src/auth/jwt.rs
use jsonwebtoken::{encode, decode, Header, EncodingKey, DecodingKey, Validation};
use serde::{Deserialize, Serialize};
use time::{Duration, OffsetDateTime};
use uuid::Uuid;
use std::sync::OnceLock;
use crate::auth::models::AuthError;
static KEYS: OnceLock<Keys> = OnceLock::new();
struct Keys {
encoding: EncodingKey,
decoding: DecodingKey,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Claims {
pub sub: Uuid, // User ID
pub exp: i64, // Expiration time
pub role: String, // User role
}
pub fn init_jwt() -> Result<(), AuthError> {
let secret = std::env::var("JWT_SECRET")
.map_err(|_| AuthError::ConfigError("JWT_SECRET must be set".to_string()))?;
KEYS.set(Keys {
encoding: EncodingKey::from_secret(secret.as_bytes()),
decoding: DecodingKey::from_secret(secret.as_bytes()),
}).map_err(|_| AuthError::ConfigError("Failed to initialize JWT keys".to_string()))?;
Ok(())
}
pub fn generate_token(user_id: Uuid, role: &str) -> Result<String, AuthError> {
let keys = KEYS.get().ok_or(AuthError::ConfigError("JWT not initialized".to_string()))?;
let exp = OffsetDateTime::now_utc() + Duration::days(365000);
let claims = Claims {
sub: user_id,
exp: exp.unix_timestamp(),
role: role.to_string(),
};
encode(&Header::default(), &claims, &keys.encoding)
.map_err(|e| AuthError::JwtError(e.to_string()))
}
pub fn validate_token(token: &str) -> Result<Claims, AuthError> {
let keys = KEYS.get().ok_or(AuthError::ConfigError("JWT not initialized".to_string()))?;
decode::<Claims>(token, &keys.decoding, &Validation::default())
.map(|data| data.claims)
.map_err(|e| AuthError::JwtError(e.to_string()))
}

View File

@@ -0,0 +1,22 @@
// src/auth/middleware.rs
use tonic::{metadata::MetadataValue, service::Interceptor, Status};
use crate::auth::{logic::jwt, models::AuthError};
pub struct AuthInterceptor;
impl Interceptor for AuthInterceptor {
fn call(&mut self, mut request: tonic::Request<()>) -> Result<tonic::Request<()>, Status> {
let metadata = request.metadata();
let token = metadata.get("authorization")
.and_then(|v| v.to_str().ok())
.and_then(|s| s.strip_prefix("Bearer "))
.ok_or(Status::unauthenticated("Missing authorization header"))?;
let claims = jwt::validate_token(token)
.map_err(|e| Status::unauthenticated(e.to_string()))?;
// Store claims in request extensions
request.extensions_mut().insert(claims);
Ok(request)
}
}

View File

@@ -0,0 +1,36 @@
// src/auth/logic/rbac.rs
use tower::ServiceBuilder;
use crate::auth::logic::rbac;
pub async fn run_server(db_pool: sqlx::PgPool) -> Result<(), Box<dyn std::error::Error>> {
// ... existing setup code ...
// Create service layers
let adresar_layer = ServiceBuilder::new()
.layer(rbac::create_adresar_layer())
.into_inner();
let uctovnictvo_layer = ServiceBuilder::new()
.layer(rbac::create_uctovnictvo_layer())
.into_inner();
// Create services with layers
let adresar_service = AdresarServer::new(AdresarService { db_pool: db_pool.clone() })
.layer(adresar_layer);
let uctovnictvo_service = UctovnictvoServer::new(UctovnictvoService { db_pool: db_pool.clone() })
.layer(uctovnictvo_layer);
// ... repeat for other services ...
Server::builder()
.add_service(auth_server)
.add_service(adresar_service)
.add_service(uctovnictvo_service)
// ... other services ...
.serve(addr)
.await?;
Ok(())
}

6
server/src/auth/mod.rs Normal file
View File

@@ -0,0 +1,6 @@
// src/auth/mod.rs
pub mod models;
pub mod logic;
pub mod handlers;

41
server/src/auth/models.rs Normal file
View File

@@ -0,0 +1,41 @@
// src/auth/models.rs
use serde::{Deserialize, Serialize};
use validator::Validate;
#[derive(Debug, Validate, Deserialize)]
pub struct RegisterRequest {
#[validate(length(min = 1, max = 30))]
pub username: String,
#[validate(email)]
pub email: String,
#[validate(length(min = 1))]
pub password: String,
pub password_confirmation: String,
}
#[derive(Debug, Validate, Deserialize)]
pub struct LoginRequest {
#[validate(length(min = 1))]
pub identifier: String,
#[validate(length(min = 1))]
pub password: String,
}
#[derive(Debug, thiserror::Error)]
pub enum AuthError {
#[error("Passwords do not match")]
PasswordMismatch,
#[error("User already exists")]
UserExists,
#[error("Database error: {0}")]
DatabaseError(String),
#[error("Hashing error: {0}")]
HashingError(String),
#[error("Invalid credentials")]
InvalidCredentials,
#[error("JWT error: {0}")]
JwtError(String),
#[error("Configuration error: {0}")]
ConfigError(String),
}

View File

@@ -1,5 +1,7 @@
// src/db.rs
use sqlx::postgres::{PgPool, PgPoolOptions};
use sqlx::postgres::PgPoolOptions;
pub use sqlx::postgres::PgPool;
use std::time::Duration;
use tracing::info;

View File

@@ -1,5 +1,6 @@
// src/lib.rs
pub mod db;
pub mod auth;
pub mod server;
pub mod adresar;
pub mod uctovnictvo;

View File

@@ -10,15 +10,22 @@ use crate::server::services::{
TableDefinitionService,
TablesDataService,
TableScriptService,
AuthServiceImpl
};
use common::proto::multieko2::{
adresar::adresar_server::AdresarServer,
uctovnictvo::uctovnictvo_server::UctovnictvoServer,
table_structure::table_structure_service_server::TableStructureServiceServer,
table_definition::table_definition_server::TableDefinitionServer,
tables_data::tables_data_server::TablesDataServer,
table_script::table_script_server::TableScriptServer,
auth::auth_service_server::AuthServiceServer
};
use common::proto::multieko2::adresar::adresar_server::AdresarServer;
use common::proto::multieko2::uctovnictvo::uctovnictvo_server::UctovnictvoServer;
use common::proto::multieko2::table_structure::table_structure_service_server::TableStructureServiceServer;
use common::proto::multieko2::table_definition::table_definition_server::TableDefinitionServer;
use common::proto::multieko2::tables_data::tables_data_server::TablesDataServer;
use common::proto::multieko2::table_script::table_script_server::TableScriptServer;
pub async fn run_server(db_pool: sqlx::PgPool) -> Result<(), Box<dyn std::error::Error>> {
// Initialize JWT for authentication
crate::auth::logic::jwt::init_jwt()?;
let addr = "[::1]:50051".parse()?;
let reflection_service = ReflectionBuilder::configure()
@@ -27,8 +34,9 @@ pub async fn run_server(db_pool: sqlx::PgPool) -> Result<(), Box<dyn std::error:
// Initialize services
let table_definition_service = TableDefinitionService { db_pool: db_pool.clone() };
let tables_data_service = TablesDataService { db_pool: db_pool.clone() }; // Add this
let tables_data_service = TablesDataService { db_pool: db_pool.clone() };
let table_script_service = TableScriptService { db_pool: db_pool.clone() };
let auth_service = AuthServiceImpl { db_pool: db_pool.clone() };
Server::builder()
.add_service(AdresarServer::new(AdresarService { db_pool: db_pool.clone() }))
@@ -37,6 +45,7 @@ pub async fn run_server(db_pool: sqlx::PgPool) -> Result<(), Box<dyn std::error:
.add_service(TableDefinitionServer::new(table_definition_service))
.add_service(TablesDataServer::new(tables_data_service))
.add_service(TableScriptServer::new(table_script_service))
.add_service(AuthServiceServer::new(auth_service))
.add_service(reflection_service)
.serve(addr)
.await?;

View File

@@ -0,0 +1,36 @@
// src/server/services/auth_service.rs
use tonic::{Request, Response, Status};
use common::proto::multieko2::auth::{
auth_service_server::AuthService,
RegisterRequest, AuthResponse,
LoginRequest, LoginResponse
};
use crate::auth::handlers::{
login::login,
register::register
};
use sqlx::PgPool;
#[derive(Debug)]
pub struct AuthServiceImpl {
pub db_pool: PgPool,
}
#[tonic::async_trait]
impl AuthService for AuthServiceImpl {
async fn register(
&self,
request: Request<RegisterRequest>,
) -> Result<Response<AuthResponse>, Status> {
let response = register(&self.db_pool, request.into_inner()).await?;
Ok(response)
}
async fn login(
&self,
request: Request<LoginRequest>,
) -> Result<Response<LoginResponse>, Status> {
let response = login(&self.db_pool, request.into_inner()).await?;
Ok(response)
}
}

View File

@@ -6,6 +6,7 @@ pub mod uctovnictvo_service;
pub mod table_definition_service;
pub mod tables_data_service;
pub mod table_script_service;
pub mod auth_service;
pub use adresar_service::AdresarService;
pub use table_structure_service::TableStructureHandler;
@@ -13,3 +14,4 @@ pub use uctovnictvo_service::UctovnictvoService;
pub use table_definition_service::TableDefinitionService;
pub use tables_data_service::TablesDataService;
pub use table_script_service::TableScriptService;
pub use auth_service::AuthServiceImpl;