#![allow(dead_code, unused_variables)]
use rust_pattern_macros::simple_factory;
use rust_patterns::{FactoryError, FactoryFallback};
#[simple_factory]
pub trait Product {
fn name(&self) -> &str;
fn price(&self) -> f64;
}
#[derive(Default)]
struct ProductA;
impl Product for ProductA {
fn name(&self) -> &str {
"Product A"
}
fn price(&self) -> f64 {
19.99
}
}
#[derive(Default)]
struct ProductB;
impl Product for ProductB {
fn name(&self) -> &str {
"Product B"
}
fn price(&self) -> f64 {
29.99
}
}
#[simple_factory(Send + Sync)]
pub trait Service {
fn execute(&self) -> Result<String, String>;
}
#[derive(Default)]
struct ServiceImpl;
impl Service for ServiceImpl {
fn execute(&self) -> Result<String, String> {
Ok("Service executed successfully".to_string())
}
}
fn main() {
println!("=== Simple Factory Macro Example ===\n");
println!("1. Testing Product factory:");
match ProductFactory::create("product_a", FactoryFallback::NoFallback) {
Ok(product) => {
println!(
" Created product: {}, Price: ${:.2}",
product.name(),
product.price()
);
}
Err(FactoryError::FactoryNotFound(id)) => {
println!(
" Product factory not found for ID: '{}' (expected - factories not registered)",
id
);
}
Err(e) => {
println!(" Error: {}", e);
}
}
println!("\n2. Testing fallback strategies:");
match ProductFactory::create("", FactoryFallback::First) {
Ok(product) => {
println!(" First fallback created: {}", product.name());
}
Err(FactoryError::NoFactoriesAvailable) => {
println!(" No factories available (expected - factories not registered)");
}
Err(e) => {
println!(" Error: {}", e);
}
}
println!("\n3. Testing Service factory with Send + Sync bounds:");
match ServiceFactory::create("service_impl", FactoryFallback::NoFallback) {
Ok(service) => match service.execute() {
Ok(result) => println!(" Service executed: {}", result),
Err(e) => println!(" Service error: {}", e),
},
Err(FactoryError::FactoryNotFound(id)) => {
println!(" Service factory not found for ID: '{}' (expected)", id);
}
Err(e) => {
println!(" Error: {}", e);
}
}
println!("\n4. Generated factory types:");
println!(
" - ProductFactory: {}",
std::any::type_name::<ProductFactory>()
);
println!(
" - ServiceFactory: {}",
std::any::type_name::<ServiceFactory>()
);
println!("\n=== Example completed ===");
}
#[allow(dead_code)]
fn demonstrate_generated_code() {
}