// Advanced Standard Library Example
// Demonstrates time, strings, math, and log modules
use std::time
use std::strings
use std::math
use std::log
// === TIME EXAMPLES ===
fn time_examples() {
log.info("=== Time Examples ===")
// Current time
let now = time.now()
log.info(format!("Current time: {}", time.to_rfc3339(&now)))
// Format time
let formatted = time.format(&now, "%Y-%m-%d %H:%M:%S")
log.info(format!("Formatted: {}", formatted))
// Time arithmetic
let future = time.add(&now, time.hours(24))
log.info(format!("24 hours from now: {}", time.to_rfc3339(&future)))
// Unix timestamp
let timestamp = time.timestamp(&now)
log.info(format!("Unix timestamp: {}", timestamp))
// Duration
let duration = time.minutes(30)
log.debug(format!("Duration created: {:?}", duration))
}
// === STRING EXAMPLES ===
fn string_examples() {
log.info("\n=== string Examples ===")
// Splitting
let text = "hello,world,rust"
let parts = strings.split(text, ",")
log.info(format!("Split: {:?}", parts))
// Joining
let joined = strings.join(&parts, " | ")
log.info(format!("Joined: {}", joined))
// Case conversion
let upper = strings.to_uppercase("hello world")
let lower = strings.to_lowercase("HELLO WORLD")
let cap = strings.capitalize("hello")
log.info(format!("Upper: {}, Lower: {}, Cap: {}", upper, lower, cap))
// Trimming
let trimmed = strings.trim(" spaces ")
log.info(format!("Trimmed: '{}'", trimmed))
// Searching
if strings.contains("hello world", "world") {
log.info("string contains 'world'")
}
// Replacement
let replaced = strings.replace("foo bar foo", "foo", "baz")
log.info(format!("Replaced: {}", replaced))
// Substring
let sub = strings.substring("hello world", 0, 5)
log.info(format!("Substring: {}", sub))
}
// === MATH EXAMPLES ===
fn math_examples() {
log.info("\n=== Math Examples ===")
// Constants
log.info(format!("PI = {}", math.PI))
log.info(format!("E = {}", math.E))
// Basic operations
let sqrt_result = math.sqrt(16.0)
let pow_result = math.pow(2.0, 8.0)
log.info(format!("sqrt(16) = {}", sqrt_result))
log.info(format!("2^8 = {}", pow_result))
// Rounding
let x = 3.7
log.info(format!("round({}) = {}", x, math.round(x)))
log.info(format!("floor({}) = {}", x, math.floor(x)))
log.info(format!("ceil({}) = {}", x, math.ceil(x)))
// Trigonometry
let angle = math.to_radians(45.0)
let sine = math.sin(angle)
log.info(format!("sin(45°) = {}", sine))
// Min/Max
let min_val = math.min_f64(10.0, 20.0)
let max_val = math.max_f64(10.0, 20.0)
log.info(format!("min(10, 20) = {}, max(10, 20) = {}", min_val, max_val))
// Random
let random_num = math.random()
let random_range = math.random_range(1, 100)
log.info(format!("Random: {}, Random range: {}", random_num, random_range))
}
// === LOGGING EXAMPLES ===
fn logging_examples() {
log.info("\n=== Logging Examples ===")
// Different log levels
log.error("This is an error message")
log.warn("This is a warning")
log.info("This is an info message")
log.debug("This is a debug message")
log.trace("This is a trace message")
// Structured logging with context
let mut ctx = log.context()
ctx.add("user_id", "12345")
.add("action", "login")
.info("User logged in")
}
// === DATA PROCESSING EXAMPLE ===
fn process_data() {
log.info("\n=== Data Processing Example ===")
// Sample CSV data
let csv_data = "John,30,Engineer\nAlice,25,Designer\nBob,35,Manager"
// Process each line
let lines = strings.lines(csv_data)
for line in lines {
let fields = strings.split(line, ",")
if fields.len() == 3 {
let name = &fields[0]
let age = &fields[1]
let role = &fields[2]
log.info(format!("Name: {}, Age: {}, Role: {}", name, age, role))
// Do some processing
let age_in_months = age.parse::<i64>().unwrap() * 12
log.debug(format!("{} is {} months old", name, age_in_months))
}
}
}
// === CALCULATION EXAMPLE ===
fn calculate_statistics(numbers: &[f64]) {
log.info("\n=== Statistics Calculation ===")
if numbers.is_empty() {
log.warn("No numbers to calculate")
return
}
// Calculate sum
let sum: f64 = numbers.iter().sum()
// Calculate mean
let mean = sum / numbers.len() as f64
// Find min and max
let mut min = numbers[0]
let mut max = numbers[0]
for &num in numbers {
min = math.min_f64(min, num)
max = math.max_f64(max, num)
}
// Calculate standard deviation
let variance: f64 = numbers
.iter()
.map(|&x| math.pow(x - mean, 2.0))
.sum::<f64>() / numbers.len() as f64
let std_dev = math.sqrt(variance)
// Log results
log.info(format!("Count: {}", numbers.len()))
log.info(format!("Sum: {:.2}", sum))
log.info(format!("Mean: {:.2}", mean))
log.info(format!("Min: {:.2}, Max: {:.2}", min, max))
log.info(format!("Std Dev: {:.2}", std_dev))
}
// === MAIN ===
fn main() {
// Initialize logging
log.init()
log.info("š Windjammer Standard Library - Advanced Examples")
log.info("=" * 50)
// Run all examples
time_examples()
string_examples()
math_examples()
logging_examples()
process_data()
// Statistics example
let numbers = vec![10.0, 20.0, 30.0, 40.0, 50.0]
calculate_statistics(&numbers)
log.info("\nā
All examples completed successfully!")
}