routers_codec 0.1.3

Encoding and Decoding Primitives for Routers
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
use crate::osm::primitives::opening_hours::{OpeningHours, OpeningHoursParser};
use alloc::fmt;
use core::fmt::{Display, Formatter};
use core::str::FromStr;
use serde::Serialize;
use strum::{Display, EnumIter, EnumString};

/// Represents a complete conditional restriction condition
/// Examples: "Tu-Fr 00:00-24:00", "winter", "snow", "weight < 7.5"
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Condition {
    pub condition_type: ConditionType,
}

/// Main condition types as defined in OSM conditional restrictions
#[derive(Debug, Clone, PartialEq, Serialize)]
pub enum ConditionType {
    /// Time and date conditions using opening hours syntax
    /// Examples: "Mo-Fr 07:00-19:00", "Tu-Fr 00:00-24:00", "sunrise-sunset"
    TimeDate(TimeDateCondition),

    /// Seasonal conditions for time of year
    /// Examples: "winter", "summer"
    Season(SeasonCondition),

    /// Road surface conditions
    /// Examples: "wet", "snow", "dry"
    RoadCondition(RoadCondition),

    /// Vehicle property restrictions with comparison operators
    /// Examples: "weight<7.5", "length>5", "height<=3.5"
    VehicleProperty(VehiclePropertyCondition),

    /// Vehicle usage conditions
    /// Examples: "occupants>1", "hazmat"
    VehicleUsage(VehicleUsageCondition),

    /// User group restrictions
    /// Examples: "doctor", "disabled", "emergency", "female"
    UserGroup(UserGroupCondition),

    /// Purpose of access conditions
    /// Examples: "destination", "delivery", "customers"
    Purpose(PurposeCondition),

    /// Stay duration conditions
    /// Examples: "stay < 2 hours", "stay >= 30 minutes"
    StayDuration(StayDurationCondition),

    /// Combined conditions using AND operator
    /// Examples: "destination @ Sa-Su AND weight>7"
    Combined(CombinedCondition),

    /// Raw/unparsed condition for complex cases
    Raw(String),
}

/// Time and date conditions using opening hours syntax
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct TimeDateCondition {
    /// Raw opening hours string
    /// Examples: "Mo-Fr 07:00-19:00", "sunrise-sunset", "Jan-Mar"
    pub opening_hours: OpeningHours,
    /// Optional comment in local language
    /// Example: "bij grote verkeersdrukte"
    pub comment: Option<String>,
}

/// Seasonal time restrictions
#[derive(Debug, Clone, Copy, PartialEq, Display, EnumString, EnumIter, Serialize)]
#[strum(serialize_all = "lowercase")]
pub enum SeasonCondition {
    /// Winter season (dates vary by location/year)
    Winter,
    /// Summer season (dates vary by location/year)
    Summer,
    /// Spring season
    Spring,
    /// Autumn/Fall season
    Autumn,
}

/// Road surface and weather conditions
#[derive(Debug, Clone, PartialEq, Display, EnumString, EnumIter, Serialize)]
#[strum(serialize_all = "lowercase")]
pub enum RoadCondition {
    /// Wet road surface
    Wet,
    /// Dry road surface
    Dry,
    /// Snow on road
    Snow,
    /// Ice on road
    Ice,
    /// Rain weather condition
    Rain,
    /// Fog weather condition
    Fog,
}

/// Vehicle property conditions with comparison operators
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct VehiclePropertyCondition {
    pub property: VehicleProperty,
    pub operator: ComparisonOperator,
    pub value: f64,
    pub unit: Option<String>,
}

/// Vehicle properties that can be restricted
#[derive(Debug, Clone, PartialEq, Display, EnumString, EnumIter, Serialize)]
#[strum(serialize_all = "lowercase")]
pub enum VehicleProperty {
    /// Vehicle weight in tonnes
    Weight,
    /// Axle load in tonnes
    Axleload,
    /// Vehicle length in meters
    Length,
    /// Vehicle width in meters
    Width,
    /// Vehicle height in meters
    Height,
    /// Number of wheels
    Wheels,
    /// Ship draught in meters
    Draught,
}

/// Comparison operators for vehicle properties
#[derive(Debug, Clone, PartialEq, Display, Serialize)]
pub enum ComparisonOperator {
    #[strum(serialize = "<")]
    LessThan,
    #[strum(serialize = ">")]
    GreaterThan,
    #[strum(serialize = "=")]
    Equal,
    #[strum(serialize = "<=")]
    LessThanOrEqual,
    #[strum(serialize = ">=")]
    GreaterThanOrEqual,
}

/// Vehicle usage conditions
#[derive(Debug, Clone, PartialEq, Serialize)]
pub enum VehicleUsageCondition {
    /// Number of occupants with comparison
    /// Example: "occupants>1" for HOV lanes
    Occupants {
        operator: ComparisonOperator,
        count: u32,
    },
    /// Vehicle carrying hazardous materials
    Hazmat,
    /// Vehicle carrying specific load type
    Load(String),
}

/// User group conditions for access restrictions
#[derive(Debug, Clone, PartialEq, Display, EnumString, EnumIter, Serialize)]
#[strum(serialize_all = "lowercase")]
pub enum UserGroupCondition {
    /// Medical doctors
    Doctor,
    /// Disabled persons
    Disabled,
    /// Emergency services
    Emergency,
    /// Female users (for specific cultural contexts)
    Female,
    /// Residents of the area
    Residents,
    /// Permit holders
    Permit,
    /// Staff/employees
    Staff,
    /// Customers of businesses
    Customers,
}

/// Purpose of access conditions
#[derive(Debug, Clone, PartialEq, Display, EnumString, EnumIter, Serialize)]
#[strum(serialize_all = "lowercase")]
pub enum PurposeCondition {
    /// Destination traffic only
    Destination,
    /// Delivery vehicles
    Delivery,
    /// Customer access
    Customers,
    /// Forestry vehicles
    Forestry,
    /// Agricultural vehicles
    Agricultural,
    /// Private access
    Private,
    /// Permit required
    Permit,
}

/// Stay duration conditions
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct StayDurationCondition {
    pub operator: ComparisonOperator,
    pub duration: Duration,
}

/// Duration representation
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Duration {
    pub value: u32,
    pub unit: DurationUnit,
}

/// Duration units
#[derive(Debug, Clone, PartialEq, Display, EnumString, EnumIter, Serialize)]
#[strum(serialize_all = "lowercase")]
pub enum DurationUnit {
    Minutes,
    Hours,
    Days,
}

/// Combined conditions using logical operators
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct CombinedCondition {
    pub left: Box<ConditionType>,
    pub operator: LogicalOperator,
    pub right: Box<ConditionType>,
}

/// Logical operators for combining conditions
#[derive(Debug, Clone, PartialEq, Display, EnumString, Serialize)]
#[strum(serialize_all = "UPPERCASE")]
pub enum LogicalOperator {
    And,
    Or,
}

impl FromStr for ComparisonOperator {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "<" => Ok(ComparisonOperator::LessThan),
            ">" => Ok(ComparisonOperator::GreaterThan),
            "=" => Ok(ComparisonOperator::Equal),
            "<=" => Ok(ComparisonOperator::LessThanOrEqual),
            ">=" => Ok(ComparisonOperator::GreaterThanOrEqual),
            _ => Err(format!("Unknown comparison operator: {s}")),
        }
    }
}

impl Display for Condition {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match &self.condition_type {
            ConditionType::TimeDate(td) => {
                if let Some(comment) = &td.comment {
                    write!(f, "{} \"{}\"", td.opening_hours, comment)
                } else {
                    write!(f, "{}", td.opening_hours)
                }
            }
            ConditionType::Season(season) => write!(f, "{season}"),
            ConditionType::RoadCondition(road) => write!(f, "{road}"),
            ConditionType::VehicleProperty(vp) => {
                let unit_str = vp.unit.as_deref().unwrap_or("");
                write!(f, "{}{}{}{}", vp.property, vp.operator, vp.value, unit_str)
            }
            ConditionType::VehicleUsage(vu) => match vu {
                VehicleUsageCondition::Occupants { operator, count } => {
                    write!(f, "occupants{operator}{count}")
                }
                VehicleUsageCondition::Hazmat => write!(f, "hazmat"),
                VehicleUsageCondition::Load(load) => write!(f, "{load}"),
            },
            ConditionType::UserGroup(ug) => write!(f, "{ug}"),
            ConditionType::Purpose(purpose) => write!(f, "{purpose}"),
            ConditionType::StayDuration(sd) => {
                write!(
                    f,
                    "stay {} {} {}",
                    sd.operator, sd.duration.value, sd.duration.unit
                )
            }
            ConditionType::Combined(combined) => {
                write!(
                    f,
                    "{} {} {}",
                    Condition {
                        condition_type: *combined.left.clone()
                    },
                    combined.operator,
                    Condition {
                        condition_type: *combined.right.clone()
                    }
                )
            }
            ConditionType::Raw(raw) => write!(f, "{raw}"),
        }
    }
}

impl Condition {
    /// Parse a condition string into a Condition struct
    ///
    /// # Arguments
    /// * `condition_str` - The condition string to parse
    /// * `context` - Optional context from other OSM tags for disambiguation
    ///
    /// # Examples
    /// ```
    /// use routers_codec::osm::primitives::Condition;
    ///
    /// let condition = Condition::parse("Tu-Fr 00:00-24:00")?;
    /// let condition = Condition::parse("weight < 7.5")?;
    /// let condition = Condition::parse("winter")?;
    /// ```
    pub fn parse(condition_str: &str) -> Result<Self, ParseError> {
        let trimmed = condition_str.trim();

        // Handle parentheses
        let cleaned = if trimmed.starts_with('(') && trimmed.ends_with(')') {
            &trimmed[1..trimmed.len() - 1]
        } else {
            trimmed
        };

        // Check for combined conditions (AND/OR)
        if let Ok(combined) = Self::parse_combined_condition(cleaned) {
            return Ok(Condition {
                condition_type: combined,
            });
        }

        // Try to parse as specific condition types
        if let Ok(time_date) = Self::parse_time_date(cleaned) {
            return Ok(Condition {
                condition_type: ConditionType::TimeDate(time_date),
            });
        }

        if let Ok(season) = Self::parse_season(cleaned) {
            return Ok(Condition {
                condition_type: ConditionType::Season(season),
            });
        }

        if let Ok(road_condition) = Self::parse_road_condition(cleaned) {
            return Ok(Condition {
                condition_type: ConditionType::RoadCondition(road_condition),
            });
        }

        if let Ok(vehicle_prop) = Self::parse_vehicle_property(cleaned) {
            return Ok(Condition {
                condition_type: ConditionType::VehicleProperty(vehicle_prop),
            });
        }

        if let Ok(vehicle_usage) = Self::parse_vehicle_usage(cleaned) {
            return Ok(Condition {
                condition_type: ConditionType::VehicleUsage(vehicle_usage),
            });
        }

        if let Ok(user_group) = Self::parse_user_group(cleaned) {
            return Ok(Condition {
                condition_type: ConditionType::UserGroup(user_group),
            });
        }

        if let Ok(purpose) = Self::parse_purpose(cleaned) {
            return Ok(Condition {
                condition_type: ConditionType::Purpose(purpose),
            });
        }

        if let Ok(stay_duration) = Self::parse_stay_duration(cleaned) {
            return Ok(Condition {
                condition_type: ConditionType::StayDuration(stay_duration),
            });
        }

        // If no specific parser matches, store as raw condition
        Ok(Condition {
            condition_type: ConditionType::Raw(cleaned.to_string()),
        })
    }

    // Private parsing methods

    fn parse_combined_condition(s: &str) -> Result<ConditionType, ParseError> {
        // Look for AND/OR operators (case insensitive)
        let s_upper = s.to_uppercase();

        if let Some(and_pos) = s_upper.find(" AND ") {
            let left_str = &s[..and_pos].trim();
            let right_str = &s[and_pos + 5..].trim();

            let left_condition = Self::parse(left_str)?.condition_type;
            let right_condition = Self::parse(right_str)?.condition_type;

            return Ok(ConditionType::Combined(CombinedCondition {
                left: Box::new(left_condition),
                operator: LogicalOperator::And,
                right: Box::new(right_condition),
            }));
        }

        if let Some(or_pos) = s_upper.find(" OR ") {
            let left_str = &s[..or_pos].trim();
            let right_str = &s[or_pos + 4..].trim();

            let left_condition = Self::parse(left_str)?.condition_type;
            let right_condition = Self::parse(right_str)?.condition_type;

            return Ok(ConditionType::Combined(CombinedCondition {
                left: Box::new(left_condition),
                operator: LogicalOperator::Or,
                right: Box::new(right_condition),
            }));
        }

        Err(ParseError::NotCombinedCondition)
    }

    fn parse_time_date(s: &str) -> Result<TimeDateCondition, ParseError> {
        let (comment, hours) = if let Some(quote_start) = s.find('"') {
            let opening_hours = s[..quote_start].trim().to_string();
            let comment_end = s.rfind('"').unwrap_or(s.len());
            let comment = s[quote_start + 1..comment_end].to_string();
            (Some(comment), opening_hours)
        } else {
            (None, s.to_string())
        };

        Ok(TimeDateCondition {
            comment,
            opening_hours: OpeningHoursParser::parse(&hours)
                .map_err(|_| ParseError::NotTimeDate)?,
        })
    }

    fn parse_season(s: &str) -> Result<SeasonCondition, ParseError> {
        SeasonCondition::from_str(s).map_err(|_| ParseError::NotSeason)
    }

    fn parse_road_condition(s: &str) -> Result<RoadCondition, ParseError> {
        RoadCondition::from_str(s).map_err(|_| ParseError::NotRoadCondition)
    }

    fn parse_vehicle_property(s: &str) -> Result<VehiclePropertyCondition, ParseError> {
        // Look for comparison operators
        let operators = ["<=", ">=", "<", ">", "="];

        for op_str in &operators {
            if let Some(op_pos) = s.find(op_str) {
                let property_str = s[..op_pos].trim();
                let value_str = s[op_pos + op_str.len()..].trim();

                let property = VehicleProperty::from_str(property_str)
                    .map_err(|_| ParseError::UnknownVehicleProperty)?;
                let operator = ComparisonOperator::from_str(op_str)
                    .map_err(|_| ParseError::UnknownOperator)?;

                // Parse value and optional unit
                let (value, unit) = Self::parse_value_with_unit(value_str)?;

                return Ok(VehiclePropertyCondition {
                    property,
                    operator,
                    value,
                    unit,
                });
            }
        }

        Err(ParseError::NotVehicleProperty)
    }

    fn parse_vehicle_usage(s: &str) -> Result<VehicleUsageCondition, ParseError> {
        if s == "hazmat" {
            return Ok(VehicleUsageCondition::Hazmat);
        }

        // Check for occupants condition
        if let Some(rest) = s.strip_prefix("occupants") {
            let operators = ["<=", ">=", "<", ">", "="];

            for op_str in &operators {
                if let Some(op_pos) = rest.find(op_str) {
                    let operator = ComparisonOperator::from_str(op_str)
                        .map_err(|_| ParseError::UnknownOperator)?;
                    let count_str = rest[op_pos + op_str.len()..].trim();
                    let count = count_str
                        .parse::<u32>()
                        .map_err(|_| ParseError::InvalidNumber)?;

                    return Ok(VehicleUsageCondition::Occupants { operator, count });
                }
            }
        }

        // Treat as generic load condition
        Ok(VehicleUsageCondition::Load(s.to_string()))
    }

    fn parse_user_group(s: &str) -> Result<UserGroupCondition, ParseError> {
        UserGroupCondition::from_str(s).map_err(|_| ParseError::NotUserGroup)
    }

    fn parse_purpose(s: &str) -> Result<PurposeCondition, ParseError> {
        PurposeCondition::from_str(s).map_err(|_| ParseError::NotPurpose)
    }

    fn parse_stay_duration(s: &str) -> Result<StayDurationCondition, ParseError> {
        if !s.starts_with("stay") {
            return Err(ParseError::NotStayDuration);
        }

        let rest = s[4..].trim(); // Skip "stay"
        let operators = ["<=", ">=", "<", ">", "="];

        for op_str in &operators {
            if let Some(op_pos) = rest.find(op_str) {
                let operator = ComparisonOperator::from_str(op_str)
                    .map_err(|_| ParseError::UnknownOperator)?;
                let duration_str = rest[op_pos + op_str.len()..].trim();
                let duration = Self::parse_duration(duration_str)?;

                return Ok(StayDurationCondition { operator, duration });
            }
        }

        Err(ParseError::NotStayDuration)
    }

    fn parse_duration(s: &str) -> Result<Duration, ParseError> {
        let parts: Vec<&str> = s.split_whitespace().collect();
        if parts.len() != 2 {
            return Err(ParseError::InvalidDuration);
        }

        let value = parts[0]
            .parse::<u32>()
            .map_err(|_| ParseError::InvalidNumber)?;
        let unit_str = parts[1].to_lowercase();

        let unit = match unit_str.as_str() {
            "minute" | "minutes" => DurationUnit::Minutes,
            "hour" | "hours" => DurationUnit::Hours,
            "day" | "days" => DurationUnit::Days,
            _ => return Err(ParseError::InvalidDurationUnit),
        };

        Ok(Duration { value, unit })
    }

    fn parse_value_with_unit(s: &str) -> Result<(f64, Option<String>), ParseError> {
        // Try to parse as pure number first
        if let Ok(value) = s.parse::<f64>() {
            return Ok((value, None));
        }

        // Look for number followed by unit
        let mut number_end = 0;
        for (i, c) in s.chars().enumerate() {
            if c.is_numeric() || c == '.' {
                number_end = i + 1;
            } else {
                break;
            }
        }

        if number_end > 0 {
            let number_str = &s[..number_end];
            let unit_str = &s[number_end..].trim();

            let value = number_str
                .parse::<f64>()
                .map_err(|_| ParseError::InvalidNumber)?;
            let unit = if unit_str.is_empty() {
                None
            } else {
                Some(unit_str.to_string())
            };

            Ok((value, unit))
        } else {
            Err(ParseError::InvalidNumber)
        }
    }
}

/// Errors that can occur during parsing
#[derive(Debug, PartialEq)]
pub enum ParseError {
    NotTimeDate,
    NotSeason,
    NotRoadCondition,
    NotVehicleProperty,
    NotVehicleUsage,
    NotUserGroup,
    NotPurpose,
    NotStayDuration,
    NotCombinedCondition,
    UnknownVehicleProperty,
    UnknownOperator,
    InvalidNumber,
    InvalidDuration,
    InvalidDurationUnit,
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ParseError::NotTimeDate => write!(f, "Not a time/date condition"),
            ParseError::NotSeason => write!(f, "Not a season condition"),
            ParseError::NotRoadCondition => write!(f, "Not a road condition"),
            ParseError::NotVehicleProperty => write!(f, "Not a vehicle property condition"),
            ParseError::NotVehicleUsage => write!(f, "Not a vehicle usage condition"),
            ParseError::NotUserGroup => write!(f, "Not a user group condition"),
            ParseError::NotPurpose => write!(f, "Not a purpose condition"),
            ParseError::NotStayDuration => write!(f, "Not a stay duration condition"),
            ParseError::NotCombinedCondition => write!(f, "Not a combined condition"),
            ParseError::UnknownVehicleProperty => write!(f, "Unknown vehicle property"),
            ParseError::UnknownOperator => write!(f, "Unknown comparison operator"),
            ParseError::InvalidNumber => write!(f, "Invalid number format"),
            ParseError::InvalidDuration => write!(f, "Invalid duration format"),
            ParseError::InvalidDurationUnit => write!(f, "Invalid duration unit"),
        }
    }
}

impl core::error::Error for ParseError {}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::osm::primitives::opening_hours::{
        OpeningRule, Time, TimeRange, Weekday, WeekdayRange,
    };

    #[test]
    fn test_parse_time_date() {
        let condition = Condition::parse("Tu-Fr 00:00-24:00").unwrap();
        if let ConditionType::TimeDate(td) = condition.condition_type {
            // "Tu-Fr 00:00-24:00"
            assert_eq!(
                td.opening_hours.rules[0],
                OpeningRule {
                    weekdays: Some(WeekdayRange::Range(Weekday::Tuesday, Weekday::Friday)),
                    times: vec![TimeRange {
                        start: Time { hour: 0, minute: 0 },
                        end: Time {
                            hour: 24,
                            minute: 0
                        }
                    }],
                    closed: false
                }
            );
            assert_eq!(td.comment, None);
        } else {
            panic!("Expected TimeDate condition");
        }
    }

    #[test]
    fn test_parse_season() {
        let condition = Condition::parse("winter").unwrap();
        if let ConditionType::Season(season) = condition.condition_type {
            assert_eq!(season, SeasonCondition::Winter);
        } else {
            panic!("Expected Season condition");
        }
    }

    #[test]
    fn test_parse_vehicle_property() {
        let condition = Condition::parse("weight < 7.5").unwrap();
        if let ConditionType::VehicleProperty(vp) = condition.condition_type {
            assert_eq!(vp.property, VehicleProperty::Weight);
            assert_eq!(vp.operator, ComparisonOperator::LessThan);
            assert_eq!(vp.value, 7.5);
        } else {
            panic!("Expected VehicleProperty condition");
        }
    }

    #[test]
    fn test_parse_road_condition() {
        let condition = Condition::parse("snow").unwrap();
        if let ConditionType::RoadCondition(road) = condition.condition_type {
            assert_eq!(road, RoadCondition::Snow);
        } else {
            panic!("Expected RoadCondition");
        }
    }

    #[test]
    fn test_to_string_roundtrip() {
        let original = "weight < 7.5";
        let condition = Condition::parse(original).unwrap();
        let regenerated = condition.to_string();
        assert_eq!(regenerated, "weight<7.5"); // Note: spaces might be normalized
    }
}