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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use number::Number;
use chrono::{DateTime, FixedOffset};
use chrono_tz::Tz;
use context::Context;
use substance::Substance;
use std::ops::{Add, Div, Mul, Neg, Sub};
use date;
use date::GenericDateTime;

#[derive(Clone, Debug)]
pub enum Value {
    Number(Number),
    DateTime(date::GenericDateTime),
    Substance(Substance),
}

pub trait Show {
    /// Provides a string representation of something, using information contained in a Context.
    fn show(&self, context: &Context) -> String;
}

impl Show for DateTime<FixedOffset> {
    fn show(&self, context: &Context) -> String {
        if let Some(h) = context.humanize(*self) {
            format!("{} ({})", self, h)
        } else {
            format!("{}", self)
        }
    }
}

impl Show for DateTime<Tz> {
    fn show(&self, context: &Context) -> String {
        if let Some(h) = context.humanize(*self) {
            format!("{} ({})", self, h)
        } else {
            format!("{}", self)
        }
    }
}

impl Show for GenericDateTime {
    fn show(&self, context: &Context) -> String {
        match *self {
            GenericDateTime::Fixed(ref date) => date.show(context),
            GenericDateTime::Timezone(ref date) => date.show(context),
        }
    }
}

impl Show for Value {
    fn show(&self, context: &Context) -> String {
        match *self {
            Value::Number(ref num) => num.show(context),
            Value::DateTime(ref dt) => dt.show(context),
            Value::Substance(ref v) => v.show(context),
        }
    }
}

impl Value {
    pub fn pow(&self, exp: &Value) -> Result<Value, String> {
        match (self, exp) {
            (&Value::Number(ref left), &Value::Number(ref right)) =>
                left.pow(right).map(Value::Number),
            (_, _) => Err(format!("Operation is not defined"))
        }
    }
}

impl<'a,'b> Add<&'b Value> for &'a Value {
    type Output = Result<Value, String>;

    fn add(self, other: &Value) -> Result<Value, String> {
        match (self, other) {
            (&Value::Number(ref left), &Value::Number(ref right)) =>
                (left + right)
                .ok_or(format!("Addition of units with mismatched units is not meaningful"))
                .map(Value::Number),
            (&Value::DateTime(ref left), &Value::Number(ref right)) |
            (&Value::Number(ref right), &Value::DateTime(ref left)) =>
                match *left {
                    GenericDateTime::Fixed(left) => left.checked_add(try!(date::to_duration(
                        right
                    ))).map(GenericDateTime::Fixed),
                    GenericDateTime::Timezone(left) => left.checked_add(try!(date::to_duration(
                        right
                    ))).map(GenericDateTime::Timezone),
                }
                .ok_or(format!("Implementation error: value is out of range representable by datetime"))
                .map(Value::DateTime),
            (&Value::Substance(ref left), &Value::Substance(ref right)) =>
                left.add(right)
                .map(Value::Substance),
            (_, _) => Err(format!("Operation is not defined"))
        }
    }
}

impl<'a,'b> Sub<&'b Value> for &'a Value {
    type Output = Result<Value, String>;

    fn sub(self, other: &Value) -> Result<Value, String> {
        match (self, other) {
            (&Value::Number(ref left), &Value::Number(ref right)) =>
                (left - right)
                .ok_or(format!("Subtraction of units with mismatched units is not meaningful"))
                .map(Value::Number),
            (&Value::DateTime(ref left), &Value::Number(ref right)) |
            (&Value::Number(ref right), &Value::DateTime(ref left)) =>
                match *left {
                    GenericDateTime::Fixed(left) => left.checked_sub(try!(date::to_duration(
                        right
                    ))).map(GenericDateTime::Fixed),
                    GenericDateTime::Timezone(left) => left.checked_sub(try!(date::to_duration(
                        right
                    ))).map(GenericDateTime::Timezone),
                }
                .ok_or(format!("Implementation error: value is out of range representable by datetime"))
                .map(Value::DateTime),
            (&Value::DateTime(ref left), &Value::DateTime(ref right)) =>
                date::from_duration(&match (left, right) {
                    (&GenericDateTime::Fixed(ref left), &GenericDateTime::Fixed(ref right)) =>
                        *left - *right,
                    (&GenericDateTime::Fixed(ref left), &GenericDateTime::Timezone(ref right)) =>
                        *left - *right,
                    (&GenericDateTime::Timezone(ref left), &GenericDateTime::Timezone(ref right)) =>
                        *left - *right,
                    (&GenericDateTime::Timezone(ref left), &GenericDateTime::Fixed(ref right)) =>
                        *left - *right,
                })
                .map(Value::Number),
            (_, _) => Err(format!("Operation is not defined"))
        }
    }
}

impl<'a> Neg for &'a Value {
    type Output = Result<Value, String>;

    fn neg(self) -> Self::Output {
        match *self {
            Value::Number(ref num) =>
                (-num).ok_or(format!("Bug: Negation should not fail")).map(Value::Number),
            _ => Err(format!("Operation is not defined"))
        }
    }
}

impl<'a,'b> Mul<&'b Value> for &'a Value {
    type Output = Result<Value, String>;

    fn mul(self, other: &Value) -> Result<Value, String> {
        match (self, other) {
            (&Value::Number(ref left), &Value::Number(ref right)) =>
                (left * right)
                .ok_or(format!("Bug: Mul should not fail"))
                .map(Value::Number),
            (&Value::Number(ref co), &Value::Substance(ref sub)) |
            (&Value::Substance(ref sub), &Value::Number(ref co)) =>
                (sub * co).map(Value::Substance),
            (_, _) => Err(format!("Operation is not defined"))
        }
    }
}

impl<'a,'b> Div<&'b Value> for &'a Value {
    type Output = Result<Value, String>;

    fn div(self, other: &Value) -> Result<Value, String> {
        match (self, other) {
            (&Value::Number(ref left), &Value::Number(ref right)) =>
                (left / right)
                .ok_or(format!("Division by zero"))
                .map(Value::Number),
            (&Value::Substance(ref sub), &Value::Number(ref co)) =>
                (sub / co).map(Value::Substance),
            (_, _) => Err(format!("Operation is not defined"))
        }
    }
}