rxing/client/result/
AddressBookParsedResult.rs

1/*
2 * Copyright 2007 ZXing authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// package com.google.zxing.client.result;
18
19use crate::common::Result;
20use crate::exceptions::Exceptions;
21
22use super::{ParsedRXingResult, ParsedRXingResultType, ResultParser};
23
24/**
25 * Represents a parsed result that encodes contact information, like that in an address book
26 * entry.
27 *
28 * @author Sean Owen
29 */
30#[derive(PartialEq, Eq, Hash, Debug)]
31pub struct AddressBookParsedRXingResult {
32    names: Vec<String>,
33    nicknames: Vec<String>,
34    pronunciation: String,
35    phone_numbers: Vec<String>,
36    phone_types: Vec<String>,
37    emails: Vec<String>,
38    email_types: Vec<String>,
39    instant_messenger: String,
40    note: String,
41    addresses: Vec<String>,
42    address_types: Vec<String>,
43    org: String,
44    birthday: String,
45    title: String,
46    urls: Vec<String>,
47    geo: Vec<String>,
48}
49impl ParsedRXingResult for AddressBookParsedRXingResult {
50    fn getType(&self) -> super::ParsedRXingResultType {
51        ParsedRXingResultType::Addressbook
52    }
53
54    fn getDisplayRXingResult(&self) -> String {
55        let mut result = String::with_capacity(100);
56
57        ResultParser::maybe_append_multiple(&self.names, &mut result);
58        ResultParser::maybe_append_multiple(&self.nicknames, &mut result);
59        ResultParser::maybe_append_string(&self.pronunciation, &mut result);
60        ResultParser::maybe_append_string(&self.title, &mut result);
61        ResultParser::maybe_append_string(&self.org, &mut result);
62        ResultParser::maybe_append_multiple(&self.addresses, &mut result);
63        ResultParser::maybe_append_multiple(&self.phone_numbers, &mut result);
64        ResultParser::maybe_append_multiple(&self.emails, &mut result);
65        ResultParser::maybe_append_string(&self.instant_messenger, &mut result);
66        ResultParser::maybe_append_multiple(&self.urls, &mut result);
67        ResultParser::maybe_append_string(&self.birthday, &mut result);
68        ResultParser::maybe_append_multiple(&self.geo, &mut result);
69        ResultParser::maybe_append_string(&self.note, &mut result);
70
71        result
72    }
73}
74impl AddressBookParsedRXingResult {
75    pub fn new(
76        names: Vec<String>,
77        phone_numbers: Vec<String>,
78        phone_types: Vec<String>,
79        emails: Vec<String>,
80        email_types: Vec<String>,
81        addresses: Vec<String>,
82        address_types: Vec<String>,
83    ) -> Result<Self> {
84        Self::with_details(
85            names,
86            Vec::new(),
87            String::default(),
88            phone_numbers,
89            phone_types,
90            emails,
91            email_types,
92            String::default(),
93            String::default(),
94            addresses,
95            address_types,
96            String::default(),
97            String::default(),
98            String::default(),
99            Vec::new(),
100            Vec::new(),
101        )
102    }
103
104    #[allow(clippy::too_many_arguments)]
105    pub fn with_details(
106        names: Vec<String>,
107        nicknames: Vec<String>,
108        pronunciation: String,
109        phone_numbers: Vec<String>,
110        phone_types: Vec<String>,
111        emails: Vec<String>,
112        email_types: Vec<String>,
113        instant_messenger: String,
114        note: String,
115        addresses: Vec<String>,
116        address_types: Vec<String>,
117        org: String,
118        birthday: String,
119        title: String,
120        urls: Vec<String>,
121        geo: Vec<String>,
122    ) -> Result<Self> {
123        if phone_numbers.len() != phone_types.len() && !phone_types.is_empty() {
124            return Err(Exceptions::illegal_argument_with(
125                "Phone numbers and types lengths differ",
126            ));
127        }
128        if emails.len() != email_types.len() && !email_types.is_empty() {
129            return Err(Exceptions::illegal_argument_with(
130                "Emails and types lengths differ",
131            ));
132        }
133        if addresses.len() != address_types.len() && !address_types.is_empty() {
134            return Err(Exceptions::illegal_argument_with(
135                "Addresses and types lengths differ",
136            ));
137        }
138        Ok(Self {
139            names,
140            nicknames,
141            pronunciation,
142            phone_numbers,
143            phone_types,
144            emails,
145            email_types,
146            instant_messenger,
147            note,
148            addresses,
149            address_types,
150            org,
151            birthday,
152            title,
153            urls,
154            geo,
155        })
156    }
157
158    pub fn getNames(&self) -> &Vec<String> {
159        &self.names
160    }
161
162    pub fn getNicknames(&self) -> &Vec<String> {
163        &self.nicknames
164    }
165
166    /**
167     * In Japanese, the name is written in kanji, which can have multiple readings. Therefore a hint
168     * is often provided, called furigana, which spells the name phonetically.
169     *
170     * @return The pronunciation of the getNames() field, often in hiragana or katakana.
171     */
172    pub fn getPronunciation(&self) -> &str {
173        &self.pronunciation
174    }
175
176    pub fn getPhoneNumbers(&self) -> &Vec<String> {
177        &self.phone_numbers
178    }
179
180    /**
181     * @return optional descriptions of the type of each phone number. It could be like "HOME", but,
182     *  there is no guaranteed or standard format.
183     */
184    pub fn getPhoneTypes(&self) -> &Vec<String> {
185        &self.phone_types
186    }
187
188    pub fn getEmails(&self) -> &Vec<String> {
189        &self.emails
190    }
191
192    /**
193     * @return optional descriptions of the type of each e-mail. It could be like "WORK", but,
194     *  there is no guaranteed or standard format.
195     */
196    pub fn getEmailTypes(&self) -> &Vec<String> {
197        &self.email_types
198    }
199
200    pub fn getInstantMessenger(&self) -> &str {
201        &self.instant_messenger
202    }
203
204    pub fn getNote(&self) -> &str {
205        &self.note
206    }
207
208    pub fn getAddresses(&self) -> &Vec<String> {
209        &self.addresses
210    }
211
212    /**
213     * @return optional descriptions of the type of each e-mail. It could be like "WORK", but,
214     *  there is no guaranteed or standard format.
215     */
216    pub fn getAddressTypes(&self) -> &Vec<String> {
217        &self.address_types
218    }
219
220    pub fn getTitle(&self) -> &str {
221        &self.title
222    }
223
224    pub fn getOrg(&self) -> &str {
225        &self.org
226    }
227
228    pub fn getURLs(&self) -> &Vec<String> {
229        &self.urls
230    }
231
232    /**
233     * @return birthday formatted as yyyyMMdd (e.g. 19780917)
234     */
235    pub fn getBirthday(&self) -> &str {
236        &self.birthday
237    }
238
239    /**
240     * @return a location as a latitude/longitude pair
241     */
242    pub fn getGeo(&self) -> &Vec<String> {
243        &self.geo
244    }
245}