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
use std::collections::HashMap;
use {SegmentHistory, Image, Time, TimingMethod, TimeSpan};
use run::PERSONAL_BEST_COMPARISON_NAME;

#[derive(Clone, Default, Debug)]
pub struct Segment {
    name: String,
    icon: Image,
    best_segment_time: Time,
    split_time: Time,
    segment_history: SegmentHistory,
    comparisons: HashMap<String, Time>,
}

impl Segment {
    pub fn new<S>(name: S) -> Self
        where S: Into<String>
    {
        Segment { name: name.into(), ..Default::default() }
    }

    #[inline]
    pub fn name(&self) -> &str {
        &self.name
    }

    #[inline]
    pub fn set_name<S>(&mut self, name: S)
        where S: AsRef<str>
    {
        self.name.clear();
        self.name.push_str(name.as_ref());
    }

    #[inline]
    pub fn icon(&self) -> &Image {
        &self.icon
    }

    #[inline]
    pub fn set_icon<D: Into<Image>>(&mut self, image: D) {
        self.icon = image.into();
    }

    #[inline]
    pub fn comparisons_mut(&mut self) -> &mut HashMap<String, Time> {
        &mut self.comparisons
    }

    #[inline]
    pub fn comparison_mut(&mut self, comparison: &str) -> &mut Time {
        self.comparisons.entry(comparison.into()).or_insert_with(Time::default)
    }

    #[inline]
    pub fn comparison(&self, comparison: &str) -> Time {
        self.comparisons.get(comparison).cloned().unwrap_or_default()
    }

    #[inline]
    pub fn comparison_timing_method(&self,
                                    comparison: &str,
                                    method: TimingMethod)
                                    -> Option<TimeSpan> {
        self.comparisons.get(comparison).and_then(|t| t[method])
    }

    #[inline]
    pub fn personal_best_split_time(&self) -> Time {
        self.comparisons
            .get(PERSONAL_BEST_COMPARISON_NAME)
            .cloned()
            .unwrap_or_else(Time::default)
    }

    #[inline]
    pub fn personal_best_split_time_mut(&mut self) -> &mut Time {
        self.comparisons
            .entry(PERSONAL_BEST_COMPARISON_NAME.to_string())
            .or_insert_with(Time::default)
    }

    #[inline]
    pub fn set_personal_best_split_time(&mut self, time: Time) {
        self.comparisons.insert(PERSONAL_BEST_COMPARISON_NAME.into(), time);
    }

    #[inline]
    pub fn best_segment_time(&self) -> Time {
        self.best_segment_time
    }

    #[inline]
    pub fn best_segment_time_mut(&mut self) -> &mut Time {
        &mut self.best_segment_time
    }

    #[inline]
    pub fn set_best_segment_time(&mut self, time: Time) {
        self.best_segment_time = time;
    }

    #[inline]
    pub fn split_time(&self) -> Time {
        self.split_time
    }

    #[inline]
    pub fn set_split_time(&mut self, time: Time) {
        self.split_time = time;
    }

    #[inline]
    pub fn clear_split_time(&mut self) {
        self.set_split_time(Default::default());
    }

    #[inline]
    pub fn segment_history(&self) -> &SegmentHistory {
        &self.segment_history
    }

    #[inline]
    pub fn segment_history_mut(&mut self) -> &mut SegmentHistory {
        &mut self.segment_history
    }
}