From 55b2c4ec5652c49d620bc91bdfe822281adf49c6 Mon Sep 17 00:00:00 2001 From: doryan04 Date: Tue, 6 Feb 2024 23:35:52 +0400 Subject: [PATCH] first commit --- Cargo.lock | 924 ++++++++++++++++++ Cargo.toml | 3 + README.md | 0 src/controller.rs | 47 + src/main.rs | 66 +- src/model.rs | 61 ++ src/ui_src/components/switch.rs | 152 +++ src/ui_src/components/wrapper.rs | 20 + src/ui_src/properties.rs | 118 +++ .../hamming_code_seven_four.rs | 106 ++ src/utils/model_utils/check_correct_input.rs | 11 + .../model_utils/from_string_to_vec_bits.rs | 14 + src/utils/model_utils/processing_input.rs | 10 + src/utils/parse_input.rs | 29 + src/utils/state_controller.rs | 7 + src/view.rs | 151 +++ 16 files changed, 1676 insertions(+), 43 deletions(-) create mode 100644 README.md create mode 100644 src/controller.rs create mode 100644 src/model.rs create mode 100644 src/ui_src/components/switch.rs create mode 100644 src/ui_src/components/wrapper.rs create mode 100644 src/ui_src/properties.rs create mode 100644 src/utils/hamming_code_utils/hamming_code_seven_four.rs create mode 100644 src/utils/model_utils/check_correct_input.rs create mode 100644 src/utils/model_utils/from_string_to_vec_bits.rs create mode 100644 src/utils/model_utils/processing_input.rs create mode 100644 src/utils/parse_input.rs create mode 100644 src/utils/state_controller.rs create mode 100644 src/view.rs diff --git a/Cargo.lock b/Cargo.lock index ccfeb2c..b5b14a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,930 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "addr2line" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a30b2e23b9e17a9f90641c7ab1549cd9b44f296d3ccbf309d2863cfe398a0cb" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + +[[package]] +name = "anyhow" +version = "1.0.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "backtrace" +version = "0.3.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2089b7e3f35b9dd2d0ed921ead4f6d318c27680d4a5bd167b3ee120edb105837" +dependencies = [ + "addr2line", + "cc", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", +] + +[[package]] +name = "bitflags" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" + +[[package]] +name = "bitvec" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c" +dependencies = [ + "funty", + "radium", + "tap", + "wyz", +] + +[[package]] +name = "cairo-rs" +version = "0.18.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f33613627f0dea6a731b0605101fad59ba4f193a52c96c4687728d822605a8a1" +dependencies = [ + "bitflags", + "cairo-sys-rs", + "glib", + "libc", + "once_cell", + "thiserror", +] + +[[package]] +name = "cairo-sys-rs" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "685c9fa8e590b8b3d678873528d83411db17242a73fccaed827770ea0fedda51" +dependencies = [ + "glib-sys", + "libc", + "system-deps", +] + +[[package]] +name = "cc" +version = "1.0.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" +dependencies = [ + "libc", +] + +[[package]] +name = "cfg-expr" +version = "0.15.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03915af431787e6ffdcc74c645077518c6b6e01f80b761e0fbbfa288536311b3" +dependencies = [ + "smallvec", + "target-lexicon", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + +[[package]] +name = "field-offset" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38e2275cc4e4fc009b0669731a1e5ab7ebf11f469eaede2bab9309a5b4d6057f" +dependencies = [ + "memoffset", + "rustc_version", +] + +[[package]] +name = "funty" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" + +[[package]] +name = "futures-channel" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff4dd66668b557604244583e3e1e1eada8c5c2e96a6d0d6653ede395b78bbacb" +dependencies = [ + "futures-core", +] + +[[package]] +name = "futures-core" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb1d22c66e66d9d72e1758f0bd7d4fd0bee04cad842ee34587d68c07e45d088c" + +[[package]] +name = "futures-executor" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f4fb8693db0cf099eadcca0efe2a5a22e4550f98ed16aba6c48700da29597bc" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8bf34a163b5c4c52d0478a4d757da8fb65cabef42ba90515efee0f6f9fa45aaa" + +[[package]] +name = "futures-macro" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53b153fd91e4b0147f4aced87be237c98248656bb01050b96bf3ee89220a8ddb" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.39", +] + +[[package]] +name = "futures-task" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "efd193069b0ddadc69c46389b740bbccdd97203899b48d09c5f7969591d6bae2" + +[[package]] +name = "futures-util" +version = "0.3.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a19526d624e703a3179b3d322efec918b6246ea0fa51d41124525f00f1cc8104" +dependencies = [ + "futures-core", + "futures-macro", + "futures-task", + "pin-project-lite", + "pin-utils", + "slab", +] + +[[package]] +name = "gdk-pixbuf" +version = "0.18.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "446f32b74d22c33b7b258d4af4ffde53c2bf96ca2e29abdf1a785fe59bd6c82c" +dependencies = [ + "gdk-pixbuf-sys", + "gio", + "glib", + "libc", + "once_cell", +] + +[[package]] +name = "gdk-pixbuf-sys" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9839ea644ed9c97a34d129ad56d38a25e6756f99f3a88e15cd39c20629caf7" +dependencies = [ + "gio-sys", + "glib-sys", + "gobject-sys", + "libc", + "system-deps", +] + +[[package]] +name = "gdk4" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7edb019ad581f8ecf8ea8e4baa6df7c483a95b5a59be3140be6a9c3b0c632af6" +dependencies = [ + "cairo-rs", + "gdk-pixbuf", + "gdk4-sys", + "gio", + "glib", + "libc", + "pango", +] + +[[package]] +name = "gdk4-sys" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbab43f332a3cf1df9974da690b5bb0e26720ed09a228178ce52175372dcfef0" +dependencies = [ + "cairo-sys-rs", + "gdk-pixbuf-sys", + "gio-sys", + "glib-sys", + "gobject-sys", + "libc", + "pango-sys", + "pkg-config", + "system-deps", +] + +[[package]] +name = "gimli" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fb8d784f27acf97159b40fc4db5ecd8aa23b9ad5ef69cdd136d3bc80665f0c0" + +[[package]] +name = "gio" +version = "0.18.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d809baf02bdf1b5ef4ad3bf60dd9d4977149db4612b7bbb58e56aef168193b" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-util", + "gio-sys", + "glib", + "libc", + "once_cell", + "pin-project-lite", + "smallvec", + "thiserror", +] + +[[package]] +name = "gio-sys" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37566df850baf5e4cb0dfb78af2e4b9898d817ed9263d1090a2df958c64737d2" +dependencies = [ + "glib-sys", + "gobject-sys", + "libc", + "system-deps", + "winapi", +] + +[[package]] +name = "glib" +version = "0.18.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58cf801b6f7829fa76db37449ab67c9c98a2b1bf21076d9113225621e61a0fa6" +dependencies = [ + "bitflags", + "futures-channel", + "futures-core", + "futures-executor", + "futures-task", + "futures-util", + "gio-sys", + "glib-macros", + "glib-sys", + "gobject-sys", + "libc", + "memchr", + "once_cell", + "smallvec", + "thiserror", +] + +[[package]] +name = "glib-macros" +version = "0.18.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72793962ceece3863c2965d7f10c8786323b17c7adea75a515809fa20ab799a5" +dependencies = [ + "heck", + "proc-macro-crate 2.0.0", + "proc-macro-error", + "proc-macro2", + "quote", + "syn 2.0.39", +] + +[[package]] +name = "glib-sys" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "063ce2eb6a8d0ea93d2bf8ba1957e78dbab6be1c2220dd3daca57d5a9d869898" +dependencies = [ + "libc", + "system-deps", +] + +[[package]] +name = "gobject-sys" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0850127b514d1c4a4654ead6dedadb18198999985908e6ffe4436f53c785ce44" +dependencies = [ + "glib-sys", + "libc", + "system-deps", +] + +[[package]] +name = "graphene-rs" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b2228cda1505613a7a956cca69076892cfbda84fc2b7a62b94a41a272c0c401" +dependencies = [ + "glib", + "graphene-sys", + "libc", +] + +[[package]] +name = "graphene-sys" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc4144cee8fc8788f2a9b73dc5f1d4e1189d1f95305c4cb7bd9c1af1cfa31f59" +dependencies = [ + "glib-sys", + "libc", + "pkg-config", + "system-deps", +] + +[[package]] +name = "gsk4" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d958e351d2f210309b32d081c832d7de0aca0b077aa10d88336c6379bd01f7e" +dependencies = [ + "cairo-rs", + "gdk4", + "glib", + "graphene-rs", + "gsk4-sys", + "libc", + "pango", +] + +[[package]] +name = "gsk4-sys" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12bd9e3effea989f020e8f1ff3fa3b8c63ba93d43b899c11a118868853a56d55" +dependencies = [ + "cairo-sys-rs", + "gdk4-sys", + "glib-sys", + "gobject-sys", + "graphene-sys", + "libc", + "pango-sys", + "system-deps", +] + +[[package]] +name = "gtk4" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aeb51aa3e9728575a053e1f43543cd9992ac2477e1b186ad824fd4adfb70842" +dependencies = [ + "cairo-rs", + "field-offset", + "futures-channel", + "gdk-pixbuf", + "gdk4", + "gio", + "glib", + "graphene-rs", + "gsk4", + "gtk4-macros", + "gtk4-sys", + "libc", + "pango", +] + +[[package]] +name = "gtk4-macros" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d57ec49cf9b657f69a05bca8027cff0a8dfd0c49e812be026fc7311f2163832f" +dependencies = [ + "anyhow", + "proc-macro-crate 1.3.1", + "proc-macro-error", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "gtk4-sys" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54d8c4aa23638ce9faa2caf7e2a27d4a1295af2155c8e8d28c4d4eeca7a65eb8" +dependencies = [ + "cairo-sys-rs", + "gdk-pixbuf-sys", + "gdk4-sys", + "gio-sys", + "glib-sys", + "gobject-sys", + "graphene-sys", + "gsk4-sys", + "libc", + "pango-sys", + "system-deps", +] + +[[package]] +name = "hashbrown" +version = "0.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f93e7192158dbcda357bdec5fb5788eebf8bbac027f3f33e719d29135ae84156" + +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + +[[package]] +name = "hermit-abi" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" + +[[package]] +name = "indexmap" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "libc" +version = "0.2.150" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c" + +[[package]] +name = "memchr" +version = "2.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" + +[[package]] +name = "memoffset" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c" +dependencies = [ + "autocfg", +] + +[[package]] +name = "miniz_oxide" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7810e0be55b428ada41041c41f32c9f1a42817901b4ccf45fa3d4b6561e74c7" +dependencies = [ + "adler", +] + +[[package]] +name = "num_cpus" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" +dependencies = [ + "hermit-abi", + "libc", +] + +[[package]] +name = "object" +version = "0.32.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cf5f9dd3933bd50a9e1f149ec995f39ae2c496d31fd772c1fd45ebc27e902b0" +dependencies = [ + "memchr", +] + +[[package]] +name = "once_cell" +version = "1.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" + +[[package]] +name = "pango" +version = "0.18.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ca27ec1eb0457ab26f3036ea52229edbdb74dee1edd29063f5b9b010e7ebee4" +dependencies = [ + "gio", + "glib", + "libc", + "once_cell", + "pango-sys", +] + +[[package]] +name = "pango-sys" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "436737e391a843e5933d6d9aa102cb126d501e815b83601365a948a518555dc5" +dependencies = [ + "glib-sys", + "gobject-sys", + "libc", + "system-deps", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "pkg-config" +version = "0.3.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" + +[[package]] +name = "proc-macro-crate" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f4c021e1093a56626774e81216a4ce732a735e5bad4868a03f3ed65ca0c3919" +dependencies = [ + "once_cell", + "toml_edit 0.19.15", +] + +[[package]] +name = "proc-macro-crate" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e8366a6159044a37876a2b9817124296703c586a5c92e2c53751fa06d8d43e8" +dependencies = [ + "toml_edit 0.20.7", +] + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn 1.0.109", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "radium" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" + +[[package]] +name = "rustc-demangle" +version = "0.1.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" + +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver", +] + +[[package]] +name = "semver" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090" + +[[package]] +name = "serde" +version = "1.0.193" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.193" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.39", +] + +[[package]] +name = "serde_spanned" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12022b835073e5b11e90a14f86838ceb1c8fb0325b72416845c487ac0fa95e80" +dependencies = [ + "serde", +] + +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + +[[package]] +name = "smallvec" +version = "1.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4dccd0940a2dcdf68d092b8cbab7dc0ad8fa938bf95787e1b916b0e3d0e8e970" + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "system-deps" +version = "6.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a2d580ff6a20c55dfb86be5f9c238f67835d0e81cbdea8bf5680e0897320331" +dependencies = [ + "cfg-expr", + "heck", + "pkg-config", + "toml", + "version-compare", +] + +[[package]] +name = "tap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" + +[[package]] +name = "target-lexicon" +version = "0.12.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c39fd04924ca3a864207c66fc2cd7d22d7c016007f9ce846cbb9326331930a" + +[[package]] +name = "thiserror" +version = "1.0.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.39", +] + +[[package]] +name = "tokio" +version = "1.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0c014766411e834f7af5b8f4cf46257aab4036ca95e9d2c144a10f59ad6f5b9" +dependencies = [ + "backtrace", + "num_cpus", + "pin-project-lite", + "tokio-macros", +] + +[[package]] +name = "tokio-macros" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.39", +] + +[[package]] +name = "toml" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1a195ec8c9da26928f773888e0742ca3ca1040c6cd859c919c9f59c1954ab35" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit 0.21.0", +] + +[[package]] +name = "toml_datetime" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.19.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421" +dependencies = [ + "indexmap", + "toml_datetime", + "winnow", +] + +[[package]] +name = "toml_edit" +version = "0.20.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70f427fce4d84c72b5b732388bf4a9f4531b53f74e2887e3ecb2481f68f66d81" +dependencies = [ + "indexmap", + "toml_datetime", + "winnow", +] + +[[package]] +name = "toml_edit" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d34d383cd00a163b4a5b85053df514d45bc330f6de7737edfe0a93311d1eaa03" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "winnow", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + [[package]] name = "untitled" version = "0.1.0" +dependencies = [ + "bitvec", + "gtk4", + "tokio", +] + +[[package]] +name = "version-compare" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "579a42fc0b8e0c63b76519a339be31bed574929511fa53c1a3acae26eb258f29" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "winnow" +version = "0.5.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "829846f3e3db426d4cee4510841b71a8e58aa2a76b1132579487ae430ccd9c7b" +dependencies = [ + "memchr", +] + +[[package]] +name = "wyz" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" +dependencies = [ + "tap", +] diff --git a/Cargo.toml b/Cargo.toml index 62ea391..883b3b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,6 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +bitvec = "1.0.1" +gtk4 = "0.7.3" +tokio = { version = "1.0.0", features = ["rt", "rt-multi-thread", "macros"] } diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/src/controller.rs b/src/controller.rs new file mode 100644 index 0000000..a59fe79 --- /dev/null +++ b/src/controller.rs @@ -0,0 +1,47 @@ +use gtk4 as gtk; + +use gtk::prelude::*; +use super::switch::*; + +pub struct EventHandler{ + component: C, + callback: F, +} + +impl EventHandler +where F: Fn(&C) + FnOnce(&C) + FnMut(&C){ + pub fn new(component: C, callback: F) -> EventHandler{ + Self{ + component, + callback, + } + } +} + +pub trait BtnEventHandler{ + fn on_click(self) -> (); +} + +pub trait SwEventHandler{ + fn on_toggle(self) -> (); +} + +impl BtnEventHandler for EventHandler + where F: Fn(&C) + FnOnce(&C) + FnMut(&C) + 'static, C: ButtonExt + WidgetExt{ + fn on_click(self) -> () { + self.component.connect_clicked(move |button| { + (self.callback)(button) + }); + } +} + +impl SwEventHandler for EventHandler + where F: Fn(&C) + FnOnce(&C) + FnMut(&C) + 'static, C: SwitchExt + WidgetExt{ + fn on_toggle(self) -> () { + self.component.connect_state_notify(move |switch| { + (self.callback)(switch) + }); + } +} + + diff --git a/src/main.rs b/src/main.rs index c52d54c..d520a8d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,53 +1,33 @@ -mod graph { +use gtk4 as gtk; - use std::collections::{HashMap, HashSet}; - use std::vec::Vec; +use gtk::*; +use view::*; +use gtk::prelude::*; - pub struct Graph { - frame_graph : HashMap>, - flags_graph : HashSet<(u32, bool)> - } +#[path="ui_src/components/switch.rs"] +mod switch; - impl Graph { +#[path="ui_src/components/wrapper.rs"] +mod wrapper; - pub fn new() -> Graph { - Graph{ - frame_graph : HashMap::new(), - flags_graph : HashSet::new(), - } - } +#[path="ui_src/properties.rs"] +mod properties; - pub fn add(&mut self, parent : u32, children : Vec){ +#[path="utils/parse_input.rs"] +mod parse_input; - self.frame_graph.insert(parent, children); - self.flags_graph.insert((parent, false)); - for &i in self.frame_graph.get(&parent).unwrap() { - self.flags_graph.insert((i, false)); - } +#[path="utils/state_controller.rs"] +mod state_controller; - } +mod view; +mod model; +mod controller; - pub fn get(&self) -> &Graph { - self.clone() - } - - } -} - -mod new_class{ - - use super::graph; - -} - - -fn main(){ - - use crate::graph::Graph; - - let mut graph : Graph = Graph::new(); - graph.add(1, vec![2, 3, 4]); - graph.add(2, vec![9, 7, 6, 1]); - graph.add(6, vec![12, 11, 19]); +fn main() { + let app = Application::builder() + .application_id("com.github.gtk-rs.examples.basic") + .build(); + app.connect_activate(ui); + app.run(); } \ No newline at end of file diff --git a/src/model.rs b/src/model.rs new file mode 100644 index 0000000..ed9c5d7 --- /dev/null +++ b/src/model.rs @@ -0,0 +1,61 @@ +use std::ops::Deref; +use bitvec::prelude::{AsBits}; +use crate::model::processing_input::processing_input; +use crate::model::check_correct_input::check_correct_input; +use crate::model::from_string_to_vec_bits::from_string_to_vec_bits; +use crate::model::hamming_code::hamming_code::hamming_decrypt_data; +use crate::model::hamming_code::hamming_code::hamming_encrypt_data; + +#[path="utils/model_utils/processing_input.rs"] +mod processing_input; + +#[path="utils/model_utils/check_correct_input.rs"] +mod check_correct_input; + +#[path="utils/model_utils/from_string_to_vec_bits.rs"] +mod from_string_to_vec_bits; + +#[path="utils/hamming_code_utils/hamming_code_seven_four.rs"] +mod hamming_code; + +pub enum HammingMode{ + Encrypt, + Decrypt +} + +// ошибочная позиция 1 false true true +// ошибочная позиция 2 false false true +// ошибочная позиция 3 true false true +// ошибочная позиция 4 false true false +// ошибочная позиция 5 true true false +// ошибочная позиция 6 true false false +// ошибочная позиция 7 false false false + +pub fn hamming(raw_input: String, mode: HammingMode) -> Result{ + + let length_of_code : usize = if let HammingMode::Encrypt = mode { 4 } else { 7 }; + + let prepared_input : String = processing_input(&raw_input); + + let (fc, sc) = check_correct_input(&raw_input, &prepared_input, length_of_code); + + if !fc || !sc { + + Err("Ошибка. Проверьте корректность ввода.".to_string()) + + } else { + + let mut data : String = String::new(); + + let prepared_data: Vec = from_string_to_vec_bits(prepared_input); + + match mode { + HammingMode::Encrypt => hamming_encrypt_data(&prepared_data, &mut data, length_of_code), + HammingMode::Decrypt => hamming_decrypt_data(&prepared_data, &mut data, length_of_code), + } + + return Ok(data); + + } +} + diff --git a/src/ui_src/components/switch.rs b/src/ui_src/components/switch.rs new file mode 100644 index 0000000..364fbc4 --- /dev/null +++ b/src/ui_src/components/switch.rs @@ -0,0 +1,152 @@ +use gtk4 as gtk; + +use gtk::*; +use gtk::prelude::*; + +use glib::{ + signal::{connect_raw, SignalHandlerId}, + translate::*, +}; +use std::boxed::Box as Box_; + +mod wrapper; + +mod sealed { + pub trait Sealed {} + impl> Sealed for T {} +} + +pub trait SwitchExt: IsA + sealed::Sealed + 'static { + #[doc(alias = "gtk_switch_get_active")] + #[doc(alias = "get_active")] + fn is_active(&self) -> bool { + unsafe { from_glib(ffi::gtk_switch_get_active(self.as_ref().to_glib_none().0)) } + } + + #[doc(alias = "gtk_switch_get_state")] + #[doc(alias = "get_state")] + fn state(&self) -> bool { + unsafe { from_glib(ffi::gtk_switch_get_state(self.as_ref().to_glib_none().0)) } + } + + #[doc(alias = "gtk_switch_set_active")] + fn set_active(&self, is_active: bool) { + unsafe { + ffi::gtk_switch_set_active(self.as_ref().to_glib_none().0, is_active.into_glib()); + } + } + + #[doc(alias = "gtk_switch_set_state")] + fn set_state(&self, state: bool) { + unsafe { + ffi::gtk_switch_set_state(self.as_ref().to_glib_none().0, state.into_glib()); + } + } + + #[doc(alias = "activate")] + fn connect_activate(&self, f: F) -> SignalHandlerId { + unsafe extern "C" fn activate_trampoline, F: Fn(&P) + 'static>( + this: *mut ffi::GtkSwitch, + f: glib::ffi::gpointer, + ) { + let f: &F = &*(f as *const F); + f(Switch::from_glib_borrow(this).unsafe_cast_ref()) + } + unsafe { + let f: Box_ = Box_::new(f); + connect_raw( + self.as_ptr() as *mut _, + b"activate\0".as_ptr() as *const _, + Some(std::mem::transmute::<_, unsafe extern "C" fn()>( + activate_trampoline:: as *const (), + )), + Box_::into_raw(f), + ) + } + } + + fn emit_activate(&self) { + self.emit_by_name::<()>("activate", &[]); + } + + #[doc(alias = "state-set")] + fn connect_state_set glib::Propagation + 'static>( + &self, + f: F, + ) -> SignalHandlerId { + unsafe extern "C" fn state_set_trampoline< + P: IsA, + F: Fn(&P, bool) -> glib::Propagation + 'static, + >( + this: *mut ffi::GtkSwitch, + state: glib::ffi::gboolean, + f: glib::ffi::gpointer, + ) -> glib::ffi::gboolean { + let f: &F = &*(f as *const F); + f( + Switch::from_glib_borrow(this).unsafe_cast_ref(), + from_glib(state), + ) + .into_glib() + } + unsafe { + let f: Box_ = Box_::new(f); + connect_raw( + self.as_ptr() as *mut _, + b"state-set\0".as_ptr() as *const _, + Some(std::mem::transmute::<_, unsafe extern "C" fn()>( + state_set_trampoline:: as *const (), + )), + Box_::into_raw(f), + ) + } + } + + #[doc(alias = "active")] + fn connect_active_notify(&self, f: F) -> SignalHandlerId { + unsafe extern "C" fn notify_active_trampoline, F: Fn(&P) + 'static>( + this: *mut ffi::GtkSwitch, + _param_spec: glib::ffi::gpointer, + f: glib::ffi::gpointer, + ) { + let f: &F = &*(f as *const F); + f(Switch::from_glib_borrow(this).unsafe_cast_ref()) + } + unsafe { + let f: Box_ = Box_::new(f); + connect_raw( + self.as_ptr() as *mut _, + b"notify::active\0".as_ptr() as *const _, + Some(std::mem::transmute::<_, unsafe extern "C" fn()>( + notify_active_trampoline:: as *const (), + )), + Box_::into_raw(f), + ) + } + } + + #[doc(alias = "state")] + fn connect_state_notify(&self, f: F) -> SignalHandlerId { + unsafe extern "C" fn notify_state_trampoline, F: Fn(&P) + 'static>( + this: *mut ffi::GtkSwitch, + _param_spec: glib::ffi::gpointer, + f: glib::ffi::gpointer, + ) { + let f: &F = &*(f as *const F); + f(Switch::from_glib_borrow(this).unsafe_cast_ref()) + } + unsafe { + let f: Box_ = Box_::new(f); + connect_raw( + self.as_ptr() as *mut _, + b"notify::state\0".as_ptr() as *const _, + Some(std::mem::transmute::<_, unsafe extern "C" fn()>( + notify_state_trampoline:: as *const (), + )), + Box_::into_raw(f), + ) + } + } +} + +impl> SwitchExt for O {} diff --git a/src/ui_src/components/wrapper.rs b/src/ui_src/components/wrapper.rs new file mode 100644 index 0000000..28fdd3c --- /dev/null +++ b/src/ui_src/components/wrapper.rs @@ -0,0 +1,20 @@ +use gtk4 as gtk; + +use gtk::Orientation; +use gtk::builders::BoxBuilder; +use gtk::Box; + +#[allow(dead_code)] +pub struct Wrapper; + +impl Wrapper{ + + pub fn row_builder() -> BoxBuilder { + Box::builder().orientation(Orientation::Vertical) + } + + pub fn col_builder() -> BoxBuilder { + Box::builder().orientation(Orientation::Horizontal) + } + +} diff --git a/src/ui_src/properties.rs b/src/ui_src/properties.rs new file mode 100644 index 0000000..a3685a5 --- /dev/null +++ b/src/ui_src/properties.rs @@ -0,0 +1,118 @@ +use gtk4 as gtk; + +use gtk::{Align}; +use gtk::builders::*; + +/** + * Types + */ + +pub type Margin = (i32, i32, i32, i32); + +/** + * Enums + */ + +pub enum MarginData{ + EqualsMargin(i32), + MultipleMargin(Margin), +} + +/** + * Structs + */ + +#[allow(dead_code)] + +pub struct Size { + pub width: i32, + pub height: i32 +} + +pub struct Alignment { + pub horizontal: Align, + pub vertical : Align +} + +/** + * Traits + */ + +pub trait Setters{ + fn set_margin(self, margin: MarginData) -> Self; + fn set_align(self, align: Alignment) -> Self; +} + +pub trait TextViewSetters{ + fn set_text_view_margin(self, margin: MarginData) -> Self; +} + +impl TextViewSetters for TextViewBuilder{ + fn set_text_view_margin(self, margin: MarginData) -> Self{ + match margin{ + MarginData::EqualsMargin(margin) => + self.top_margin(margin) + .left_margin(margin) + .bottom_margin(margin) + .right_margin(margin), + MarginData::MultipleMargin(margins) => + self.top_margin(margins.0) + .left_margin(margins.1) + .bottom_margin(margins.2) + .right_margin(margins.3), + } + } +} + +/** + * Macros + */ + +macro_rules! impl_setters { + ($($t:ty),+) => { + $( + impl Setters for $t { + fn set_margin(self, margin: MarginData) -> Self{ + match margin{ + MarginData::EqualsMargin(margin) => + self.margin_top(margin) + .margin_start(margin) + .margin_bottom(margin) + .margin_end(margin), + MarginData::MultipleMargin(margins) => + self.margin_top(margins.0) + .margin_start(margins.1) + .margin_bottom(margins.2) + .margin_end(margins.3), + } + } + fn set_align(self, align: Alignment) -> Self { + self.halign(align.horizontal) + .valign(align.vertical) + } + } + )* + } +} + +impl_setters!{ButtonBuilder, EntryBuilder, TextViewBuilder, + BoxBuilder, SwitchBuilder, FrameBuilder, LabelBuilder} + +#[allow(dead_code)] +impl Size{ + pub fn new(w: i32, h: i32) -> Size{ + Size{ + width: w, + height: h, + } + } +} + +impl Alignment{ + pub fn new(horizontal: Align, vertical : Align) -> Alignment{ + Alignment{ + horizontal, + vertical, + } + } +} diff --git a/src/utils/hamming_code_utils/hamming_code_seven_four.rs b/src/utils/hamming_code_utils/hamming_code_seven_four.rs new file mode 100644 index 0000000..916f5ea --- /dev/null +++ b/src/utils/hamming_code_utils/hamming_code_seven_four.rs @@ -0,0 +1,106 @@ +// ошибочная позиция 1 false true true +// ошибочная позиция 2 false false true +// ошибочная позиция 3 true false true +// ошибочная позиция 4 false true false +// ошибочная позиция 5 true true false +// ошибочная позиция 6 true false false +// ошибочная позиция 7 false false false + +pub mod hamming_code{ + use std::borrow::Borrow; + use std::collections::HashMap; + + pub fn hamming_encrypt_data(data: &Vec, result_string: &mut String, length_of_code: usize) -> () { + let mut i : usize = length_of_code; + + while i <= data.len(){ + + let data_bits = &data[i - length_of_code..i]; + let (check_bit_1, check_bit_2, check_bit_3) = ( + data_bits[0] ^ data_bits[1] ^ data_bits[3], + data_bits[0] ^ data_bits[2] ^ data_bits[3], + data_bits[1] ^ data_bits[2] ^ data_bits[3] + ); + result_string.push_str(&*format!("{check_bit_1}{}{check_bit_2}{}{check_bit_3}{}{} ", + data_bits[0], + data_bits[1], + data_bits[2], + data_bits[3])); + i += length_of_code; + + } + } + + pub fn hamming_decrypt_data(data: &Vec, result_string: &mut String, length_of_code: usize) -> () { + let mut i : usize = length_of_code; + + let syndromes : HashMap = HashMap::from( + [ + (1, (false, true, true)), + (2, (false, false, true)), + (3, (true, false, true)), + (4, (false, true, false)), + (5, (true, true, false)), + (6, (true, false, false)), + (7, (false, false, false)), + ] + ); + + let mut errors : String = String::new(); + + while i <= data.len(){ + + let mut data_bits = &data[i - length_of_code..i]; + + let checked_bits : (bool, bool, bool) = + ( + (data_bits[1] ^ data_bits[3] ^ data_bits[6]) == data_bits[0], + (data_bits[1] ^ data_bits[5] ^ data_bits[6]) == data_bits[2], + (data_bits[3] ^ data_bits[5] ^ data_bits[6]) == data_bits[4] + ); + + match checked_bits { + (true, true, true) => { + i += length_of_code; + continue; + }, + _ => { + + let error_position = syndromes + .iter() + .find(move |&(&error_position, &error)| error == checked_bits). + unwrap().0; + + let correctly_code : Vec = data_bits + .iter() + .enumerate() + .map(|(index, bit)| { + if index == error_position - 1 { + if *bit == 1u8 { 0u8 } else { 1u8 } + } else { + *bit + } + }).collect(); + + let error = format!("Ошибка в коде {} {:?}, позиция ошибки {}, корректный код: {:?}; \n", + i / 7, + &data_bits, + error_position, + correctly_code + ); + + errors.push_str(error.as_str()); + + i += length_of_code; + + } + } + } + + if errors.len() == 0 { + result_string.push_str("Все коды корректны."); + } else { + result_string.push_str(errors.as_str()) + } + } +} \ No newline at end of file diff --git a/src/utils/model_utils/check_correct_input.rs b/src/utils/model_utils/check_correct_input.rs new file mode 100644 index 0000000..d9f7b79 --- /dev/null +++ b/src/utils/model_utils/check_correct_input.rs @@ -0,0 +1,11 @@ +pub fn check_correct_input(input: &String, prepared_input: &String, l: usize) -> (bool, bool){ + + let first_condition = input + .chars() + .all(|c| {c == '1' || c == '0' || c == ' '}); + + let second_condition = prepared_input.len() % l == 0; + + (first_condition, second_condition) + +} \ No newline at end of file diff --git a/src/utils/model_utils/from_string_to_vec_bits.rs b/src/utils/model_utils/from_string_to_vec_bits.rs new file mode 100644 index 0000000..1747bb2 --- /dev/null +++ b/src/utils/model_utils/from_string_to_vec_bits.rs @@ -0,0 +1,14 @@ +use std::ops::Deref; +use bitvec::order::Lsb0; +use bitvec::prelude::AsBits; + +pub fn from_string_to_vec_bits(raw_data: String) -> Vec{ + + raw_data + .as_bits::() + .iter() + .step_by(8) + .map(|x| *x.deref() as u8) + .collect() + +} \ No newline at end of file diff --git a/src/utils/model_utils/processing_input.rs b/src/utils/model_utils/processing_input.rs new file mode 100644 index 0000000..487842b --- /dev/null +++ b/src/utils/model_utils/processing_input.rs @@ -0,0 +1,10 @@ +pub fn processing_input(input : &String) -> String { + + input + .split_ascii_whitespace() + .filter(|&x| { + x != "" + }) + .fold(String::new(), |c: String, n: &str| { c + n }) + +} \ No newline at end of file diff --git a/src/utils/parse_input.rs b/src/utils/parse_input.rs new file mode 100644 index 0000000..24a3719 --- /dev/null +++ b/src/utils/parse_input.rs @@ -0,0 +1,29 @@ +use gtk4 as gtk; + +use gtk::*; +use gtk::prelude::*; +use super::model::*; + +pub fn parse_input(input : &TextView, output : &TextView, mode: bool) -> (){ + + let (iter_start, iter_end) = input.buffer().bounds(); + let parsed_input : String = input + .buffer() + .text(&iter_start, &iter_end, false) + .to_string() + .trim() + .parse() + .unwrap(); + + let operation = if mode == false { + HammingMode::Encrypt + } else { + HammingMode::Decrypt + }; + + match hamming(parsed_input, operation) { + Ok(res) => output.buffer().set_text(res.trim_end()), + Err(rej) => output.buffer().set_text(rej.as_str()), + } + +} \ No newline at end of file diff --git a/src/utils/state_controller.rs b/src/utils/state_controller.rs new file mode 100644 index 0000000..aebedf1 --- /dev/null +++ b/src/utils/state_controller.rs @@ -0,0 +1,7 @@ +use gtk4 as gtk; +use gtk::*; + +pub fn state_controller(switch: &Switch, label: &Label) -> (){ + if switch.state() == true { label.set_label("Режим: проверка"); } + else { label.set_label("Режим: кодирование"); } +} \ No newline at end of file diff --git a/src/view.rs b/src/view.rs new file mode 100644 index 0000000..b6d4edc --- /dev/null +++ b/src/view.rs @@ -0,0 +1,151 @@ +use gtk4 as gtk; + +use gtk::*; +use gtk::prelude::*; +use super::wrapper::*; +use super::controller::*; +use super::parse_input::parse_input; +use super::state_controller::state_controller; +use super::properties::{MarginData, Setters, TextViewSetters, Alignment}; + +fn clearing(output : &TextView, input: &TextView){ + input.buffer().set_text(""); + output.buffer().set_text(""); +} + +pub fn laboratory_work_first_section(wrapper: &Box) -> (){ + + // input + + let hamming_text_view_input_label = Label::builder() + .halign(Align::Start) + .set_margin(MarginData::MultipleMargin((10, 5, 0, 0))) + .label(String::from("Поле ввода для кода:")) + .build(); + + let hamming_text_view_input = TextView::builder() + .monospace(true) + .set_text_view_margin(MarginData::EqualsMargin(6)) + .wrap_mode(WrapMode::Word) + .build(); + + let hamming_text_view_input_frame = Frame::builder() + .child(&hamming_text_view_input) + .height_request(64) + .set_margin(MarginData::MultipleMargin((0, 5, 0, 5))) + .build(); + + // output + + let hamming_text_view_output_label = Label::builder() + .halign(Align::Start) + .set_margin(MarginData::MultipleMargin((10, 5, 0, 0))) + .label(String::from("Результат:")) + .build(); + + let hamming_text_view_output = TextView::builder() + .monospace(true) + .editable(false) + .set_text_view_margin(MarginData::EqualsMargin(6)) + .wrap_mode(WrapMode::Word) + .build(); + + let hamming_text_view_output_frame = Frame::builder() + .child(&hamming_text_view_output) + .height_request(64) + .set_margin(MarginData::MultipleMargin((0, 5, 0, 5))) + .build(); + + // interactive panel + + let hamming_crypt_button = Button::builder() + .set_align(Alignment::new(Align::Fill, Align::Fill)) + .label("Выполнить") + .build(); + + let crypt_mode_switch = Switch::new(); + + let crypt_mode_label = Label::builder() + .label("Режим: кодирование") + .build(); + + // references for binding actions + + let crypt_mode_label_to_handle = crypt_mode_label.clone(); + let crypt_mode_switch_to_handle = crypt_mode_switch.clone(); + let text_view_input_for_parse = hamming_text_view_input.clone(); + let text_view_output_for_output = hamming_text_view_output.clone(); + + // actions + + EventHandler::new( + hamming_crypt_button.clone(), + move |_| { + parse_input( + &text_view_input_for_parse, + &text_view_output_for_output, + crypt_mode_switch_to_handle.state() + ) + }).on_click(); + + EventHandler::new( + crypt_mode_switch.clone(), + move |s| { + state_controller(s, &crypt_mode_label_to_handle); + }).on_toggle(); + + // wrappers + + let crypt_mode_wrapper = Wrapper::col_builder() + .set_align(Alignment::new(Align::Fill, Align::Center)) + .hexpand(true) + .spacing(10) + .build(); + + let interactive_components_wrapper = Wrapper::col_builder() + .set_align(Alignment::new(Align::Fill, Align::Fill)) + .set_margin(MarginData::MultipleMargin((0, 5, 0, 5))) + .spacing(10) + .build(); + + // separators + + let separator = Separator::builder() + .orientation(Orientation::Horizontal) + .build(); + + crypt_mode_wrapper.append(&crypt_mode_switch); + crypt_mode_wrapper.append(&crypt_mode_label); + + interactive_components_wrapper.append(&crypt_mode_wrapper); + interactive_components_wrapper.append(&hamming_crypt_button); + + wrapper.append(&hamming_text_view_input_label); + wrapper.append(&hamming_text_view_input_frame); + wrapper.append(&interactive_components_wrapper); + wrapper.append(&hamming_text_view_output_label); + wrapper.append(&hamming_text_view_output_frame); + wrapper.append(&separator); + +} + +pub fn ui(application: &Application) { + + let mutual_wrapper = Wrapper::row_builder() + .set_align(Alignment::new(Align::Fill, Align::Fill)) + .set_margin(MarginData::EqualsMargin(15)) + .spacing(10) + .build(); + + laboratory_work_first_section(&mutual_wrapper); + + let window = ApplicationWindow::builder() + .title("Комплексная программа для лаб. работ") + .width_request(650) + .height_request(400) + .application(application) + .child(&mutual_wrapper) + .build(); + + window.show(); +} \ No newline at end of file