wf-market 0.2.1

A Rust client library for the warframe.market API
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//! Request models for creating and updating orders.
//!
//! This module provides builder-pattern types for constructing
//! API requests with compile-time safety.

use serde::Serialize;

use super::common::OrderType;

/// Request to create a new order.
///
/// Use the builder methods to construct orders with proper validation.
///
/// # Example
///
/// ```
/// use wf_market::{CreateOrder, OrderType};
///
/// // Simple sell order
/// let order = CreateOrder::sell("nikana_prime_set", 100, 1);
///
/// // Mod order with rank
/// let mod_order = CreateOrder::sell("serration", 50, 1)
///     .with_mod_rank(10);
///
/// // Hidden order
/// let hidden = CreateOrder::buy("mesa_prime_set", 200, 1)
///     .hidden();
///
/// // Sculpture with stars
/// let sculpture = CreateOrder::sell("ayatan_anasa_sculpture", 25, 1)
///     .with_sculpture_stars(2, 4);
/// ```
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateOrder {
    /// ID of the item to trade
    pub item_id: String,

    /// Order type (buy or sell)
    #[serde(rename = "type")]
    pub order_type: OrderType,

    /// Price in platinum
    pub platinum: u32,

    /// Quantity to trade
    pub quantity: u32,

    /// Whether the order is visible
    pub visible: bool,

    /// Minimum items per trade
    #[serde(skip_serializing_if = "Option::is_none")]
    pub per_trade: Option<u32>,

    /// Mod rank (for rankable mods)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rank: Option<u8>,

    /// Charges remaining (for consumable mods)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub charges: Option<u8>,

    /// Item subtype (e.g., blueprint, crafted)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subtype: Option<String>,

    /// Amber stars installed (for sculptures)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub amber_stars: Option<u8>,

    /// Cyan stars installed (for sculptures)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cyan_stars: Option<u8>,
}

impl CreateOrder {
    /// Create a new sell order.
    ///
    /// # Arguments
    ///
    /// * `item_id` - The item's ID or slug
    /// * `platinum` - Price in platinum (must be > 0)
    /// * `quantity` - Number of items to sell (must be > 0)
    ///
    /// # Panics
    ///
    /// Panics in debug builds if `platinum` or `quantity` is 0.
    pub fn sell(item_id: impl Into<String>, platinum: u32, quantity: u32) -> Self {
        debug_assert!(platinum > 0, "platinum must be greater than 0");
        debug_assert!(quantity > 0, "quantity must be greater than 0");
        Self {
            item_id: item_id.into(),
            order_type: OrderType::Sell,
            platinum,
            quantity,
            visible: true,
            per_trade: None,
            rank: None,
            charges: None,
            subtype: None,
            amber_stars: None,
            cyan_stars: None,
        }
    }

    /// Create a new buy order.
    ///
    /// # Arguments
    ///
    /// * `item_id` - The item's ID or slug
    /// * `platinum` - Price in platinum (must be > 0)
    /// * `quantity` - Number of items to buy (must be > 0)
    ///
    /// # Panics
    ///
    /// Panics in debug builds if `platinum` or `quantity` is 0.
    pub fn buy(item_id: impl Into<String>, platinum: u32, quantity: u32) -> Self {
        debug_assert!(platinum > 0, "platinum must be greater than 0");
        debug_assert!(quantity > 0, "quantity must be greater than 0");
        Self {
            item_id: item_id.into(),
            order_type: OrderType::Buy,
            platinum,
            quantity,
            visible: true,
            per_trade: None,
            rank: None,
            charges: None,
            subtype: None,
            amber_stars: None,
            cyan_stars: None,
        }
    }

    /// Set the mod rank (for rankable mods).
    pub fn with_mod_rank(mut self, rank: u8) -> Self {
        self.rank = Some(rank);
        self
    }

    /// Set the charges (for consumable mods like Requiem).
    pub fn with_charges(mut self, charges: u8) -> Self {
        self.charges = Some(charges);
        self
    }

    /// Set the item subtype (e.g., "blueprint", "crafted").
    pub fn with_subtype(mut self, subtype: impl Into<String>) -> Self {
        self.subtype = Some(subtype.into());
        self
    }

    /// Set the installed stars (for Ayatan sculptures).
    pub fn with_sculpture_stars(mut self, amber: u8, cyan: u8) -> Self {
        self.amber_stars = Some(amber);
        self.cyan_stars = Some(cyan);
        self
    }

    /// Set the minimum items per trade.
    pub fn with_per_trade(mut self, per_trade: u32) -> Self {
        self.per_trade = Some(per_trade);
        self
    }

    /// Make the order hidden (not visible to others).
    pub fn hidden(mut self) -> Self {
        self.visible = false;
        self
    }

    /// Make the order visible (default).
    pub fn visible(mut self) -> Self {
        self.visible = true;
        self
    }
}

/// Request to update an existing order.
///
/// Only include the fields you want to change.
///
/// # Example
///
/// ```
/// use wf_market::UpdateOrder;
///
/// // Update just the price
/// let update = UpdateOrder::new().platinum(90);
///
/// // Update price and quantity
/// let update = UpdateOrder::new()
///     .platinum(85)
///     .quantity(10);
///
/// // Hide the order
/// let update = UpdateOrder::new().visible(false);
///
/// // Update sculpture stars
/// let update = UpdateOrder::new()
///     .amber_stars(2)
///     .cyan_stars(4);
/// ```
#[derive(Debug, Clone, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateOrder {
    /// New price in platinum
    #[serde(skip_serializing_if = "Option::is_none")]
    pub platinum: Option<u32>,

    /// New quantity
    #[serde(skip_serializing_if = "Option::is_none")]
    pub quantity: Option<u32>,

    /// New visibility
    #[serde(skip_serializing_if = "Option::is_none")]
    pub visible: Option<bool>,

    /// New minimum items per trade
    #[serde(skip_serializing_if = "Option::is_none")]
    pub per_trade: Option<u32>,

    /// New mod rank
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rank: Option<u8>,

    /// New charges count (for consumable mods)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub charges: Option<u8>,

    /// New amber stars count (for sculptures)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub amber_stars: Option<u8>,

    /// New cyan stars count (for sculptures)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cyan_stars: Option<u8>,

    /// New subtype
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subtype: Option<String>,
}

impl UpdateOrder {
    /// Create a new empty update request.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the new price.
    ///
    /// # Panics
    ///
    /// Panics in debug builds if `platinum` is 0.
    pub fn platinum(mut self, platinum: u32) -> Self {
        debug_assert!(platinum > 0, "platinum must be greater than 0");
        self.platinum = Some(platinum);
        self
    }

    /// Set the new quantity.
    ///
    /// # Panics
    ///
    /// Panics in debug builds if `quantity` is 0.
    pub fn quantity(mut self, quantity: u32) -> Self {
        debug_assert!(quantity > 0, "quantity must be greater than 0");
        self.quantity = Some(quantity);
        self
    }

    /// Set the new visibility.
    pub fn visible(mut self, visible: bool) -> Self {
        self.visible = Some(visible);
        self
    }

    /// Set the new minimum items per trade.
    pub fn per_trade(mut self, per_trade: u32) -> Self {
        self.per_trade = Some(per_trade);
        self
    }

    /// Set the new mod rank.
    pub fn rank(mut self, rank: u8) -> Self {
        self.rank = Some(rank);
        self
    }

    /// Set the new charges count (for consumable mods).
    pub fn charges(mut self, charges: u8) -> Self {
        self.charges = Some(charges);
        self
    }

    /// Set the new amber stars count (for Ayatan sculptures).
    pub fn amber_stars(mut self, stars: u8) -> Self {
        self.amber_stars = Some(stars);
        self
    }

    /// Set the new cyan stars count (for Ayatan sculptures).
    pub fn cyan_stars(mut self, stars: u8) -> Self {
        self.cyan_stars = Some(stars);
        self
    }

    /// Set the new subtype.
    pub fn subtype(mut self, subtype: impl Into<String>) -> Self {
        self.subtype = Some(subtype.into());
        self
    }

    /// Check if any fields are set.
    pub fn is_empty(&self) -> bool {
        self.platinum.is_none()
            && self.quantity.is_none()
            && self.visible.is_none()
            && self.per_trade.is_none()
            && self.rank.is_none()
            && self.charges.is_none()
            && self.amber_stars.is_none()
            && self.cyan_stars.is_none()
            && self.subtype.is_none()
    }
}

/// Filter options for fetching top orders.
///
/// Use the builder methods to construct filters for the `get_top_orders` endpoint.
///
/// # Example
///
/// ```
/// use wf_market::TopOrderFilters;
///
/// // Filter for max rank mods
/// let filters = TopOrderFilters::new().rank(10);
///
/// // Filter for mods with rank less than 5
/// let filters = TopOrderFilters::new().rank_lt(5);
///
/// // Filter for sculptures with specific star counts
/// let filters = TopOrderFilters::new()
///     .amber_stars(2)
///     .cyan_stars(4);
///
/// // Filter by subtype
/// let filters = TopOrderFilters::new().subtype("blueprint");
/// ```
#[derive(Debug, Clone, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TopOrderFilters {
    /// Filter by exact mod rank
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rank: Option<u8>,

    /// Filter by mod rank less than this value
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rank_lt: Option<u8>,

    /// Filter by exact charges (for consumable mods)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub charges: Option<u8>,

    /// Filter by charges less than this value
    #[serde(skip_serializing_if = "Option::is_none")]
    pub charges_lt: Option<u8>,

    /// Filter by exact amber stars (for sculptures)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub amber_stars: Option<u8>,

    /// Filter by amber stars less than this value
    #[serde(skip_serializing_if = "Option::is_none")]
    pub amber_stars_lt: Option<u8>,

    /// Filter by exact cyan stars (for sculptures)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cyan_stars: Option<u8>,

    /// Filter by cyan stars less than this value
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cyan_stars_lt: Option<u8>,

    /// Filter by item subtype (e.g., "blueprint", "crafted")
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subtype: Option<String>,
}

impl TopOrderFilters {
    /// Create new empty filters.
    pub fn new() -> Self {
        Self::default()
    }

    /// Filter by exact mod rank.
    pub fn rank(mut self, rank: u8) -> Self {
        self.rank = Some(rank);
        self
    }

    /// Filter by mod rank less than this value.
    ///
    /// # Panics
    ///
    /// Panics in debug builds if `rank` is 0.
    pub fn rank_lt(mut self, rank: u8) -> Self {
        debug_assert!(rank > 0, "rank_lt must be greater than 0");
        self.rank_lt = Some(rank);
        self
    }

    /// Filter by exact charges (for consumable mods like Requiem).
    pub fn charges(mut self, charges: u8) -> Self {
        self.charges = Some(charges);
        self
    }

    /// Filter by charges less than this value.
    ///
    /// # Panics
    ///
    /// Panics in debug builds if `charges` is 0.
    pub fn charges_lt(mut self, charges: u8) -> Self {
        debug_assert!(charges > 0, "charges_lt must be greater than 0");
        self.charges_lt = Some(charges);
        self
    }

    /// Filter by exact amber stars (for Ayatan sculptures).
    pub fn amber_stars(mut self, stars: u8) -> Self {
        self.amber_stars = Some(stars);
        self
    }

    /// Filter by amber stars less than this value.
    ///
    /// # Panics
    ///
    /// Panics in debug builds if `stars` is 0.
    pub fn amber_stars_lt(mut self, stars: u8) -> Self {
        debug_assert!(stars > 0, "amber_stars_lt must be greater than 0");
        self.amber_stars_lt = Some(stars);
        self
    }

    /// Filter by exact cyan stars (for Ayatan sculptures).
    pub fn cyan_stars(mut self, stars: u8) -> Self {
        self.cyan_stars = Some(stars);
        self
    }

    /// Filter by cyan stars less than this value.
    ///
    /// # Panics
    ///
    /// Panics in debug builds if `stars` is 0.
    pub fn cyan_stars_lt(mut self, stars: u8) -> Self {
        debug_assert!(stars > 0, "cyan_stars_lt must be greater than 0");
        self.cyan_stars_lt = Some(stars);
        self
    }

    /// Filter by item subtype (e.g., "blueprint", "crafted").
    pub fn subtype(mut self, subtype: impl Into<String>) -> Self {
        self.subtype = Some(subtype.into());
        self
    }

    /// Check if any filters are set.
    pub fn is_empty(&self) -> bool {
        self.rank.is_none()
            && self.rank_lt.is_none()
            && self.charges.is_none()
            && self.charges_lt.is_none()
            && self.amber_stars.is_none()
            && self.amber_stars_lt.is_none()
            && self.cyan_stars.is_none()
            && self.cyan_stars_lt.is_none()
            && self.subtype.is_none()
    }

    /// Build query string for the filters.
    pub(crate) fn to_query_string(&self) -> String {
        let mut params = Vec::new();

        if let Some(v) = self.rank {
            params.push(format!("rank={}", v));
        }
        if let Some(v) = self.rank_lt {
            params.push(format!("rankLt={}", v));
        }
        if let Some(v) = self.charges {
            params.push(format!("charges={}", v));
        }
        if let Some(v) = self.charges_lt {
            params.push(format!("chargesLt={}", v));
        }
        if let Some(v) = self.amber_stars {
            params.push(format!("amberStars={}", v));
        }
        if let Some(v) = self.amber_stars_lt {
            params.push(format!("amberStarsLt={}", v));
        }
        if let Some(v) = self.cyan_stars {
            params.push(format!("cyanStars={}", v));
        }
        if let Some(v) = self.cyan_stars_lt {
            params.push(format!("cyanStarsLt={}", v));
        }
        if let Some(ref v) = self.subtype {
            // URL-encode the subtype to handle special characters safely
            params.push(format!("subtype={}", urlencoding::encode(v)));
        }

        if params.is_empty() {
            String::new()
        } else {
            format!("?{}", params.join("&"))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_create_sell_order() {
        let order = CreateOrder::sell("test-item", 100, 5);

        assert_eq!(order.item_id, "test-item");
        assert!(matches!(order.order_type, OrderType::Sell));
        assert_eq!(order.platinum, 100);
        assert_eq!(order.quantity, 5);
        assert!(order.visible);
    }

    #[test]
    fn test_create_buy_order() {
        let order = CreateOrder::buy("test-item", 50, 10);

        assert!(matches!(order.order_type, OrderType::Buy));
        assert_eq!(order.platinum, 50);
    }

    #[test]
    fn test_order_builder_chain() {
        let order = CreateOrder::sell("mod-item", 100, 1)
            .with_mod_rank(10)
            .hidden();

        assert_eq!(order.rank, Some(10));
        assert!(!order.visible);
    }

    #[test]
    fn test_update_order() {
        let update = UpdateOrder::new().platinum(90).quantity(5);

        assert_eq!(update.platinum, Some(90));
        assert_eq!(update.quantity, Some(5));
        assert!(!update.is_empty());
    }

    #[test]
    fn test_update_order_empty() {
        let update = UpdateOrder::new();
        assert!(update.is_empty());
    }

    #[test]
    fn test_serialization() {
        let order = CreateOrder::sell("item", 100, 1).with_mod_rank(5);

        let json = serde_json::to_string(&order).unwrap();
        assert!(json.contains("\"rank\":5"));
        assert!(!json.contains("charges")); // None fields should be skipped
    }
}