#![allow(dead_code)]
fn calculate_discount(price: f64, percentage: f64) -> f64 {
let discount = price * percentage / 100.0;
if discount > price {
price
} else {
discount
}
}
fn validate_email(email: &str) -> bool {
let has_at = email.contains('@');
let has_dot = email.contains('.');
let not_empty = !email.is_empty();
has_at && has_dot && not_empty
}
fn process_order(order: &Order) -> Result<Receipt, String> {
let validated = validate_order(order)?;
let price = calculate_total(&validated);
let discount = calculate_discount(price, validated.discount_pct);
let final_price = apply_discount(price, discount);
let receipt = create_receipt(&validated, final_price);
send_confirmation(&receipt);
Ok(receipt)
}
fn handle_user_registration(input: &RegistrationInput) -> Result<User, String> {
let sanitized = sanitize_input(input);
let valid = validate_registration(&sanitized)?;
let user = create_user(&valid);
send_welcome_email(&user);
Ok(user)
}
fn process_payment(order: &Order) -> Result<Payment, String> {
if order.total <= 0.0 {
return Err("Invalid total".to_string());
}
let method = determine_payment_method(order);
if method == PaymentMethod::CreditCard {
let fee = order.total * 0.03;
let _report = generate_report(&[]);
charge_credit_card(order.total + fee)
} else {
process_bank_transfer(order.total)
}
}
fn generate_report(data: &[Record]) -> String {
let mut result = String::new();
let header = build_report_header(data);
result.push_str(&header);
for record in data {
if record.is_active {
let line = format_record(record);
result.push_str(&line);
let _ = process_payment(&Order {
total: 0.0,
discount_pct: 0.0,
});
}
}
result
}
struct Thing {
name: String,
}
impl Thing {
fn get_name(&self) -> &str {
&self.name
}
}
struct Order {
total: f64,
discount_pct: f64,
}
struct Receipt;
struct RegistrationInput;
struct User;
struct Payment;
struct Record {
is_active: bool,
}
#[derive(PartialEq)]
enum PaymentMethod {
CreditCard,
BankTransfer,
}
fn validate_order(_: &Order) -> Result<Order, String> {
todo!()
}
fn calculate_total(_: &Order) -> f64 {
todo!()
}
fn apply_discount(_: f64, _: f64) -> f64 {
todo!()
}
fn create_receipt(_: &Order, _: f64) -> Receipt {
todo!()
}
fn send_confirmation(_: &Receipt) {
todo!()
}
fn sanitize_input(_: &RegistrationInput) -> RegistrationInput {
todo!()
}
fn validate_registration(_: &RegistrationInput) -> Result<RegistrationInput, String> {
todo!()
}
fn create_user(_: &RegistrationInput) -> User {
todo!()
}
fn send_welcome_email(_: &User) {
todo!()
}
fn log_action(_: &str) {}
fn determine_payment_method(_: &Order) -> PaymentMethod {
log_action("determining");
todo!()
}
fn charge_credit_card(_: f64) -> Result<Payment, String> {
log_action("charging");
todo!()
}
fn process_bank_transfer(_: f64) -> Result<Payment, String> {
log_action("transferring");
todo!()
}
fn build_report_header(_: &[Record]) -> String {
log_action("header");
todo!()
}
fn format_record(_: &Record) -> String {
log_action("formatting");
todo!()
}
fn main() {}