paft 0.7.1

Facade crate for paft: unified public API over the paft ecosystem.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# Best Practices for paft Extensible Enums

This document provides specific best practices for working with paft's extensible enum pattern, focusing on practical implementation strategies for library authors and consumers.

## Table of Contents

1. [Consumer Best Practices]#consumer-best-practices
2. [Library Author Best Practices]#library-author-best-practices
3. [Provider Adapter Patterns]#provider-adapter-patterns
4. [Migration Strategies]#migration-strategies
5. [Performance Considerations]#performance-considerations
6. [Testing Strategies]#testing-strategies

## Consumer Best Practices

### 1. Always Handle Other Variants

**❌ Compilation Error:**
```rust
use paft_money::{Currency, IsoCurrency};

fn process_currency(currency: Currency) -> &'static str {
    match currency {
        Currency::Iso(IsoCurrency::USD) => "US Dollar",
        Currency::Iso(IsoCurrency::EUR) => "Euro",
        // Missing Other variant - compiler error!
    }
}
```

**✅ Correct Approach:**
```rust
use paft_money::{Currency, IsoCurrency};

fn process_currency(currency: Currency) -> String {
    match currency {
        Currency::Iso(IsoCurrency::USD) => "US Dollar".to_string(),
        Currency::Iso(IsoCurrency::EUR) => "Euro".to_string(),
        Currency::Other(code) => format!("Unknown currency: {}", code),
    }
}
```

### 2. Use Helper Methods When Available

Many paft enums provide helper methods to check for canonical variants:

```rust
fn analyze_asset(asset: AssetKind) {
    match asset {
        AssetKind::Equity => println!("Stock analysis"),
        AssetKind::Crypto => println!("Crypto analysis"),
        _ => println!("General analysis"),
    }
}
```

### 3. Create Mapping Functions for Common Cases

```rust
/// Maps provider-specific currency codes to canonical variants.
/// Never produce Other for values we model canonically.
fn normalize_currency(provider_code: &str) -> Currency {
    match provider_code.to_uppercase().as_ref() {
        // Map to canonical variants when possible
        "DOLLAR" | "US_DOLLAR" | "USD" => Currency::Iso(IsoCurrency::USD),
        "EURO" | "EUR" => Currency::Iso(IsoCurrency::EUR),
        "POUND" | "GBP" => Currency::Iso(IsoCurrency::GBP),

        // Map common cryptos to canonical variants when available
        "BITCOIN" | "XBT" | "BTC" => Currency::BTC,
        "ETHEREUM" | "ETH" => Currency::ETH,

        // Preserve other values using the library parser
        _ => Currency::try_from_str(provider_code)
            .unwrap_or_else(|_| Currency::Other(paft_utils::Canonical::try_new(provider_code).unwrap())),
    }
}
```

### 4. Use Default Values Appropriately

```rust
fn get_currency_info(currency: Currency) -> CurrencyInfo {
    match currency {
        Currency::Iso(IsoCurrency::USD) => CurrencyInfo::usd(),
        Currency::Iso(IsoCurrency::EUR) => CurrencyInfo::euro(),
        Currency::Other(code) => {
            // Log unknown currency for monitoring
            log::warn!("Unknown currency encountered: {}", code);
            
            // Return a generic currency info
            CurrencyInfo::generic(code)
        }
    }
}
```

## Library Author Best Practices

### 1. Encourage Ecosystem Convergence

When building libraries on top of paft, map provider-specific strings to canonical variants whenever possible:

```rust
// In your provider adapter
impl From<GenericProviderCurrency> for Currency {
    fn from(gp_currency: GenericProviderCurrency) -> Self {
        match gp_currency.code.as_ref() {
            // Map to canonical variants
            "USD" => Currency::Iso(IsoCurrency::USD),
            "EUR" => Currency::Iso(IsoCurrency::EUR),
            "GBP" => Currency::Iso(IsoCurrency::GBP),
            
            // Normalize crypto currencies
            "BTC" | "BITCOIN" => Currency::BTC,
            "ETH" | "ETHEREUM" => Currency::ETH,
            
            // Preserve other values
            other => Currency::try_from_str(other)
                .unwrap_or_else(|_| Currency::Other(other.trim().to_uppercase())),
        }
    }
}
```

### 2. Provide Conversion Utilities

```rust
pub mod currency_utils {
    use super::Currency;
    use paft_money::IsoCurrency;

    /// Attempts to normalize a currency code to a canonical variant
    pub fn normalize_currency_code(code: &str) -> Currency {
        let trimmed = code.trim();
        if trimmed.is_empty() {
            return Currency::Other(paft_utils::Canonical::try_new("UNKNOWN").unwrap());
        }
        match trimmed.to_uppercase().as_ref() {
            "DOLLAR" | "US_DOLLAR" | "USD" => Currency::Iso(IsoCurrency::USD),
            "EURO" | "EUR" => Currency::Iso(IsoCurrency::EUR),
            "POUND" | "GBP" => Currency::Iso(IsoCurrency::GBP),
            "BITCOIN" | "XBT" | "BTC" => Currency::BTC,
            "ETHEREUM" | "ETH" => Currency::ETH,
            other => Currency::Other(paft_utils::Canonical::try_new(other).unwrap()),
        }
    }

    /// Returns true if the currency is commonly used
    pub fn is_common_currency(currency: &Currency) -> bool {
        matches!(
            currency,
            Currency::Iso(IsoCurrency::USD)
                | Currency::Iso(IsoCurrency::EUR)
                | Currency::Iso(IsoCurrency::GBP)
                | Currency::Iso(IsoCurrency::JPY)
                | Currency::BTC
                | Currency::ETH
        )
    }
}
```

### 3. Document Your Mappings

```rust
/// Currency mapping for Alpha Vantage provider
/// 
/// Known mappings:
/// - "US_DOLLAR" -> Currency::Iso(IsoCurrency::USD) (canonical)
/// - "EURO" -> Currency::Iso(IsoCurrency::EUR) (canonical)
/// - "BITCOIN" -> BTC (canonical)
/// 
/// Unmapped values are preserved as Other(Canonical) in uppercase
impl From<AlphaVantageCurrency> for Currency {
    fn from(av_currency: AlphaVantageCurrency) -> Self {
        match av_currency.code.to_uppercase().as_ref() {
            "US_DOLLAR" => Currency::Iso(IsoCurrency::USD),
            "EURO" => Currency::Iso(IsoCurrency::EUR),
            "BITCOIN" => Currency::BTC,
            _ => Currency::Other(paft_utils::Canonical::try_new(&av_currency.code).unwrap()),
        }
    }
}
```

### 4. Add Validation and Logging

```rust
pub struct CurrencyProcessor {
    unknown_currencies: std::collections::HashSet<String>,
}

impl CurrencyProcessor {
    pub fn process_currency(&mut self, currency: Currency) -> Result<CurrencyInfo, Error> {
        match currency {
            Currency::Iso(IsoCurrency::USD) => Ok(CurrencyInfo::usd()),
            Currency::Iso(IsoCurrency::EUR) => Ok(CurrencyInfo::euro()),
            Currency::Other(code) => {
                // Track unknown currencies for analysis
                self.unknown_currencies.insert(code.as_ref().to_string());
                
                // Log for monitoring
                log::warn!("Unknown currency encountered: {}", code);
                
                // Return error or generic info based on your needs
                Err(Error::UnsupportedCurrency(code.as_ref().to_string()))
            }
        }
    }
    
    /// Get statistics about unknown currencies encountered
    pub fn unknown_currency_stats(&self) -> &std::collections::HashSet<String> {
        &self.unknown_currencies
    }
}
```

## Provider Adapter Patterns

### 1. Comprehensive Mapping Strategy

```rust
pub struct ProviderAdapter {
    currency_mappings: std::collections::HashMap<String, Currency>,
    exchange_mappings: std::collections::HashMap<String, Exchange>,
}

impl ProviderAdapter {
    pub fn new() -> Self {
        let mut currency_mappings = std::collections::HashMap::new();
        let mut exchange_mappings = std::collections::HashMap::new();
        
        // Populate mappings
        currency_mappings.insert("DOLLAR".to_string(), Currency::Iso(IsoCurrency::USD));
        currency_mappings.insert("EURO".to_string(), Currency::Iso(IsoCurrency::EUR));
        currency_mappings.insert("BITCOIN".to_string(), Currency::BTC);
        
        exchange_mappings.insert("NASDAQ-GS".to_string(), Exchange::NASDAQ);
        exchange_mappings.insert("NYSE-ARCA".to_string(), Exchange::NYSE);
        
        Self {
            currency_mappings,
            exchange_mappings,
        }
    }
    
    pub fn normalize_currency(&self, provider_code: &str) -> Currency {
        self.currency_mappings
            .get(&provider_code.to_uppercase())
            .cloned()
            .unwrap_or_else(|| Currency::Other(paft_utils::Canonical::try_new(provider_code).unwrap()))
    }
    
    pub fn normalize_exchange(&self, provider_code: &str) -> Exchange {
        self.exchange_mappings
            .get(&provider_code.to_uppercase())
            .cloned()
            .unwrap_or_else(|| Exchange::Other(paft_utils::Canonical::try_new(provider_code).unwrap()))
    }
}
```

### 2. Lazy Loading Pattern

```rust
pub struct LazyProviderAdapter {
    mappings: std::sync::OnceLock<std::collections::HashMap<String, Currency>>,
}

impl LazyProviderAdapter {
    fn get_mappings(&self) -> &std::collections::HashMap<String, Currency> {
        self.mappings.get_or_init(|| {
            let mut mappings = std::collections::HashMap::new();
            mappings.insert("DOLLAR".to_string(), Currency::Iso(IsoCurrency::USD));
            mappings.insert("EURO".to_string(), Currency::Iso(IsoCurrency::EUR));
            mappings.insert("BITCOIN".to_string(), Currency::BTC);
            mappings
        })
    }
    
    pub fn normalize_currency(&self, provider_code: &str) -> Currency {
        self.get_mappings()
            .get(&provider_code.to_uppercase())
            .cloned()
            .unwrap_or_else(|| Currency::Other(paft_utils::Canonical::try_new(provider_code).unwrap()))
    }
}
```

## Migration Strategies

### 1. Gradual Migration from String-based Code

**Before (string-based):**
```rust
fn process_currency(currency_code: &str) -> String {
    match currency_code {
        "USD" => "US Dollar".to_string(),
        "EUR" => "Euro".to_string(),
        _ => format!("Unknown: {}", currency_code),
    }
}
```

**After (paft-based):**
```rust
fn process_currency(currency: Currency) -> String {
    match currency {
        Currency::Iso(IsoCurrency::USD) => "US Dollar".to_string(),
        Currency::Iso(IsoCurrency::EUR) => "Euro".to_string(),
        Currency::Other(code) => format!("Unknown: {}", code),
    }
}

// Migration helper
fn migrate_currency_code(code: &str) -> Currency {
    match code {
        "USD" => Currency::Iso(IsoCurrency::USD),
        "EUR" => Currency::Iso(IsoCurrency::EUR),
        _ => Currency::Other(paft_utils::Canonical::try_new(code).unwrap()),
    }
}
```

### 2. Backward Compatibility Layer

```rust
pub struct CurrencyCompat {
    currency: Currency,
}

impl CurrencyCompat {
    pub fn from_string(code: &str) -> Self {
        Self {
            currency: Currency::try_from_str(code)
                .unwrap_or_else(|_| Currency::Other(paft_utils::Canonical::try_new(code).unwrap())),
        }
    }
    
    pub fn to_string(&self) -> String {
        self.currency.to_string()
    }
    
    pub fn is_known(&self) -> bool {
        !matches!(self.currency, Currency::Other(_))
    }
}
```

## Performance Considerations

### 1. Avoid Repeated String Allocations

**❌ Inefficient:**
```rust
fn process_currencies(currencies: Vec<Currency>) {
    for currency in currencies {
        match currency {
            Currency::Other(code) => {
                // Creates new string allocation
                let upper_code = code.to_uppercase();
                println!("Processing: {}", upper_code);
            }
            _ => {}
        }
    }
}
```

**✅ Efficient:**
```rust
fn process_currencies(currencies: Vec<Currency>) {
    for currency in currencies {
        match currency {
            Currency::Other(code) => {
                // Use the string directly
                println!("Processing: {}", code);
            }
            _ => {}
        }
    }
}
```

### 2. Use String Interning for Common Other Values

```rust
use std::sync::OnceLock;

pub struct CurrencyCache {
    btc: OnceLock<Currency>,
    eth: OnceLock<Currency>,
}

impl CurrencyCache {
    pub fn btc() -> Currency {
        static CACHE: OnceLock<Currency> = OnceLock::new();
        *CACHE.get_or_init(|| Currency::Other("BTC".to_string()))
    }
    
    pub fn eth() -> Currency {
        static CACHE: OnceLock<Currency> = OnceLock::new();
        *CACHE.get_or_init(|| Currency::Other("ETH".to_string()))
    }
}
```

## Testing Strategies

### 1. Test Both Canonical and Other Variants

```rust
#[cfg(test)]
mod tests {
    use super::*;
    use paft_money::IsoCurrency;
    
    #[test]
    fn test_currency_processing() {
        // Test canonical variants
        assert_eq!(process_currency(Currency::Iso(IsoCurrency::USD)), "US Dollar");
        assert_eq!(process_currency(Currency::Iso(IsoCurrency::EUR)), "Euro");
        
        // Test Other variants
        assert_eq!(
            process_currency(Currency::Other("BTC".to_string())),
            "Unknown currency: BTC"
        );
        assert_eq!(
            process_currency(Currency::Other("UNKNOWN".to_string())),
            "Unknown currency: UNKNOWN"
        );
    }
    
    #[test]
    fn test_currency_normalization() {
        assert_eq!(normalize_currency("DOLLAR"), Currency::Iso(IsoCurrency::USD));
        assert_eq!(normalize_currency("BITCOIN"), Currency::BTC);
        assert_eq!(normalize_currency("UNKNOWN"), Currency::Other("UNKNOWN".to_string()));
    }
}
```

### 2. Property-based Testing

```rust
use proptest::prelude::*;

proptest! {
    #[test]
    fn test_currency_roundtrip(currency in any::<Currency>()) {
        let string_repr = currency.to_string();
        let parsed = Currency::try_from_str(&string_repr).unwrap();
        assert_eq!(currency, parsed);
    }
    
    #[test]
    fn test_currency_normalization_preserves_other(
        code in "[A-Z]{1,10}"
    ) {
        let currency = Currency::Other(code.clone());
        assert_eq!(currency.to_string(), code);
    }
}
```

### 3. Integration Testing with Real Provider Data

```rust
#[test]
fn test_generic_provider_currency_mapping() {
    let test_cases = vec![
        ("USD", Currency::Iso(IsoCurrency::USD)),
        ("EUR", Currency::Iso(IsoCurrency::EUR)),
        ("BTC", Currency::BTC),
        ("UNKNOWN", Currency::Other("UNKNOWN".to_string())),
    ];
    
    for (provider_code, expected) in test_cases {
        let result = normalize_generic_provider_currency(provider_code);
        assert_eq!(result, expected, "Failed for provider code: {}", provider_code);
    }
}
```

## Conclusion

The extensible enum pattern in paft provides a robust foundation for handling the fragmented nature of financial data providers. By following these best practices:

1. **Always handle Other variants** in your match statements
2. **Map provider-specific strings** to canonical variants when possible
3. **Use helper methods** like `is_canonical()` for cleaner code
4. **Document your mappings** for future maintainers
5. **Test both canonical and Other variants** thoroughly
6. **Consider performance implications** of string operations

You can build reliable, maintainable financial applications that work seamlessly across different data providers while encouraging ecosystem convergence over time.