// Copyright ยฉ 2024 StaticDataGen. All rights reserved. // SPDX-License-Identifier: Apache-2.0 OR MIT //! # StaticDataGen Web App Manifest Examples //! //! This program demonstrates the generation of web app manifests //! using the StaticDataGen library, showing various configurations //! and use cases for Progressive Web Apps (PWAs). use staticdatagen::models::data::{IconData, ManifestData}; use staticdatagen::modules::manifest::create_manifest_data; use std::collections::HashMap; /// Entry point for the StaticDataGen Manifest Examples program. /// /// Demonstrates various web app manifest generation scenarios including /// basic PWA setup, custom themes, and different display modes. /// /// # Errors /// /// Returns a `Result` containing a `Box` if any error /// occurs during the execution of the examples. pub fn main() -> Result<(), Box> { println!("\n๐Ÿงช StaticDataGen Web App Manifest Examples\n"); basic_manifest_example()?; custom_theme_example()?; full_pwa_example()?; icon_configuration_example()?; display_modes_example()?; orientation_example()?; metadata_based_example()?; validation_example()?; println!("\n๐ŸŽ‰ All manifest examples completed successfully!"); Ok(()) } /// Demonstrates basic manifest generation. fn basic_manifest_example() -> Result<(), Box> { println!("๐Ÿฆ€ Basic Manifest Example"); println!("---------------------------------------------"); let mut manifest = ManifestData::new(); manifest.name = "My Web App".to_string(); manifest.short_name = "MyApp".to_string(); manifest.start_url = "/".to_string(); manifest.display = "standalone".to_string(); match manifest.validate() { Ok(_) => { println!(" โœ… Basic manifest validated:"); println!(" ๐Ÿ“ฑ Name: {}", manifest.name); println!(" ๐Ÿ“ฑ Short Name: {}", manifest.short_name); println!(" ๐Ÿ”— Start URL: {}", manifest.start_url); println!(" ๐Ÿ–ฅ๏ธ Display: {}", manifest.display); } Err(e) => println!(" โŒ Validation error: {:?}", e), } Ok(()) } /// Demonstrates custom theme configuration. fn custom_theme_example() -> Result<(), Box> { println!("\n๐Ÿฆ€ Custom Theme Example"); println!("---------------------------------------------"); let mut manifest = ManifestData::new(); manifest.name = "Themed App".to_string(); manifest.background_color = "#f0f0f0".to_string(); manifest.theme_color = "#2196f3".to_string(); match manifest.validate() { Ok(_) => { println!(" โœ… Theme configuration:"); println!( " ๐ŸŽจ Background: {}", manifest.background_color ); println!(" ๐ŸŽจ Theme: {}", manifest.theme_color); } Err(e) => println!(" โŒ Validation error: {:?}", e), } Ok(()) } /// Demonstrates a complete PWA configuration. fn full_pwa_example() -> Result<(), Box> { println!("\n๐Ÿฆ€ Full PWA Example"); println!("---------------------------------------------"); let mut manifest = ManifestData::new(); manifest.name = "Complete PWA".to_string(); manifest.short_name = "PWA".to_string(); manifest.start_url = "/".to_string(); manifest.display = "standalone".to_string(); manifest.background_color = "#ffffff".to_string(); manifest.theme_color = "#000000".to_string(); manifest.description = "A complete Progressive Web App example".to_string(); manifest.orientation = "portrait".to_string(); manifest.scope = "/".to_string(); let icon = IconData::new( "/icons/icon-512x512.png".to_string(), "512x512".to_string(), ); manifest.icons.push(icon); match manifest.validate() { Ok(_) => { println!(" โœ… Full PWA manifest validated"); println!(" ๐Ÿ“ฑ App Info:"); println!(" Name: {}", manifest.name); println!(" Description: {}", manifest.description); println!(" ๐ŸŽจ Theme:"); println!( " Background: {}", manifest.background_color ); println!(" Theme: {}", manifest.theme_color); println!(" ๐Ÿ“ฑ Display:"); println!(" Mode: {}", manifest.display); println!(" Orientation: {}", manifest.orientation); } Err(e) => println!(" โŒ Validation error: {:?}", e), } Ok(()) } /// Demonstrates icon configuration. fn icon_configuration_example() -> Result<(), Box> { println!("\n๐Ÿฆ€ Icon Configuration Example"); println!("---------------------------------------------"); let mut manifest = ManifestData::new(); manifest.name = "Icon Demo".to_string(); let icon_configs = vec![ ("192x192", "/icons/icon-192.png", Some("maskable")), ("512x512", "/icons/icon-512.png", Some("any")), ("256x256", "/icons/icon-256.png", None), ]; for (size, src, purpose) in icon_configs { let mut icon = IconData::new(src.to_string(), size.to_string()); if let Some(p) = purpose { icon.purpose = Some(p.to_string()); } manifest.icons.push(icon); } match manifest.validate() { Ok(_) => { println!(" โœ… Icon configuration:"); for (i, icon) in manifest.icons.iter().enumerate() { println!(" ๐Ÿ–ผ๏ธ Icon {}:", i + 1); println!(" Size: {}", icon.sizes); println!(" Source: {}", icon.src); if let Some(purpose) = &icon.purpose { println!(" Purpose: {}", purpose); } } } Err(e) => println!(" โŒ Validation error: {:?}", e), } Ok(()) } /// Demonstrates different display modes. fn display_modes_example() -> Result<(), Box> { println!("\n๐Ÿฆ€ Display Modes Example"); println!("---------------------------------------------"); let display_modes = vec![ ("fullscreen", "Full screen mode"), ("standalone", "Standalone app mode"), ("minimal-ui", "Minimal UI mode"), ("browser", "Browser mode"), ]; for (mode, description) in display_modes { let mut manifest = ManifestData::new(); manifest.name = format!("{} Demo", mode); manifest.display = mode.to_string(); match manifest.validate() { Ok(_) => { println!(" โœ… {}: {} - Valid", mode, description) } Err(e) => println!( " โŒ {}: {} - Error: {:?}", mode, description, e ), } } Ok(()) } /// Demonstrates orientation configurations. fn orientation_example() -> Result<(), Box> { println!("\n๐Ÿฆ€ Orientation Example"); println!("---------------------------------------------"); let orientations = vec![ "portrait", "landscape", "portrait-primary", "landscape-primary", "portrait-secondary", "landscape-secondary", ]; for orientation in orientations { let mut manifest = ManifestData::new(); manifest.name = format!("{} Orientation Demo", orientation); manifest.orientation = orientation.to_string(); match manifest.validate() { Ok(_) => println!(" โœ… {}: Valid", orientation), Err(e) => { println!(" โŒ {}: Error: {:?}", orientation, e) } } } Ok(()) } /// Demonstrates manifest generation from metadata. fn metadata_based_example() -> Result<(), Box> { println!("\n๐Ÿฆ€ Metadata-based Example"); println!("---------------------------------------------"); let mut metadata = HashMap::new(); let _ = metadata.insert("name".to_string(), "Metadata App".to_string()); let _ = metadata .insert("short_name".to_string(), "MetaApp".to_string()); let _ = metadata.insert( "description".to_string(), "App from metadata".to_string(), ); let _ = metadata.insert("theme-color".to_string(), "blue".to_string()); let _ = metadata .insert("background-color".to_string(), "white".to_string()); let manifest = create_manifest_data(&metadata); println!(" โœ… Generated from metadata:"); println!(" ๐Ÿ“ฑ Name: {}", manifest.name); println!(" ๐Ÿ“ฑ Short Name: {}", manifest.short_name); println!(" ๐Ÿ“ Description: {}", manifest.description); println!(" ๐ŸŽจ Theme Color: {}", manifest.theme_color); println!(" ๐ŸŽจ Background: {}", manifest.background_color); Ok(()) } /// Demonstrates manifest validation. fn validation_example() -> Result<(), Box> { println!("\n๐Ÿฆ€ Validation Example"); println!("---------------------------------------------"); let test_cases = vec![ ( "Valid Basic", ManifestData { name: "Valid App".to_string(), short_name: "App".to_string(), start_url: "/".to_string(), display: "standalone".to_string(), ..Default::default() }, true, ), ( "Invalid Display", ManifestData { name: "Invalid Display".to_string(), display: "invalid".to_string(), ..Default::default() }, false, ), ( "Long Short Name", ManifestData { name: "Valid Name".to_string(), short_name: "ThisShortNameIsTooLong".to_string(), ..Default::default() }, false, ), ]; for (case, manifest, should_be_valid) in test_cases { match manifest.validate() { Ok(_) => { if should_be_valid { println!(" โœ… {}: Valid as expected", case); } else { println!(" โŒ {}: Unexpectedly valid", case); } } Err(e) => { if !should_be_valid { println!(" โœ… {}: Invalid as expected", case); } else { println!( " โŒ {}: Unexpected error: {:?}", case, e ); } } } } Ok(()) }