Skip to content
Snippets Groups Projects
Commit 258a20a8 authored by Bah Mamadu-lamarana's avatar Bah Mamadu-lamarana
Browse files

tp folder

parent 66e159ef
Branches
No related tags found
No related merge requests found
[package]
name = "tp1"
version = "0.1.0"
edition = "2024"
[dependencies]
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)
// }
// }
// }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment