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
//! Field-level structs.

use std::fmt::{self, Display, Debug};
use std::hash::{Hash, Hasher};

use serde::Serialize;
use csv_sniffer;

/// Identifier for a field in the source.
#[derive(Debug, Clone)]
pub enum FieldIdent {
    /// Unnamed field identifier, using the field index in the source file.
    Index(usize),
    /// Field name in the source file
    Name(String)
}
impl FieldIdent {
    /// Produce a string representation of the field identifier. Either the name if
    /// of the `FieldIdent::Name` variant, or the string "Field #" if using the `FieldIdent::Index`
    /// variant.
    pub fn to_string(&self) -> String {
        match *self {
            FieldIdent::Index(i) => format!("Field {}", i),
            FieldIdent::Name(ref s) => s.clone(),
        }
    }
}
impl fmt::Display for FieldIdent {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        write!(f, "{}", self.to_string())
    }
}
impl PartialEq for FieldIdent {
    fn eq(&self, other: &FieldIdent) -> bool {
        self.to_string().eq(&other.to_string())
    }
}
impl Eq for FieldIdent {}
impl Hash for FieldIdent {
    fn hash<H>(&self, state: &mut H) where H: Hasher {
        self.to_string().hash(state)
    }
}

impl From<usize> for FieldIdent {
    fn from(src: usize) -> FieldIdent {
        FieldIdent::Index(src)
    }
}
impl<'a> From<&'a str> for FieldIdent {
    fn from(src: &'a str) -> FieldIdent {
        FieldIdent::Name(src.to_string())
    }
}
impl From<String> for FieldIdent {
    fn from(src: String) -> FieldIdent {
        FieldIdent::Name(src)
    }
}

/// Valid field types
#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
pub enum FieldType {
    /// Unsigned integer field
    Unsigned,
    /// Signed integer field
    Signed,
    /// Text (string) field
    Text,
    /// Boolean (yes/no) field
    Boolean,
    /// Floating-point field
    Float
}
impl From<csv_sniffer::Type> for FieldType {
    fn from(orig: csv_sniffer::Type) -> FieldType {
        match orig {
            csv_sniffer::Type::Unsigned => FieldType::Unsigned,
            csv_sniffer::Type::Signed   => FieldType::Signed,
            csv_sniffer::Type::Text     => FieldType::Text,
            csv_sniffer::Type::Boolean  => FieldType::Boolean,
            csv_sniffer::Type::Float     => FieldType::Float,
        }
    }
}
impl fmt::Display for FieldType {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        match *self {
            FieldType::Unsigned => write!(f, "unsigned"),
            FieldType::Signed   => write!(f, "signed"),
            FieldType::Text     => write!(f, "text"),
            FieldType::Boolean  => write!(f, "boolean"),
            FieldType::Float    => write!(f, "float"),
        }
    }
}

/// Marker trait for types supported by Agnes data structures
pub trait DataType: PartialOrd + Serialize + Display + Debug + DtZero {}
impl DataType for u64 {}
impl DataType for i64 {}
impl DataType for String {}
impl DataType for bool {}
impl DataType for f64 {}

impl<'a, T> DataType for &'a T where T: DataType {}


/// Common enum for a single value of any of the valid Agnes data types.
#[derive(Debug, Clone, PartialOrd, PartialEq)]
pub enum DtValue {
    /// Unsigned integer value
    Unsigned(u64),
    /// Signed integer value
    Signed(i64),
    /// Text value
    Text(String),
    /// Boolean value
    Boolean(bool),
    /// Floating-point value
    Float(f64),
}
impl From<u64> for DtValue {
    fn from(orig: u64) -> DtValue { DtValue::Unsigned(orig) }
}
impl From<i64> for DtValue {
    fn from(orig: i64) -> DtValue { DtValue::Signed(orig) }
}
impl From<String> for DtValue {
    fn from(orig: String) -> DtValue { DtValue::Text(orig) }
}
impl From<bool> for DtValue {
    fn from(orig: bool) -> DtValue { DtValue::Boolean(orig) }
}
impl From<f64> for DtValue {
    fn from(orig: f64) -> DtValue { DtValue::Float(orig) }
}

/// Trait to produce a valid value to indicate 'zero' for Agnes data types. Used when computing
/// things like sums (which make sence when the data type is an integer or float, but maybe less
/// sense when text or a boolean).
pub trait DtZero {
    /// The type of the 'zero' value for this Agnes data type.
    type Output;
    /// Provide the 'zero' value for this Agnes data type.
    fn dt_zero() -> Self::Output;
}
impl DtZero for u64 {
    type Output = u64;
    fn dt_zero() -> u64 { 0 }
}
impl DtZero for i64 {
    type Output = i64;
    fn dt_zero() -> i64 { 0 }
}
impl DtZero for String {
    type Output = u64;
    fn dt_zero() -> u64 { 0 }
}
impl DtZero for bool {
    type Output = u64;
    fn dt_zero() -> u64 { 0 }
}
impl DtZero for f64 {
    type Output = f64;
    fn dt_zero() -> f64 { 0.0 }
}
impl<'a, T> DtZero for &'a T where T: DtZero {
    type Output = <T as DtZero>::Output;
    fn dt_zero() -> Self::Output { T::dt_zero() }
}


/// Possibly-renamed field identifier
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RFieldIdent {
    /// Original field identifier
    pub ident: FieldIdent,
    /// Renamed name (if exists)
    pub rename: Option<String>,
}
impl RFieldIdent {
    /// Produce a string representation of this `RFieldIdent`. Uses the renamed name (if exists),
    /// of the result of `to_string` on the underlying `FieldIdent`.
    pub fn to_string(&self) -> String {
        self.rename.clone().unwrap_or(self.ident.to_string())
    }
    /// Produce a new `FieldIdent` using the `rename` value of this `RFieldIdent` (if exists), or
    /// simply a clone of the underlying `FieldIdent`.
    pub fn to_renamed_field_ident(&self) -> FieldIdent {
        match self.rename {
            Some(ref renamed) => FieldIdent::Name(renamed.clone()),
            None              => self.ident.clone()
        }
    }
}

/// Field identifier along with an associated type.
#[derive(Debug, Clone)]
pub struct TypedFieldIdent {
    /// Field identifier (name or original column number)
    pub ident: FieldIdent,
    /// Field type
    pub ty: FieldType
}
impl TypedFieldIdent {
    /// Create a new typed field identifier
    pub fn new(ident: FieldIdent, ty: FieldType) -> TypedFieldIdent {
        TypedFieldIdent {
            ident: ident,
            ty: ty
        }
    }
}

/// Specification of a typed field identifier along with the index in the original source data file.
#[derive(Debug, Clone)]
pub struct SrcField {
    /// Field identifier and type
    pub ty_ident: TypedFieldIdent,
    /// Index of field within the original data file
    pub src_index: usize,
}
impl SrcField {
    /// Create a new `SrcField` object from specified field identifier, type, and source index.
    pub fn new(ident: FieldIdent, ty: FieldType, src_index: usize) -> SrcField {
        SrcField {
            ty_ident: TypedFieldIdent::new(ident, ty),
            src_index: src_index
        }
    }
    /// Create a new `SrcField` object from specified typed field identifier obejct ans source
    /// index.
    pub fn from_ty_ident(ty_ident: TypedFieldIdent, src_index: usize) -> SrcField {
        SrcField {
            ty_ident: ty_ident,
            src_index: src_index
        }
    }
}

/// Details of a field within a data store
#[derive(Debug, Clone)]
pub struct DsField {
    /// Field identifier and type
    pub ty_ident: TypedFieldIdent,
    /// Index of field within the data store
    pub ds_index: usize,
}
impl DsField {
    /// Create a new `DsField` from field identifier, type, and data store index
    pub fn new(ident: FieldIdent, ty: FieldType, ds_index: usize) -> DsField {
        DsField {
            ty_ident: TypedFieldIdent::new(ident, ty),
            ds_index: ds_index,
        }
    }
    /// Create a new `DsField` from a typed field identifier and a data store index
    pub fn from_typed_field_ident(ty_ident: TypedFieldIdent, ds_index: usize) -> DsField {
        DsField {
            ty_ident: ty_ident,
            ds_index: ds_index,
        }
    }
    /// Create a new `DsField` from a `SrcField` object and data store index. The source index from
    /// the `SrcField` object will not be included in the new object.
    pub fn from_src(src: &SrcField, ds_index: usize) -> DsField {
        DsField {
            ty_ident: src.ty_ident.clone(),
            ds_index: ds_index
        }
    }
}

#[macro_export]
macro_rules! fields {
    ($($name:expr => $ty:expr),*) => {{
        use $crate::field::TypedFieldIdent;

        vec![$(
            TypedFieldIdent::new(
                FieldIdent::Name($name.to_string()),
                $ty
            )
        ),*]
    }}
}