From 258a20a808d3d7117bd03418def3188ed966d7f9 Mon Sep 17 00:00:00 2001 From: Bah Mamadu-lamarana <mamadulamarana.bah.etu@a10p29.fil.univ-lille.fr> Date: Wed, 5 Mar 2025 12:36:43 +0100 Subject: [PATCH] tp folder --- tp1/Cargo.toml | 6 +++ tp1/src/main.rs | 130 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 tp1/Cargo.toml create mode 100644 tp1/src/main.rs diff --git a/tp1/Cargo.toml b/tp1/Cargo.toml new file mode 100644 index 0000000..c86f6d0 --- /dev/null +++ b/tp1/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "tp1" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/tp1/src/main.rs b/tp1/src/main.rs new file mode 100644 index 0000000..fa87dcc --- /dev/null +++ b/tp1/src/main.rs @@ -0,0 +1,130 @@ +use std::{io, net::IpAddr}; + +pub enum PasswordError { + Empty, + Wrong, +} + +/// A type that can be used to check if a given input is a valid password +pub struct PasswordChecker { + secret: String, +} + +impl PasswordChecker { + fn new(pass: &str) -> Self { + Self {secret : pass.to_string() } + } + + pub fn check(&self, input: &str) -> Result<(), PasswordError> { + if input.trim() == self.secret { + Ok(()) + }else { + if input.trim().is_empty() { + Err(PasswordError::Empty) + }else { + Err(PasswordError::Wrong) + } + } + } +} + +fn main() { + println!("Hello, world!"); + + // let x: i8 = 42; + // println!("{x}"); + // eprintln!("{x:#08x}"); + + // let mut y = 1; + // while y < 11 { + // println!("{y}"); + // y += 1; + // } + + // check_password(); + + println!("Enter password: "); + let mut input = String::new(); + let _ = io::stdin().read_line(&mut input); + let checker = PasswordChecker::new("Rust"); + + // Call the check function on the checker. + // This function should have the same return type as + // the check_password function of the previous exercise + match checker.check(&input) { + Ok(()) => println!("ok"), + Err(PasswordError::Empty) => println!("Empty Password"), + Err(PasswordError::Wrong) => println!("Wrong password"), + } +} + +/// Reads a file that contains an address per line and returns a Vector with all well-formed +/// addresses and prints a warning on the standard error output for every malformed lines. +/// +/// Addresses should be ipv4 or ipv6 addresses. +fn get_addrs(path: &str) -> Result<Vec<IpAddr>, std::io::Error> { todo!{} } + + +/// Tries to connect to a given host with a TCP Connection with a one second timeout. +/// +/// Returns Ok(()) or a corresponding error if connection fails. +fn try_connect(addr: IpAddr, port: u16) -> Result<(), std::io::Error> { todo!() } + + + + + + + + + + + + + + + + + + + + +// fn check_password() { +// println!("Enter password: "); + +// let mut input = String::new(); +// let _ = io::stdin().read_line(&mut input); + +// match check_password3(&input) { +// Ok(()) => println!("ok"), +// Err(PasswordError::Empty) => println!("Empty Password"), +// Err(PasswordError::Wrong) => println!("Wrong password"), +// } +// } + +// Check an input for a predetermined secret password +// Returns `Ok(())` if the input matches the password or `Err(())` otherwise. +// pub fn check_password2(input: &str) -> Result<(), ()> { +// let predefined_password = "Rust"; +// if input.trim() == predefined_password { +// // println!("Good"); +// Ok(()) +// } else { +// // println!("Wrong"); +// Err(()) +// } +// } + +// pub fn check_password3(input: &str) -> Result<(), PasswordError> { +// let predefined_password = "Rust"; +// if input.trim() == predefined_password { +// Ok(()) +// } else { +// if input.trim().is_empty() { +// Err(PasswordError::Empty) +// }else { +// Err(PasswordError::Wrong) +// } +// } +// } + -- GitLab