mod models;
mod services;
mod config;
mod utils;
use crate::models::user::{User, UserRole};
use crate::services::auth::AuthService;
use crate::services::database::DatabaseConnection;
use crate::config::settings::Settings;
use crate::utils::helper::{format_output, validate_input};
use crate::utils::helper::DataProcessor as Processor;
pub use models::user::UserRole as PublicUserRole;
pub use services::auth::AuthError;
use std::collections::HashMap;
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("=== Multi-Module Rust Application ===\n");
println!("1. Cross-module type instantiation:");
let user = User::new("Alice".to_string(), "alice@example.com".to_string(), UserRole::Admin);
println!(" Created user: {}", user);
println!("\n2. Service initialization:");
let settings = Settings::new();
println!(" Settings loaded: {:?}", settings);
let db = DatabaseConnection::new(settings.db_url());
db.connect()?;
println!(" Database connected ✓");
let auth_service = AuthService::new(db);
println!(" Auth service initialized ✓");
println!("\n3. Cross-module function calls:");
if validate_input(&user.email) {
println!(" Email validation passed ✓");
auth_service.register_user(&user)?;
let output = format_output(&format!("User {} registered", user.name));
println!(" {}", output);
}
println!("\n4. Generic cross-module usage:");
let mut processor = Processor::new(HashMap::from([
("transform".to_string(), "uppercase".to_string()),
]));
let processed = processor.process("hello world");
println!(" Processed data: {}", processed);
println!("\n5. Re-exported type usage:");
let role: PublicUserRole = PublicUserRole::User;
println!(" Using re-exported UserRole: {:?}", role);
println!("\n6. Cross-module error handling:");
match auth_service.authenticate("alice@example.com", "wrong_password") {
Ok(token) => println!(" Authentication successful: {}", token),
Err(e) => println!(" Authentication failed: {} ✓", e),
}
println!("\n=== All cross-module tests completed ===");
Ok(())
}
#[cfg(test)]
mod integration_tests {
use super::*;
use crate::models::user::User;
use crate::services::auth::{AuthService, AuthError};
#[test]
fn test_cross_module_integration() {
let user = User::new(
"Test User".to_string(),
"test@example.com".to_string(),
UserRole::User,
);
let settings = Settings::new();
let db = DatabaseConnection::new(settings.db_url());
let auth = AuthService::new(db);
assert!(auth.register_user(&user).is_ok());
}
#[test]
fn test_re_exported_types() {
let _role: PublicUserRole = PublicUserRole::Admin;
let error = AuthError::UserNotFound;
assert_eq!(error.to_string(), "User not found");
}
}