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
// ------------------------------------------------------------------------------
// Copyright 2018 Uwe Arzt, mail@uwe-arzt.de
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------------

use std::fmt;
use std::str::FromStr;
use std::num::ParseIntError;
use nom::rest_s;

#[derive(PartialEq, Eq, Hash)]
pub struct Address {
    address_type: AddressType,
    main_group: u8,
    mid_group: u8,
    address: u8,
}

#[derive(PartialEq, Eq, Hash)]
pub enum AddressType {
    Individual,
    Group,
}

impl Address {
    /// Create an Address
    pub fn new(address_type: AddressType, main_group: u8, mid_group: u8, address: u8) -> Address {
        Address {
            address_type: address_type,
            main_group: main_group,
            mid_group: mid_group,
            address: address,
        }
    }
    pub fn main(&self) -> u8 {
        self.main_group
    }
    pub fn middle(&self) -> (u8, u8) {
        (self.main_group, self.mid_group)
    }
}

impl FromStr for Address {
    type Err = ParseIntError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        //let ret = parse(CompleteStr(s)).unwrap();
        let ret = parse(s).unwrap();
        let (main, middle, address) = ret.1;
        println!("############# {} {} {}", main, middle, address);
        Ok(Address::new(
            AddressType::Group,
             u8::from_str(main).unwrap(),
             u8::from_str(middle).unwrap(),
             u8::from_str(address).unwrap(),
        ))
    }
}

 named!(parse<&str, (&str, &str, &str)>,
  do_parse!(
    main: take_until_and_consume!("/") >>
    middle: take_until_and_consume!("/") >>
    address: rest_s >>
    ((&main, &middle, &address))
  )
 );

impl fmt::Display for Address {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.address_type {
            AddressType::Group => {
                write!(f, "{}/{}/{}", self.main_group, self.mid_group, self.address)
            }
            AddressType::Individual => {
                write!(f, "{}.{}.{}", self.main_group, self.mid_group, self.address)
            }
        }
    }
}
impl fmt::Debug for Address {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{{ {} }}", self)
    }
}

#[cfg(test)]
mod tests {

    use crate::address::Address;
    use crate::address::AddressType;
    use std::str::FromStr;

    #[test]
    fn t_knx_address() {
        assert_eq!(
            Address::from_str("1/2/3").unwrap(),
            Address::new(AddressType::Group, 1, 2, 3)
        );
        assert_eq!(
            Address::from_str("15/15/255").unwrap(),
            Address::new(AddressType::Group, 15, 15, 255)
        );
    }
}