From a1134715f5a0eb93afa7717e521d753e907a4f77 Mon Sep 17 00:00:00 2001 From: martin machiebe Date: Fri, 9 May 2025 18:18:31 +0100 Subject: [PATCH 1/4] csv assignment --- Cargo.lock | 545 +++++++++++++++++++++- Cargo.toml | 9 +- data/csv_data.csv | 4 + data/testing.csv | 2 + data/write_csv_data.csv | 2 + src/assignment_task/csv_data_processor.rs | 96 ++++ src/assignment_task/mod.rs | 1 + src/main.rs | 32 +- src/session_04/debugging.rs | 13 + src/session_04/logging.rs | 45 ++ src/session_04/mod.rs | 3 + src/session_04/serde.rs | 49 ++ 12 files changed, 795 insertions(+), 6 deletions(-) create mode 100644 data/csv_data.csv create mode 100644 data/testing.csv create mode 100644 data/write_csv_data.csv create mode 100644 src/assignment_task/csv_data_processor.rs create mode 100644 src/assignment_task/mod.rs create mode 100644 src/session_04/debugging.rs create mode 100644 src/session_04/logging.rs create mode 100644 src/session_04/mod.rs create mode 100644 src/session_04/serde.rs diff --git a/Cargo.lock b/Cargo.lock index e087c80..fd7c7ae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,7 +1,550 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "bitflags" +version = "2.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "csv" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acdc4883a9c96732e4733212c01447ebd805833b7275a73ca3ee080fd77afdaf" +dependencies = [ + "csv-core", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "csv-core" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d02f3b0da4c6504f86e9cd789d8dbafab48c2321be74e9987593de5a894d93d" +dependencies = [ + "memchr", +] + +[[package]] +name = "env_logger" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cd405aab171cb85d6735e5c8d9db038c17d3ca007a4d2c25f337935c3d90580" +dependencies = [ + "humantime", + "is-terminal", + "log", + "regex", + "termcolor", +] + +[[package]] +name = "getrandom" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73fea8450eea4bac3940448fb7ae50d91f034f941199fcd9d909a5a07aa455f0" +dependencies = [ + "cfg-if", + "libc", + "r-efi", + "wasi", +] + +[[package]] +name = "hermit-abi" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f154ce46856750ed433c8649605bf7ed2de3bc35fd9d2a9f30cddd873c80cb08" + +[[package]] +name = "humantime" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b112acc8b3adf4b107a8ec20977da0273a8c386765a3ec0229bd500a1443f9f" + +[[package]] +name = "is-terminal" +version = "0.4.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e04d7f318608d35d4b61ddd75cbdaee86b023ebe2bd5a66ee0915f0bf93095a9" +dependencies = [ + "hermit-abi", + "libc", + "windows-sys", +] + +[[package]] +name = "itoa" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "libc" +version = "0.2.172" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa" + +[[package]] +name = "log" +version = "0.4.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + +[[package]] +name = "pin-project-lite" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "proc-macro2" +version = "1.0.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5" + +[[package]] +name = "rand" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fbfd9d094a40bf3ae768db9361049ace4c0e04a4fd6b359518bd7b73a73dd97" +dependencies = [ + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38" +dependencies = [ + "getrandom", +] + +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" [[package]] name = "rust_sessions" version = "0.1.0" +dependencies = [ + "csv", + "env_logger", + "log", + "rand", + "serde", + "serde_json", + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "ryu" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" + +[[package]] +name = "serde" +version = "1.0.219" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.219" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.140" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] + +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "smallvec" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8917285742e9f3e1683f0a9c4e6b57960b7314d0b08d30d1ecd426713ee2eee9" + +[[package]] +name = "syn" +version = "2.0.101" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ce2b7fc941b3a24138a0a7cf8e858bfc6a992e7978a068a5c760deb0ed43caf" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + +[[package]] +name = "tracing" +version = "0.1.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" +dependencies = [ + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8189decb5ac0fa7bc8b96b7cb9b2701d60d48805aca84a238004d665fcc4008" +dependencies = [ + "nu-ansi-term", + "sharded-slab", + "smallvec", + "thread_local", + "tracing-core", + "tracing-log", +] + +[[package]] +name = "unicode-ident" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" + +[[package]] +name = "valuable" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" + +[[package]] +name = "wasi" +version = "0.14.2+wasi-0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3" +dependencies = [ + "wit-bindgen-rt", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" +dependencies = [ + "windows-sys", +] + +[[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 = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "wit-bindgen-rt" +version = "0.39.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1" +dependencies = [ + "bitflags", +] + +[[package]] +name = "zerocopy" +version = "0.8.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1702d9583232ddb9174e01bb7c15a2ab8fb1bc6f227aa1233858c351a3ba0cb" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28a6e20d751156648aa063f3800b706ee209a32c0b4d9f24be3d980b01be55ef" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml index 425146c..0c1f0ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,11 @@ edition = "2021" path = "src/lib.rs" [dependencies] - +log = "0.4" +env_logger = "0.10" +tracing = "0.1" +tracing-subscriber = "0.3" +serde = {version = "1.0.219", features = ["derive"]} +serde_json = "1.0.140" +rand = "0.9.1" +csv = "1.3.1" diff --git a/data/csv_data.csv b/data/csv_data.csv new file mode 100644 index 0000000..ab538d1 --- /dev/null +++ b/data/csv_data.csv @@ -0,0 +1,4 @@ +name,email,age +Alice,alice@alice.com,25 +Bob,bob@bob.com,35 +Charlie,charlie@charles.com,40 diff --git a/data/testing.csv b/data/testing.csv new file mode 100644 index 0000000..970fb19 --- /dev/null +++ b/data/testing.csv @@ -0,0 +1,2 @@ +name,email,age +martin,martin@gmail.edu,32 diff --git a/data/write_csv_data.csv b/data/write_csv_data.csv new file mode 100644 index 0000000..970fb19 --- /dev/null +++ b/data/write_csv_data.csv @@ -0,0 +1,2 @@ +name,email,age +martin,martin@gmail.edu,32 diff --git a/src/assignment_task/csv_data_processor.rs b/src/assignment_task/csv_data_processor.rs new file mode 100644 index 0000000..d18d60b --- /dev/null +++ b/src/assignment_task/csv_data_processor.rs @@ -0,0 +1,96 @@ +use csv::{ReaderBuilder, Writer}; +use std::{error::Error, io}; +use std::fs::File; +use serde::{Deserialize, Serialize}; + +// type Record = (String, String, u64); + +#[derive(Debug, Deserialize)] +struct Record { + name: String, + email: String, + age: u64, +} + +pub fn csv_file_reader() -> Result<(), Box> { + // path to the csv file + let file_path = "data/csv_data.csv"; + + // this line opens the csv file from the path passed into it + let file = File::open(file_path)?; + + // this creates a new instance of ReaderBuilder struct allows you to customize how the csv data is read + // .has_headers checks if the .csv file has headers and treats the first row as it + // .from_reader finalizes the ocnfiguration, creates a Reader instance which is used to read & parse the csv file. + let mut rdr = ReaderBuilder::new().has_headers(true).from_reader(file); + + // this line iterates over the records in the csv file + for results in rdr.records() { + let record = results?; + // this line checks if the record has a 3 fields and if it's an integer + let age = record[2].parse::()?; + + // this line checks if the age is greater than 25 and then filters the record + if age > 25 { + println!("This is the record {:?}", record); + } + + } + + Ok(()) +} + +pub fn csv_file_writer() -> Result<(), Box> { + // path to the csv file + let file_path = "data/testing.csv"; + + // this line opens the csv file from the path passed into it + // let file = File::open(file_path)?; + + let file = File::create(file_path)?; + let mut writer = Writer::from_writer(file); + + writer.write_record(&["name", "email", "age"])?; + writer.write_record(&["martin", "martin@gmail.edu", "32"])?; + + + writer.flush()?; + + println!("CSV data has been written successfully."); + + let read_file_path = "data/testing.csv"; + + let file_read = File::open(read_file_path)?; + + let mut rder = ReaderBuilder::new().has_headers(true).from_reader(file_read); + + for res in rder.records() { + + println!("This is the {:?}", res); + } + + Ok(()) +} + + +pub fn csv_file_reader_using_serde() -> Result<(), Box> { + // path to the csv file + let file_path = "data/csv_data.csv"; + + // this line opens the csv file from the path passed into it + let file = File::open(file_path)?; + + // this creates a new instance of ReaderBuilder struct allows you to customize how the csv data is read + // .has_headers checks if the .csv file has headers and treats the first row as it + // .from_reader finalizes the ocnfiguration, creates a Reader instance which is used to read & parse the csv file. + let mut rdr = ReaderBuilder::new().has_headers(true).from_reader(file); + + // this line iterates over the records in the csv file + for results in rdr.deserialize() { + let record: Record = results?; + println!("This is the record {:#?}", record); + + } + + Ok(()) +} diff --git a/src/assignment_task/mod.rs b/src/assignment_task/mod.rs new file mode 100644 index 0000000..9863ba3 --- /dev/null +++ b/src/assignment_task/mod.rs @@ -0,0 +1 @@ +pub mod csv_data_processor; diff --git a/src/main.rs b/src/main.rs index 07b2faa..20a3869 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,8 +15,14 @@ // mod todo; // mod unsigned; // mod user_struct; -mod day_01; -use day_01::{enums, structs}; +// mod day_01; +// use day_01::{enums, structs}; + +use std::os::unix::process; + +mod assignment_task; +mod session_04; +extern crate tracing; // fn read_line() -> String { // let mut buffer = String::new(); @@ -65,8 +71,26 @@ fn main() { // string::strings(); // user_struct::user_registry(); // collections::collections(); - structs::user_details(); - enums::enums_details(); + + // session_04::logging::log(); + // session_04::serde::using_serde(); + + if let Err(err) = assignment_task::csv_data_processor::csv_file_reader() { + println!("error running file {}", err); + std::process::exit(1); + } + + if let Err(err) = assignment_task::csv_data_processor::csv_file_writer() { + println!("error running file {}", err); + std::process::exit(1); + } + + if let Err(err) = assignment_task::csv_data_processor::csv_file_reader_using_serde() { + println!("error running file {}", err); + std::process::exit(1); + } + + // session_04::debugging::debugging(); // //Book creation // let book = constructor::Book::new("The Rust Programming Language", "Steve Klabnik", 2019); diff --git a/src/session_04/debugging.rs b/src/session_04/debugging.rs new file mode 100644 index 0000000..f0879cd --- /dev/null +++ b/src/session_04/debugging.rs @@ -0,0 +1,13 @@ +pub fn debugging() { + let x = 5; + + dbg!(x); + println!("The value of x is {x}"); + + // debug_only_function(); +} + +#[cfg(debug_assertions)] +fn debug_only_function() { + println!("This only runs in debug builds"); +} diff --git a/src/session_04/logging.rs b/src/session_04/logging.rs new file mode 100644 index 0000000..8cce502 --- /dev/null +++ b/src/session_04/logging.rs @@ -0,0 +1,45 @@ +use log::{debug, error, info, trace, warn}; +// use tracing::{warn, info, debug, error, info_span, trace}; +// use tracing_subscriber; + +pub fn log() { + env_logger::init(); + + info!("Application starting up"); + + if let Err(e) = do_something() { + error!("Operation failed: {}", e); + } + + debug!("Detailed state information: {:?}", get_state()); + + fn do_something() -> Result { + Ok("this is a string".to_string()) + } + + fn get_state() -> String { + "hello world".to_string() + } + + warn!("Resource usage is high"); + trace!("this is trace"); + + // tracing(); +} + +// fn tracing() { +// tracing_subscriber::fmt::init(); + +// let span = info_span!("processing", user_id = 42); +// let _guard = span.enter(); + +// info!(task = "initialization", "Application starting up"); + +// if let Err(e) = do_something() { +// error!("Operation failed {}", e); +// } + +// fn do_something() -> Result { +// Ok("this is a string".to_string()) +// } +// } diff --git a/src/session_04/mod.rs b/src/session_04/mod.rs new file mode 100644 index 0000000..ddbae11 --- /dev/null +++ b/src/session_04/mod.rs @@ -0,0 +1,3 @@ +pub mod debugging; +pub mod logging; +pub mod serde; diff --git a/src/session_04/serde.rs b/src/session_04/serde.rs new file mode 100644 index 0000000..71b1ad7 --- /dev/null +++ b/src/session_04/serde.rs @@ -0,0 +1,49 @@ +use rand::seq::SliceRandom; +use rand::Rng; +use serde::{Deserialize, Serialize}; +use serde_json; + +#[derive(Serialize, Deserialize, Debug)] +struct User { + id: u32, + name: String, +} + +pub fn using_serde() -> Result<(), serde_json::Error> { + let user = User { + id: 1, + name: "marto".to_string(), + }; + + rand(); + + let user_json = serde_json::to_string(&user)?; + println!("This is the new {}", user_json); + + let user_de_json: User = serde_json::from_str(&user_json)?; + println!("This is {:?}", user_de_json); + Ok(()) +} + +fn rand() { + // Generate a random number + let mut rng = rand::thread_rng(); + let random_num: u32 = rng.gen_range(1..=100); + println!("Random number: {}", random_num); + + // Shuffle a vector + let mut numbers = vec![1, 2, 3, 4, 5]; + numbers.shuffle(&mut rng); + println!("Shuffled: {:?}", numbers); + + // Generate a random boolean + //The gen() method is generic and can generate + //random values for any type that implements the + //rand::distributions::Distribution trait. The + //rand crate provides an implementation of this + //trait for many types, including primitive + //types like bool, u32, f64, etc. + + let coin_flip: bool = rng.gen(); + println!("Coin flip: {}", if coin_flip { "Heads" } else { "Tails" }); +} From 8e46f0715ca4ad52c7af5344bc95f65220631ce7 Mon Sep 17 00:00:00 2001 From: martin machiebe Date: Fri, 9 May 2025 18:24:05 +0100 Subject: [PATCH 2/4] feat: logging and debugging --- data/csv_data.csv | 4 - data/testing.csv | 2 - data/write_csv_data.csv | 2 - src/assignment_task/csv_data_processor.rs | 96 ----------------------- src/assignment_task/mod.rs | 1 - src/main.rs | 15 ---- 6 files changed, 120 deletions(-) delete mode 100644 data/csv_data.csv delete mode 100644 data/testing.csv delete mode 100644 data/write_csv_data.csv delete mode 100644 src/assignment_task/csv_data_processor.rs delete mode 100644 src/assignment_task/mod.rs diff --git a/data/csv_data.csv b/data/csv_data.csv deleted file mode 100644 index ab538d1..0000000 --- a/data/csv_data.csv +++ /dev/null @@ -1,4 +0,0 @@ -name,email,age -Alice,alice@alice.com,25 -Bob,bob@bob.com,35 -Charlie,charlie@charles.com,40 diff --git a/data/testing.csv b/data/testing.csv deleted file mode 100644 index 970fb19..0000000 --- a/data/testing.csv +++ /dev/null @@ -1,2 +0,0 @@ -name,email,age -martin,martin@gmail.edu,32 diff --git a/data/write_csv_data.csv b/data/write_csv_data.csv deleted file mode 100644 index 970fb19..0000000 --- a/data/write_csv_data.csv +++ /dev/null @@ -1,2 +0,0 @@ -name,email,age -martin,martin@gmail.edu,32 diff --git a/src/assignment_task/csv_data_processor.rs b/src/assignment_task/csv_data_processor.rs deleted file mode 100644 index d18d60b..0000000 --- a/src/assignment_task/csv_data_processor.rs +++ /dev/null @@ -1,96 +0,0 @@ -use csv::{ReaderBuilder, Writer}; -use std::{error::Error, io}; -use std::fs::File; -use serde::{Deserialize, Serialize}; - -// type Record = (String, String, u64); - -#[derive(Debug, Deserialize)] -struct Record { - name: String, - email: String, - age: u64, -} - -pub fn csv_file_reader() -> Result<(), Box> { - // path to the csv file - let file_path = "data/csv_data.csv"; - - // this line opens the csv file from the path passed into it - let file = File::open(file_path)?; - - // this creates a new instance of ReaderBuilder struct allows you to customize how the csv data is read - // .has_headers checks if the .csv file has headers and treats the first row as it - // .from_reader finalizes the ocnfiguration, creates a Reader instance which is used to read & parse the csv file. - let mut rdr = ReaderBuilder::new().has_headers(true).from_reader(file); - - // this line iterates over the records in the csv file - for results in rdr.records() { - let record = results?; - // this line checks if the record has a 3 fields and if it's an integer - let age = record[2].parse::()?; - - // this line checks if the age is greater than 25 and then filters the record - if age > 25 { - println!("This is the record {:?}", record); - } - - } - - Ok(()) -} - -pub fn csv_file_writer() -> Result<(), Box> { - // path to the csv file - let file_path = "data/testing.csv"; - - // this line opens the csv file from the path passed into it - // let file = File::open(file_path)?; - - let file = File::create(file_path)?; - let mut writer = Writer::from_writer(file); - - writer.write_record(&["name", "email", "age"])?; - writer.write_record(&["martin", "martin@gmail.edu", "32"])?; - - - writer.flush()?; - - println!("CSV data has been written successfully."); - - let read_file_path = "data/testing.csv"; - - let file_read = File::open(read_file_path)?; - - let mut rder = ReaderBuilder::new().has_headers(true).from_reader(file_read); - - for res in rder.records() { - - println!("This is the {:?}", res); - } - - Ok(()) -} - - -pub fn csv_file_reader_using_serde() -> Result<(), Box> { - // path to the csv file - let file_path = "data/csv_data.csv"; - - // this line opens the csv file from the path passed into it - let file = File::open(file_path)?; - - // this creates a new instance of ReaderBuilder struct allows you to customize how the csv data is read - // .has_headers checks if the .csv file has headers and treats the first row as it - // .from_reader finalizes the ocnfiguration, creates a Reader instance which is used to read & parse the csv file. - let mut rdr = ReaderBuilder::new().has_headers(true).from_reader(file); - - // this line iterates over the records in the csv file - for results in rdr.deserialize() { - let record: Record = results?; - println!("This is the record {:#?}", record); - - } - - Ok(()) -} diff --git a/src/assignment_task/mod.rs b/src/assignment_task/mod.rs deleted file mode 100644 index 9863ba3..0000000 --- a/src/assignment_task/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod csv_data_processor; diff --git a/src/main.rs b/src/main.rs index 20a3869..bb47829 100644 --- a/src/main.rs +++ b/src/main.rs @@ -75,21 +75,6 @@ fn main() { // session_04::logging::log(); // session_04::serde::using_serde(); - if let Err(err) = assignment_task::csv_data_processor::csv_file_reader() { - println!("error running file {}", err); - std::process::exit(1); - } - - if let Err(err) = assignment_task::csv_data_processor::csv_file_writer() { - println!("error running file {}", err); - std::process::exit(1); - } - - if let Err(err) = assignment_task::csv_data_processor::csv_file_reader_using_serde() { - println!("error running file {}", err); - std::process::exit(1); - } - // session_04::debugging::debugging(); // //Book creation From e81489241ec57ba41bb33806cefbef7a9280212c Mon Sep 17 00:00:00 2001 From: martin machiebe Date: Fri, 9 May 2025 18:28:53 +0100 Subject: [PATCH 3/4] updates --- src/main.rs | 3 --- src/session_04/logging.rs | 9 +++++++-- src/session_04/serde.rs | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index bb47829..210ed58 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,9 +18,6 @@ // mod day_01; // use day_01::{enums, structs}; -use std::os::unix::process; - -mod assignment_task; mod session_04; extern crate tracing; diff --git a/src/session_04/logging.rs b/src/session_04/logging.rs index 8cce502..ab39669 100644 --- a/src/session_04/logging.rs +++ b/src/session_04/logging.rs @@ -21,12 +21,17 @@ pub fn log() { "hello world".to_string() } - warn!("Resource usage is high"); - trace!("this is trace"); + info!("App started"); + warn!("This is a warning"); + error!("An error occurred!"); + debug!("Debug info for developers"); + trace!("Detailed trace for deep debugging"); // tracing(); } +// RUST_LOG=debug cargo run + // fn tracing() { // tracing_subscriber::fmt::init(); diff --git a/src/session_04/serde.rs b/src/session_04/serde.rs index 71b1ad7..ef86902 100644 --- a/src/session_04/serde.rs +++ b/src/session_04/serde.rs @@ -11,7 +11,7 @@ struct User { pub fn using_serde() -> Result<(), serde_json::Error> { let user = User { - id: 1, + id: 3, name: "marto".to_string(), }; From 61d9295c4f8630098948db9e1b65188e2bbfad8c Mon Sep 17 00:00:00 2001 From: martin machiebe Date: Fri, 9 May 2025 18:48:47 +0100 Subject: [PATCH 4/4] updates --- src/main.rs | 2 ++ src/{session_04 => session_four}/debugging.rs | 0 src/{session_04 => session_four}/logging.rs | 0 src/{session_04 => session_four}/mod.rs | 0 src/{session_04 => session_four}/serde.rs | 0 5 files changed, 2 insertions(+) rename src/{session_04 => session_four}/debugging.rs (100%) rename src/{session_04 => session_four}/logging.rs (100%) rename src/{session_04 => session_four}/mod.rs (100%) rename src/{session_04 => session_four}/serde.rs (100%) diff --git a/src/main.rs b/src/main.rs index c8cb80f..50ea60f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,6 +73,8 @@ fn main() { // user_struct::user_registry(); // collections::collections(); + // session_four::serde::using_serde(); + // session_04::logging::log(); // session_04::serde::using_serde(); diff --git a/src/session_04/debugging.rs b/src/session_four/debugging.rs similarity index 100% rename from src/session_04/debugging.rs rename to src/session_four/debugging.rs diff --git a/src/session_04/logging.rs b/src/session_four/logging.rs similarity index 100% rename from src/session_04/logging.rs rename to src/session_four/logging.rs diff --git a/src/session_04/mod.rs b/src/session_four/mod.rs similarity index 100% rename from src/session_04/mod.rs rename to src/session_four/mod.rs diff --git a/src/session_04/serde.rs b/src/session_four/serde.rs similarity index 100% rename from src/session_04/serde.rs rename to src/session_four/serde.rs