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
// Copyright (C) 2018  Project Tsukurou!
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

macro_rules! convert {
    (enum $enum:ty, variant $var:path: type $ty:ty) => {
        impl<'a> From<$ty> for $enum {
            fn from(value: $ty) -> $enum {
                $var(value)
            }
        }

        impl<'a> ::util::convert::TryAsRef<$ty> for $enum {
            fn try_as_ref(&self) -> Option<&$ty> {
                if let $var(value) = self {
                    Some(value)
                } else {
                    None
                }
            }
        }

        impl<'a> ::util::convert::TryAsMut<$ty> for $enum {
            fn try_as_mut(&mut self) -> Option<&mut $ty> {
                if let $var(value) = self {
                    Some(value)
                } else {
                    None
                }
            }
        }
    };

    (enum $enum:ty, type $into:ty: from $($from:ty),*) => {
        $(impl<'a> From<$from> for $enum {
            fn from(value: $from) -> $enum {
                <$enum>::from(<$into>::from(value))
            }
        })*
    };

    (enum $enum:ty, type $val:ty: ref $ref:ty) => {
        impl<'a> ::util::convert::TryAsRef<$ref> for $enum {
            fn try_as_ref(&self) -> Option<&$ref> {
                ::util::convert::TryAsRef::<$val>::try_as_ref(self)
                    .map(AsRef::as_ref)
            }
        }

        impl<'a> ::util::convert::TryAsMut<$ref> for $enum {
            fn try_as_mut(&mut self) -> Option<&mut $ref> {
                ::util::convert::TryAsMut::<$val>::try_as_mut(self)
                    .map(AsMut::as_mut)
            }
        }
    };

    (enum $enum:ty, type $into:ty: from_as $($from:ty),*) => {
        $(impl<'a> From<$from> for $enum {
            fn from(value: $from) -> $enum {
                <$enum>::from(value as $into)
            }
        })*
    };

    (enum $enum:ty: cow $r:ty, $t:ty) => {
        impl<'a> From<&'a $r> for $enum {
            fn from(value: &'a $r) -> $enum {
                <$enum>::from(Cow::Borrowed(value))
            }
        }

        impl<'a> From<$t> for $enum {
            fn from(value: $t) -> $enum {
                <$enum as From<Cow<'a, $r>>>::from(Cow::Owned(value))
            }
        }

        impl<'a> ::util::convert::TryAsRef<$r> for $enum {
            fn try_as_ref(&self) -> Option<&$r> {
                ::util::convert::TryAsRef::<::std::borrow::Cow<'a, $r>>
                    ::try_as_ref(self)
                    .map(::std::ops::Deref::deref)
            }
        }

        impl<'a> ::util::convert::TryAsMut<$t> for $enum {
            fn try_as_mut(&mut self) -> Option<&mut $t> { 
                ::util::convert::TryAsMut::<::std::borrow::Cow<'a, $r>>
                    ::try_as_mut(self)
                    .map(::std::borrow::Cow::to_mut)
            }
        }
    }
}