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
use super::{types, FieldType, FieldValue};

/// Errors that can happen when trying to convert a FieldValue into
/// a more concrete type
#[derive(Debug)]
pub enum FieldConversionError {
    /// Happens when the conversion could not be mode because the FieldType
    /// does not mat the expected one
    FieldTypeNotAsExpected {
        /// The expected FieldType of the FieldValue the conversion was tried on
        expected: FieldType,
        /// The actual FieldType of the FieldValue the conversion was tried on
        actual: FieldType,
    },
    IncompatibleType,
    /// The value written is the file was only pad bytes / uninitialized
    /// and the user tried to convert it into a non Option-Type
    NoneValue,
}

impl std::fmt::Display for FieldConversionError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            FieldConversionError::FieldTypeNotAsExpected { expected, actual } => {
                write!(f, "Cannot convert from {} to {}", expected, actual)
            }
            FieldConversionError::IncompatibleType => write!(f, "The type is not compatible"),
            FieldConversionError::NoneValue => {
                write!(f, "Value is not initialized, which is not allowed")
            }
        }
    }
}

impl std::error::Error for FieldConversionError {}

macro_rules! impl_try_from_field_value_for_ {
    (FieldValue::$variant:ident => $out_type:ty) => {
        impl TryFrom<FieldValue> for $out_type {
            type Error = FieldConversionError;

            fn try_from(value: FieldValue) -> Result<Self, Self::Error> {
                if let FieldValue::$variant(v) = value {
                    Ok(v)
                } else {
                    Err(FieldConversionError::FieldTypeNotAsExpected {
                        expected: FieldType::$variant,
                        actual: value.field_type(),
                    })
                }
            }
        }
    };
    (FieldValue::$variant:ident(Some($v:ident)) => $out_type:ty) => {
        impl TryFrom<FieldValue> for $out_type {
            type Error = FieldConversionError;

            fn try_from(value: FieldValue) -> Result<Self, Self::Error> {
                match value {
                    FieldValue::$variant(Some($v)) => Ok($v),
                    FieldValue::$variant(None) => Err(FieldConversionError::NoneValue),
                    _ => Err(FieldConversionError::FieldTypeNotAsExpected {
                        expected: FieldType::$variant,
                        actual: value.field_type(),
                    }),
                }
            }
        }
    };
}

impl_try_from_field_value_for_!(FieldValue::Numeric => Option<f64>);

impl_try_from_field_value_for_!(FieldValue::Float => Option<f32>);
impl_try_from_field_value_for_!(FieldValue::Float(Some(v)) => f32);

impl_try_from_field_value_for_!(FieldValue::Date => Option<types::Date>);
impl_try_from_field_value_for_!(FieldValue::Date(Some(v)) => types::Date);

impl_try_from_field_value_for_!(FieldValue::Character => Option<String>);
impl_try_from_field_value_for_!(FieldValue::Character(Some(string)) => String);

impl_try_from_field_value_for_!(FieldValue::Logical => Option<bool>);
impl_try_from_field_value_for_!(FieldValue::Logical(Some(b)) => bool);

impl_try_from_field_value_for_!(FieldValue::Integer => i32);

impl TryFrom<FieldValue> for f64 {
    type Error = FieldConversionError;

    fn try_from(value: FieldValue) -> Result<Self, Self::Error> {
        match value {
            FieldValue::Numeric(Some(v)) => Ok(v),
            FieldValue::Numeric(None) => Err(FieldConversionError::NoneValue),
            FieldValue::Currency(c) => Ok(c),
            FieldValue::Double(d) => Ok(d),
            _ => Err(FieldConversionError::IncompatibleType),
        }
    }
}

// Fox Pro types
impl_try_from_field_value_for_!(FieldValue::DateTime => types::DateTime);

macro_rules! impl_from_type_for_field_value (
    ($t:ty => FieldValue::$variant:ident) => {
        impl From<$t> for FieldValue {
            fn from(v: $t) -> Self {
                FieldValue::$variant(v)
            }
        }
    };
    ($t:ty => FieldValue::$variant:ident(Some($v:ident))) => {
        impl From<$t> for FieldValue {
            fn from(v: $t) -> Self {
                FieldValue::$variant(Some(v))
            }
        }
    }
);

impl_from_type_for_field_value!(Option<String> => FieldValue::Character);
impl_from_type_for_field_value!(String => FieldValue::Character(Some(s)));

impl_from_type_for_field_value!(Option<f64> => FieldValue::Numeric);
impl_from_type_for_field_value!(f64 => FieldValue::Numeric(Some(v)));

impl_from_type_for_field_value!(Option<f32> => FieldValue::Float);
impl_from_type_for_field_value!(f32 => FieldValue::Float(Some(v)));

impl_from_type_for_field_value!(Option<bool> => FieldValue::Logical);
impl_from_type_for_field_value!(bool => FieldValue::Logical(Some(v)));

impl_from_type_for_field_value!(Option<types::Date> => FieldValue::Date);
impl_from_type_for_field_value!(types::Date => FieldValue::Date(Some(v)));

// Fox Pro types
impl_from_type_for_field_value!(types::DateTime => FieldValue::DateTime);