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
/*
 * metadata.rs: Data types handling handling for renv.
 * Copyright (C) 2019  Oddcoder
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
use super::environment::Environment;

pub type StrFn<T> = fn(&str, &str, &Environment<T>, &mut T) -> bool;
pub type U64Fn<T> = fn(&str, u64, &Environment<T>, &mut T) -> bool;
pub type I64Fn<T> = fn(&str, i64, &Environment<T>, &mut T) -> bool;
pub type BoolFn<T> = fn(&str, bool, &Environment<T>, &mut T) -> bool;
pub type ColorFn<T> = fn(&str, (u8, u8, u8), &Environment<T>, &mut T) -> bool;

pub(crate) struct EnvStr<T> {
    pub(crate) data: String,
    pub(crate) default: String,
    pub(crate) help: String,
    pub(crate) cb: Option<StrFn<T>>,
}

pub(crate) struct EnvU64<T> {
    pub(crate) data: u64,
    pub(crate) default: u64,
    pub(crate) help: String,
    pub(crate) cb: Option<U64Fn<T>>,
}

pub(crate) struct EnvI64<T> {
    pub(crate) data: i64,
    pub(crate) default: i64,
    pub(crate) help: String,
    pub(crate) cb: Option<I64Fn<T>>,
}

pub(crate) struct EnvBool<T> {
    pub(crate) data: bool,
    pub(crate) default: bool,
    pub(crate) help: String,
    pub(crate) cb: Option<BoolFn<T>>,
}

pub(crate) struct EnvColor<T> {
    pub(crate) data: (u8, u8, u8),
    pub(crate) default: (u8, u8, u8),
    pub(crate) help: String,
    pub(crate) cb: Option<ColorFn<T>>,
}

pub(crate) enum EnvMetaData<T> {
    Str(EnvStr<T>),
    U64(EnvU64<T>),
    I64(EnvI64<T>),
    Bool(EnvBool<T>),
    Color(EnvColor<T>),
}

impl<T> EnvMetaData<T> {
    pub(crate) fn as_str(&self) -> Option<&EnvStr<T>> {
        if let EnvMetaData::Str(s) = self {
            return Some(s);
        }
        return None;
    }
    pub(crate) fn as_u64(&self) -> Option<&EnvU64<T>> {
        if let EnvMetaData::U64(u) = self {
            return Some(u);
        }
        return None;
    }
    pub(crate) fn as_i64(&self) -> Option<&EnvI64<T>> {
        if let EnvMetaData::I64(i) = self {
            return Some(i);
        }
        return None;
    }
    pub(crate) fn as_bool(&self) -> Option<&EnvBool<T>> {
        if let EnvMetaData::Bool(b) = self {
            return Some(b);
        }
        return None;
    }
    pub(crate) fn as_color(&self) -> Option<&EnvColor<T>> {
        if let EnvMetaData::Color(c) = self {
            return Some(c);
        }
        return None;
    }
    pub(crate) fn mut_str(&mut self) -> Option<&mut EnvStr<T>> {
        if let EnvMetaData::Str(s) = self {
            return Some(s);
        }
        return None;
    }
    pub(crate) fn mut_u64(&mut self) -> Option<&mut EnvU64<T>> {
        if let EnvMetaData::U64(u) = self {
            return Some(u);
        }
        return None;
    }
    pub(crate) fn mut_i64(&mut self) -> Option<&mut EnvI64<T>> {
        if let EnvMetaData::I64(i) = self {
            return Some(i);
        }
        return None;
    }
    pub(crate) fn mut_bool(&mut self) -> Option<&mut EnvBool<T>> {
        if let EnvMetaData::Bool(b) = self {
            return Some(b);
        }
        return None;
    }
    pub(crate) fn mut_color(&mut self) -> Option<&mut EnvColor<T>> {
        if let EnvMetaData::Color(c) = self {
            return Some(c);
        }
        return None;
    }
}