deepmerge 0.1.0

Deep merge functionality with policy-driven merging and derive macro support
Documentation
/// Example demonstrating sequence deduplication features
///
/// This example shows:
/// 1. How to use sequence_dedupe policy flag
/// 2. Using explicit dedupe helper functions
/// 3. Current limitations and workarounds
///
/// Run with: cargo run --example sequence_dedupe --features std

use deepmerge::prelude::*;
#[cfg(feature = "std")]
use deepmerge::{vec_append_with_dedupe, vec_prepend_with_dedupe};

fn main() {
    println!("=== Sequence Deduplication Examples ===\n");
    
    basic_dedupe_example();
    #[cfg(feature = "std")]
    explicit_dedupe_functions();
    custom_key_extraction_example();
}

fn basic_dedupe_example() {
    println!("## Basic Deduplication\n");
    
    // Note: The sequence_dedupe policy flag is parsed but not automatically applied
    // because Vec<T> can't deduplicate without knowing T implements Eq + Hash
    let policy = ComposedPolicy::new(DefaultPolicy)
        .with_sequence_dedupe(true);
    
    let mut tags = vec!["rust", "web", "rust", "api"];
    let new_tags = vec!["api", "server", "web"];
    
    println!("Before merge:");
    println!("  tags: {:?}", tags);
    println!("  new_tags: {:?}", new_tags);
    
    tags.merge_with_policy(new_tags, &policy);
    
    println!("After merge (no automatic dedup without bounds):");
    println!("  tags: {:?}", tags);
    println!("  Note: Duplicates remain because Vec<&str> can't auto-dedupe\n");
}

#[cfg(feature = "std")]
fn explicit_dedupe_functions() {
    println!("## Explicit Deduplication Functions\n");
    
    // For types that implement Eq + Hash, use explicit functions
    let mut tags = vec!["rust", "web", "rust", "api"];
    let new_tags = vec!["api", "server", "web"];
    
    println!("Using vec_append_with_dedupe:");
    println!("  Before: {:?}", tags);
    vec_append_with_dedupe(&mut tags, new_tags.clone());
    println!("  After: {:?}", tags);
    
    // Prepend with deduplication
    let mut tags = vec!["rust", "web", "rust"];
    println!("\nUsing vec_prepend_with_dedupe:");
    println!("  Before: {:?}", tags);
    vec_prepend_with_dedupe(&mut tags, new_tags);
    println!("  After: {:?}", tags);
    println!();
}

fn custom_key_extraction_example() {
    println!("## Custom Key Extraction (Manual Approach)\n");
    
    #[derive(Debug, Clone, PartialEq)]
    struct User {
        id: u32,
        name: String,
        score: i32,
    }
    
    // Since unique_by isn't automatically applied, we need manual deduplication
    // This shows how you would implement custom key-based deduplication
    
    let mut users = vec![
        User { id: 1, name: "Alice".to_string(), score: 100 },
        User { id: 2, name: "Bob".to_string(), score: 85 },
        User { id: 1, name: "Alice Updated".to_string(), score: 110 }, // Duplicate ID
    ];
    
    let new_users = vec![
        User { id: 3, name: "Charlie".to_string(), score: 95 },
        User { id: 2, name: "Bob Updated".to_string(), score: 90 }, // Duplicate ID
    ];
    
    println!("Original users: {:#?}", users);
    println!("New users: {:#?}", new_users);
    
    // Manual deduplication by ID
    #[cfg(feature = "std")]
    {
        use std::collections::HashSet;
        
        let mut seen_ids = HashSet::new();
        let mut result = Vec::new();
        
        // Keep first occurrence of each ID
        for user in users.into_iter().chain(new_users.into_iter()) {
            if seen_ids.insert(user.id) {
                result.push(user);
            }
        }
        
        users = result;
        println!("\nAfter manual dedup by ID: {:#?}", users);
    }
    
    #[cfg(not(feature = "std"))]
    {
        users.extend(new_users);
        println!("\nAfter merge (no dedup without std): {:#?}", users);
    }
    
    println!("\n=== Notes ===");
    println!("- sequence_dedupe policy flag is parsed but requires explicit bounds");
    println!("- unique_by is parsed in derive macro but not yet wired to runtime");
    println!("- Use explicit helper functions for types with Eq + Hash");
    println!("- For custom key extraction, implement manual deduplication");
}