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
/*
 * Copyright Stalwart Labs Ltd. See the COPYING
 * file at the top-level directory of this distribution.
 *
 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
 * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
 * <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
 * option. This file may not be copied, modified, or distributed
 * except according to those terms.
 */

use crate::{Addr, Address, Group};

impl<'x> Address<'x> {
    /// Returns the first address in the list, or the first address in the first group.
    pub fn first(&self) -> Option<&Addr<'x>> {
        match self {
            Address::List(list) => list.first(),
            Address::Group(group) => group.iter().flat_map(|group| group.addresses.iter()).next(),
        }
    }

    /// Returns the last address in the list, or the last address in the last group.
    pub fn last(&self) -> Option<&Addr<'x>> {
        match self {
            Address::List(list) => list.last(),
            Address::Group(group) => group
                .iter()
                .flat_map(|group| group.addresses.iter())
                .next_back(),
        }
    }

    /// Converts the address into a list of `Addr`.
    pub fn into_list(self) -> Vec<Addr<'x>> {
        match self {
            Address::List(list) => list,
            Address::Group(group) => group
                .into_iter()
                .flat_map(|group| group.addresses)
                .collect(),
        }
    }

    /// Converts the address into a group of `Addr`.
    pub fn into_group(self) -> Vec<Group<'x>> {
        match self {
            Address::List(list) => list
                .into_iter()
                .map(|addr| Group {
                    name: None,
                    addresses: vec![addr],
                })
                .collect(),
            Address::Group(group) => group,
        }
    }

    /// Returns the list of addresses, or `None` if the address is a group.
    pub fn as_list(&self) -> Option<&[Addr<'x>]> {
        match self {
            Address::List(list) => Some(list),
            Address::Group(_) => None,
        }
    }

    /// Returns the group of addresses, or `None` if the address is a list.
    pub fn as_group(&self) -> Option<&[Group<'x>]> {
        match self {
            Address::List(_) => None,
            Address::Group(group) => Some(group),
        }
    }

    /// Returns an iterator over the addresses in the list, or the addresses in the groups.
    pub fn iter<'y: 'x>(&'y self) -> Box<dyn DoubleEndedIterator<Item = &Addr<'x>> + 'x> {
        match self {
            Address::List(list) => Box::new(list.iter()),
            Address::Group(group) => {
                Box::new(group.iter().flat_map(|group| group.addresses.iter()))
            }
        }
    }

    /// Returns whether the list contains the given address.
    pub fn contains(&self, addr: &str) -> bool {
        match self {
            Address::List(list) => list.iter().any(|a| {
                a.address
                    .as_ref()
                    .map_or(false, |a| a.eq_ignore_ascii_case(addr))
            }),
            Address::Group(group) => group.iter().any(|group| {
                group.addresses.iter().any(|a| {
                    a.address
                        .as_ref()
                        .map_or(false, |a| a.eq_ignore_ascii_case(addr))
                })
            }),
        }
    }

    pub fn into_owned(self) -> Address<'static> {
        match self {
            Address::List(list) => {
                Address::List(list.into_iter().map(|addr| addr.into_owned()).collect())
            }
            Address::Group(list) => {
                Address::Group(list.into_iter().map(|group| group.into_owned()).collect())
            }
        }
    }
}

impl<'x> Group<'x> {
    pub fn new(name: &'x str, addresses: Vec<Addr<'x>>) -> Self {
        Self {
            name: Some(name.into()),
            addresses,
        }
    }

    pub fn into_owned(self) -> Group<'static> {
        Group {
            name: self.name.map(|s| s.into_owned().into()),
            addresses: self.addresses.into_iter().map(|a| a.into_owned()).collect(),
        }
    }
}

impl<'x> Addr<'x> {
    pub fn new(name: Option<&'x str>, address: &'x str) -> Self {
        Self {
            name: name.map(|name| name.into()),
            address: Some(address.into()),
        }
    }

    pub fn into_owned(self) -> Addr<'static> {
        Addr {
            name: self.name.map(|s| s.into_owned().into()),
            address: self.address.map(|s| s.into_owned().into()),
        }
    }

    pub fn name(&self) -> Option<&str> {
        self.name.as_deref()
    }

    pub fn address(&self) -> Option<&str> {
        self.address.as_deref()
    }
}