├── .github ├── dependabot.yml └── workflows │ ├── ci-version.yml │ └── ci.yml ├── .gitignore ├── Cargo.toml ├── LICENSE ├── README.md ├── examples ├── create_personal_card.rs └── data │ └── photo.png ├── rustfmt.toml ├── src ├── escaping │ └── mod.rs ├── lib.rs ├── parameters │ ├── alternative_id.rs │ ├── any.rs │ ├── calscale.rs │ ├── geo.rs │ ├── label.rs │ ├── language.rs │ ├── media_type.rs │ ├── mod.rs │ ├── preference.rs │ ├── property_id.rs │ ├── sort_as.rs │ ├── time_zone.rs │ ├── typ.rs │ └── value.rs ├── properties │ ├── address.rs │ ├── anniversary.rs │ ├── begin.rs │ ├── birthday.rs │ ├── calendar_address_uri.rs │ ├── calendar_uri.rs │ ├── category.rs │ ├── client_property_id_map.rs │ ├── email.rs │ ├── end.rs │ ├── fburl.rs │ ├── formatted_name.rs │ ├── gender.rs │ ├── geo.rs │ ├── impp.rs │ ├── key.rs │ ├── language.rs │ ├── logo.rs │ ├── member.rs │ ├── mod.rs │ ├── name.rs │ ├── nickname.rs │ ├── note.rs │ ├── organization.rs │ ├── photo.rs │ ├── product_id.rs │ ├── relationship.rs │ ├── revision.rs │ ├── role.rs │ ├── sound.rs │ ├── source.rs │ ├── telephone.rs │ ├── time_zone.rs │ ├── title.rs │ ├── uid.rs │ ├── url.rs │ ├── version.rs │ └── x_property.rs └── values │ ├── address_value.rs │ ├── attribute_value.rs │ ├── audio_value.rs │ ├── boolean.rs │ ├── calscale_value.rs │ ├── client_property_id_map_value.rs │ ├── date_time.rs │ ├── email_value.rs │ ├── float.rs │ ├── gender_value.rs │ ├── geo_value.rs │ ├── image_value.rs │ ├── integer.rs │ ├── key_value.rs │ ├── kind_value.rs │ ├── language_tag.rs │ ├── mod.rs │ ├── name_value.rs │ ├── parameter_value.rs │ ├── preference_value.rs │ ├── product_id_value.rs │ ├── property_id_value.rs │ ├── telephone_value.rs │ ├── text.rs │ ├── time_zone_value.rs │ ├── type_value.rs │ ├── uid_value.rs │ ├── uri.rs │ ├── url.rs │ ├── uuid.rs │ ├── value_type.rs │ └── version_value.rs └── tests └── vcard.rs /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: github-actions 4 | directory: "/" 5 | schedule: 6 | interval: "weekly" -------------------------------------------------------------------------------- /.github/workflows/ci-version.yml: -------------------------------------------------------------------------------- 1 | name: CI-version 2 | 3 | on: 4 | push: 5 | tags: 6 | - "v*" 7 | 8 | env: 9 | CARGO_TERM_COLOR: always 10 | 11 | jobs: 12 | tests: 13 | strategy: 14 | fail-fast: false 15 | matrix: 16 | os: 17 | - ubuntu-latest 18 | - macos-latest 19 | - windows-latest 20 | toolchain: 21 | - stable 22 | - nightly 23 | features: 24 | - 25 | name: Test ${{ matrix.toolchain }} on ${{ matrix.os }} (${{ matrix.features }}) 26 | runs-on: ${{ matrix.os }} 27 | steps: 28 | - uses: actions/checkout@v4 29 | - uses: actions-rust-lang/setup-rust-toolchain@v1 30 | with: 31 | toolchain: ${{ matrix.toolchain }} 32 | - run: cargo test --release ${{ matrix.features }} 33 | - run: cargo doc --release ${{ matrix.features }} 34 | 35 | MSRV: 36 | strategy: 37 | fail-fast: false 38 | matrix: 39 | os: 40 | - ubuntu-latest 41 | - macos-latest 42 | - windows-latest 43 | toolchain: 44 | - 1.65 45 | features: 46 | - 47 | name: Test ${{ matrix.toolchain }} on ${{ matrix.os }} (${{ matrix.features }}) 48 | runs-on: ${{ matrix.os }} 49 | steps: 50 | - uses: actions/checkout@v4 51 | - uses: actions-rust-lang/setup-rust-toolchain@v1 52 | with: 53 | toolchain: ${{ matrix.toolchain }} 54 | - run: cargo test --release --lib --bins ${{ matrix.features }} -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: [ push, pull_request ] 4 | 5 | env: 6 | CARGO_TERM_COLOR: always 7 | 8 | jobs: 9 | rustfmt: 10 | runs-on: ubuntu-latest 11 | steps: 12 | - uses: actions/checkout@v4 13 | - uses: actions-rust-lang/setup-rust-toolchain@v1 14 | with: 15 | toolchain: nightly 16 | components: rustfmt 17 | - uses: actions-rust-lang/rustfmt@v1 18 | 19 | clippy: 20 | runs-on: ubuntu-latest 21 | steps: 22 | - uses: actions/checkout@v4 23 | - uses: actions-rust-lang/setup-rust-toolchain@v1 24 | with: 25 | components: clippy 26 | - run: cargo clippy --all-targets --all-features -- -D warnings 27 | 28 | tests: 29 | strategy: 30 | fail-fast: false 31 | matrix: 32 | os: 33 | - ubuntu-latest 34 | - macos-latest 35 | - windows-latest 36 | toolchain: 37 | - stable 38 | - nightly 39 | features: 40 | - 41 | name: Test ${{ matrix.toolchain }} on ${{ matrix.os }} (${{ matrix.features }}) 42 | runs-on: ${{ matrix.os }} 43 | steps: 44 | - uses: actions/checkout@v4 45 | - uses: actions-rust-lang/setup-rust-toolchain@v1 46 | with: 47 | toolchain: ${{ matrix.toolchain }} 48 | - run: cargo test ${{ matrix.features }} 49 | - run: cargo doc ${{ matrix.features }} 50 | 51 | MSRV: 52 | strategy: 53 | fail-fast: false 54 | matrix: 55 | os: 56 | - ubuntu-latest 57 | - macos-latest 58 | - windows-latest 59 | toolchain: 60 | - 1.65 61 | features: 62 | - 63 | name: Test ${{ matrix.toolchain }} on ${{ matrix.os }} (${{ matrix.features }}) 64 | runs-on: ${{ matrix.os }} 65 | steps: 66 | - uses: actions/checkout@v4 67 | - uses: actions-rust-lang/setup-rust-toolchain@v1 68 | with: 69 | toolchain: ${{ matrix.toolchain }} 70 | - run: cargo test --lib --bins ${{ matrix.features }} -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io/api/intellij+all 2 | 3 | ### Intellij+all ### 4 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 5 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 6 | 7 | # User-specific stuff 8 | .idea/**/workspace.xml 9 | .idea/**/tasks.xml 10 | .idea/**/usage.statistics.xml 11 | .idea/**/dictionaries 12 | .idea/**/shelf 13 | 14 | # Sensitive or high-churn files 15 | .idea/**/dataSources/ 16 | .idea/**/dataSources.ids 17 | .idea/**/dataSources.local.xml 18 | .idea/**/sqlDataSources.xml 19 | .idea/**/dynamic.xml 20 | .idea/**/uiDesigner.xml 21 | .idea/**/dbnavigator.xml 22 | 23 | # Gradle 24 | .idea/**/gradle.xml 25 | .idea/**/libraries 26 | 27 | # Gradle and Maven with auto-import 28 | # When using Gradle or Maven with auto-import, you should exclude module files, 29 | # since they will be recreated, and may cause churn. Uncomment if using 30 | # auto-import. 31 | # .idea/modules.xml 32 | # .idea/*.iml 33 | # .idea/modules 34 | 35 | # CMake 36 | cmake-build-*/ 37 | 38 | # Mongo Explorer plugin 39 | .idea/**/mongoSettings.xml 40 | 41 | # File-based project format 42 | *.iws 43 | 44 | # IntelliJ 45 | out/ 46 | 47 | # mpeltonen/sbt-idea plugin 48 | .idea_modules/ 49 | 50 | # JIRA plugin 51 | atlassian-ide-plugin.xml 52 | 53 | # Cursive Clojure plugin 54 | .idea/replstate.xml 55 | 56 | # Crashlytics plugin (for Android Studio and IntelliJ) 57 | com_crashlytics_export_strings.xml 58 | crashlytics.properties 59 | crashlytics-build.properties 60 | fabric.properties 61 | 62 | # Editor-based Rest Client 63 | .idea/httpRequests 64 | 65 | ### Intellij+all Patch ### 66 | # Ignores the whole .idea folder and all .iml files 67 | # See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360 68 | 69 | .idea/ 70 | 71 | # Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023 72 | 73 | *.iml 74 | modules.xml 75 | .idea/misc.xml 76 | *.ipr 77 | 78 | 79 | # End of https://www.gitignore.io/api/intellij+all 80 | 81 | 82 | ### Rust ### 83 | # Generated by Cargo 84 | # will have compiled files and executables 85 | /target/ 86 | 87 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 88 | # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html 89 | Cargo.lock 90 | 91 | # These are backup files generated by rustfmt 92 | **/*.rs.bk 93 | 94 | 95 | # End of https://www.gitignore.io/api/rust 96 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "vcard" 3 | version = "0.4.13" 4 | authors = ["Magic Len "] 5 | edition = "2021" 6 | rust-version = "1.65" 7 | repository = "https://github.com/magiclen/vcard" 8 | homepage = "https://magiclen.org/vcard" 9 | keywords = ["vcard", "rfc6350", "rfc-6350"] 10 | categories = ["value-formatting", "parser-implementations"] 11 | description = "A pure Rust implementation of vCard based on RFC 6350." 12 | license = "MIT" 13 | include = ["src/**/*", "Cargo.toml", "README.md", "LICENSE"] 14 | 15 | [dependencies] 16 | base64-stream = "2" 17 | mime = "0.3.12" 18 | mime_guess = "2" 19 | chrono = "0.4.6" 20 | chrono-tz = "0.8" 21 | regex = "1.1" 22 | lazy_static = "1.1" 23 | percent-encoding = "2" 24 | idna = "0.4" 25 | 26 | [dependencies.validators] 27 | version = "0.20.2" 28 | features = ["phone-number"] 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 magiclen.org (Ron Li) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | vCard 2 | ==================== 3 | 4 | [![CI](https://github.com/magiclen/vcard/actions/workflows/ci.yml/badge.svg)](https://github.com/magiclen/vcard/actions/workflows/ci.yml) 5 | 6 | A pure Rust implementation of vCard based on RFC 6350. 7 | 8 | ## Example 9 | 10 | ```rust 11 | use std::collections::HashSet; 12 | 13 | use vcard::{Set, VCard, XPropertyName, IanaToken}; 14 | use vcard::properties::*; 15 | use vcard::values::text::{Text, Component}; 16 | use vcard::values::language_tag::LanguageTag; 17 | use vcard::values::name_value::NameValue; 18 | use vcard::values::image_value::ImageValue; 19 | use vcard::values::date_time::{DateAndOrTime, Date, Timestamp}; 20 | use vcard::values::gender_value::{GenderValue, SexType}; 21 | use vcard::values::address_value::AddressValue; 22 | use vcard::values::type_value::{TypeValue, TypeValueWithTelephoneType}; 23 | use vcard::values::telephone_value::TelephoneValue; 24 | use vcard::values::email_value::EmailValue; 25 | use vcard::values::url; 26 | 27 | use vcard::parameters::language::Language; 28 | use vcard::parameters::typ::{Type, TypeWithTelType}; 29 | 30 | use vcard::chrono::prelude::*; 31 | 32 | let formatted_names = { 33 | let mut formatted_names = HashSet::new(); 34 | 35 | let english_name = { 36 | let mut formatted_name = FormattedName::from_text(Text::from_str("David Wang").unwrap()); 37 | formatted_name.language = Some(Language::from_language_tag(LanguageTag::from_str("en").unwrap())); 38 | 39 | formatted_name 40 | }; 41 | 42 | formatted_names.insert(english_name); 43 | 44 | let chinese_name = { 45 | let mut formatted_name = FormattedName::from_text(Text::from_str("王大衛").unwrap()); 46 | formatted_name.language = Some(Language::from_language_tag(LanguageTag::from_str("zh").unwrap())); 47 | 48 | formatted_name 49 | }; 50 | 51 | formatted_names.insert(chinese_name); 52 | 53 | formatted_names 54 | }; 55 | 56 | let mut vcard = VCard::from_formatted_names(Set::from_hash_set(formatted_names).unwrap()).unwrap(); 57 | 58 | let names = { 59 | let mut names = HashSet::new(); 60 | 61 | let name = { 62 | let name_value = NameValue::from_components( 63 | Some(Component::from_str("Wang").unwrap()), 64 | Some(Component::from_str("David").unwrap()), 65 | None, 66 | Some(Component::from_str("Dr.").unwrap()), 67 | None, 68 | ); 69 | 70 | Name::from_name_value(name_value) 71 | }; 72 | 73 | names.insert(name); 74 | 75 | names 76 | }; 77 | 78 | vcard.names = Some(Set::from_hash_set(names).unwrap()); 79 | 80 | let photos = { 81 | let mut photos = HashSet::new(); 82 | 83 | let photo = { 84 | let image_value = ImageValue::from_file("photo.png").unwrap(); 85 | 86 | Photo::from_image_value(image_value) 87 | }; 88 | 89 | photos.insert(photo); 90 | 91 | photos 92 | }; 93 | 94 | vcard.photos = Some(Set::from_hash_set(photos).unwrap()); 95 | 96 | let birthdays = { 97 | let mut birthdays = HashSet::new(); 98 | 99 | let birthday = { 100 | Birthday::from_date_and_or_time(DateAndOrTime::Date(Date::from_year_month_day(1993, 7, 7).unwrap())) 101 | }; 102 | 103 | birthdays.insert(birthday); 104 | 105 | birthdays 106 | }; 107 | 108 | vcard.birthdays = Some(Set::from_hash_set(birthdays).unwrap()); 109 | 110 | vcard.gender = Some(Gender::from_gender_value(GenderValue::from_sex_type(SexType::Male))); 111 | 112 | let addresses = { 113 | let mut addresses = HashSet::new(); 114 | 115 | let home_address = { 116 | let address_value = AddressValue::from_components( 117 | None, 118 | Some(Component::from_str("No.5").unwrap()), 119 | Some(Component::from_str("Section 5, Xinyi Road, Xinyi District").unwrap()), 120 | Some(Component::from_str("Taipei City").unwrap()), 121 | None, 122 | Some(Component::from_str("110").unwrap()), 123 | Some(Component::from_str("Taiwan").unwrap()), 124 | ); 125 | 126 | let mut address = Address::from_address_value(address_value); 127 | 128 | let type_values = { 129 | let mut type_values = HashSet::new(); 130 | 131 | type_values.insert(TypeValue::Home); 132 | 133 | Set::from_hash_set(type_values).unwrap() 134 | }; 135 | 136 | address.typ = Some(Type::from_type_values(type_values)); 137 | 138 | address 139 | }; 140 | 141 | addresses.insert(home_address); 142 | 143 | let work_address = { 144 | let address_value = AddressValue::from_components( 145 | None, 146 | Some(Component::from_str("No.3").unwrap()), 147 | Some(Component::from_str("Beiping West Road, Zhongzheng District").unwrap()), 148 | Some(Component::from_str("Taipei City").unwrap()), 149 | None, 150 | Some(Component::from_str("100").unwrap()), 151 | Some(Component::from_str("Taiwan").unwrap()), 152 | ); 153 | 154 | let mut address = Address::from_address_value(address_value); 155 | 156 | let type_values = { 157 | let mut type_values = HashSet::new(); 158 | 159 | type_values.insert(TypeValue::Work); 160 | 161 | Set::from_hash_set(type_values).unwrap() 162 | }; 163 | 164 | address.typ = Some(Type::from_type_values(type_values)); 165 | 166 | address 167 | }; 168 | 169 | addresses.insert(work_address); 170 | 171 | addresses 172 | }; 173 | 174 | vcard.addresses = Some(Set::from_hash_set(addresses).unwrap()); 175 | 176 | let telephones = { 177 | let mut telephones = HashSet::new(); 178 | 179 | let home_phone = { 180 | let mut telephone = Telephone::from_telephone_value(TelephoneValue::from_telephone_number_str( 181 | "+886 02 1234 5678", 182 | None::<&str>, 183 | ).unwrap()); 184 | 185 | let type_values = { 186 | let mut type_values = HashSet::new(); 187 | 188 | type_values.insert(TypeValueWithTelephoneType::Home); 189 | type_values.insert(TypeValueWithTelephoneType::Voice); 190 | 191 | Set::from_hash_set(type_values).unwrap() 192 | }; 193 | 194 | if let Telephone::TelephoneValue { ref mut typ, .. } = telephone { 195 | *typ = Some(TypeWithTelType::from_type_values(type_values)); 196 | } 197 | 198 | telephone 199 | }; 200 | 201 | telephones.insert(home_phone); 202 | 203 | let cell_phone = { 204 | let mut telephone = Telephone::from_telephone_value(TelephoneValue::from_telephone_number_str( 205 | "+886 987 654 321", 206 | None::<&str>, 207 | ).unwrap()); 208 | 209 | let type_values = { 210 | let mut type_values = HashSet::new(); 211 | 212 | type_values.insert(TypeValueWithTelephoneType::Cell); 213 | type_values.insert(TypeValueWithTelephoneType::Voice); 214 | 215 | Set::from_hash_set(type_values).unwrap() 216 | }; 217 | 218 | if let Telephone::TelephoneValue { ref mut typ, .. } = telephone { 219 | *typ = Some(TypeWithTelType::from_type_values(type_values)); 220 | } 221 | 222 | telephone 223 | }; 224 | 225 | telephones.insert(cell_phone); 226 | 227 | let work_phone = { 228 | let mut telephone = Telephone::from_telephone_value(TelephoneValue::from_telephone_number_str( 229 | "+886 02 8888 8888", 230 | Some("532"), 231 | ).unwrap()); 232 | 233 | let type_values = { 234 | let mut type_values = HashSet::new(); 235 | 236 | type_values.insert(TypeValueWithTelephoneType::Work); 237 | type_values.insert(TypeValueWithTelephoneType::Voice); 238 | 239 | Set::from_hash_set(type_values).unwrap() 240 | }; 241 | 242 | if let Telephone::TelephoneValue { ref mut typ, .. } = telephone { 243 | *typ = Some(TypeWithTelType::from_type_values(type_values)); 244 | } 245 | 246 | telephone 247 | }; 248 | 249 | telephones.insert(work_phone); 250 | 251 | telephones 252 | }; 253 | 254 | vcard.telephones = Some(Set::from_hash_set(telephones).unwrap()); 255 | 256 | let emails = { 257 | let mut emails = HashSet::new(); 258 | 259 | let personal_email = { 260 | let mut email = Email::from_email_value(EmailValue::from_str("david@gmail.com").unwrap()); 261 | 262 | let type_values = { 263 | let mut type_values = HashSet::new(); 264 | 265 | type_values.insert(TypeValue::Home); 266 | 267 | Set::from_hash_set(type_values).unwrap() 268 | }; 269 | 270 | email.typ = Some(Type::from_type_values(type_values)); 271 | 272 | email 273 | }; 274 | 275 | emails.insert(personal_email); 276 | 277 | let work_email = { 278 | let mut email = Email::from_email_value(EmailValue::from_str("david@thaumaturgiclen.com").unwrap()); 279 | 280 | let type_values = { 281 | let mut type_values = HashSet::new(); 282 | 283 | type_values.insert(TypeValue::Work); 284 | 285 | Set::from_hash_set(type_values).unwrap() 286 | }; 287 | 288 | email.typ = Some(Type::from_type_values(type_values)); 289 | 290 | email 291 | }; 292 | 293 | emails.insert(work_email); 294 | 295 | emails 296 | }; 297 | 298 | vcard.emails = Some(Set::from_hash_set(emails).unwrap()); 299 | 300 | let urls = { 301 | let mut urls = HashSet::new(); 302 | 303 | let company_site = { 304 | let mut url = URL::from_url(url::URL::from_str("https://職員.thaumaturgiclen.com:444/王大衛").unwrap()); 305 | 306 | let type_values = { 307 | let mut type_values = HashSet::new(); 308 | 309 | type_values.insert(TypeValue::Work); 310 | 311 | Set::from_hash_set(type_values).unwrap() 312 | }; 313 | 314 | url.typ = Some(Type::from_type_values(type_values)); 315 | 316 | url 317 | }; 318 | 319 | urls.insert(company_site); 320 | 321 | urls 322 | }; 323 | 324 | vcard.urls = Some(Set::from_hash_set(urls).unwrap()); 325 | 326 | let x_properties = { 327 | let mut x_properties = HashSet::new(); 328 | 329 | let facebook = { 330 | let mut x_socialprofile = XProperty::from_text(XPropertyName::from_str("X-SOCIALPROFILE").unwrap(), Text::from_str("https://www.facebook.com/david.vard.wang").unwrap()); 331 | 332 | let type_values = { 333 | let mut type_values = HashSet::new(); 334 | 335 | type_values.insert(TypeValue::IanaToken(IanaToken::from_str("facebook").unwrap())); 336 | 337 | Set::from_hash_set(type_values).unwrap() 338 | }; 339 | 340 | x_socialprofile.typ = Some(Type::from_type_values(type_values)); 341 | 342 | x_socialprofile 343 | }; 344 | 345 | x_properties.insert(facebook); 346 | 347 | x_properties 348 | }; 349 | 350 | vcard.x_properties = Some(Set::from_hash_set(x_properties).unwrap()); 351 | 352 | // vcard.revision = Some(Revision::now()); // this is the default value. 353 | 354 | vcard.revision = Some(Revision::from_timestamp(Timestamp::from_date_time("2018-11-06T00:00:00Z".parse::>().unwrap()).unwrap())); 355 | 356 | println!("{}", vcard); 357 | 358 | // BEGIN:VCARD 359 | // VERSION:4.0 360 | // FN;LANGUAGE=en:David Wang 361 | // FN;LANGUAGE=zh:王大衛 362 | // N:Wang;David;;Dr.; 363 | // GENDER:M 364 | // BDAY:19930707 365 | // ADR;TYPE=home:;No.5;Section 5\, Xinyi Road\, Xinyi District;Taipei City;;110;Taiwan 366 | // ADR;TYPE=work:;No.3;Beiping West Road\, Zhongzheng District;Taipei City;;100;Taiwan 367 | // TEL;TYPE="voice,cell";VALUE=uri:tel:886-987-654-321 368 | // TEL;TYPE="voice,home";VALUE=uri:tel:886-02-1234-5678 369 | // TEL;TYPE="voice,work";VALUE=uri:tel:886-02-8888-8888;ext=532 370 | // EMAIL;TYPE=work:david@thaumaturgiclen.com 371 | // EMAIL;TYPE=home:david@gmail.com 372 | // PHOTO:data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAAG0OVFdAAAABmJLR0... 373 | // URL;TYPE=work:https://xn--gwr372h.thaumaturgiclen.com:444/%E7%8E%8B%E5%A4%A7%E8%A1%9B 374 | // X-SOCIALPROFILE;TYPE=facebook:https://www.facebook.com/david.vard.wang 375 | // REV:20181106T000000Z 376 | // END:VCARD 377 | ``` 378 | 379 | ## TODO 380 | 381 | 1. Attribute Value (RFC2045) 382 | 1. Language Tag (RFC5646) 383 | 1. Product ID Value (ISO9070 and RFC3406) 384 | 1. Media Type (RFC4288) 385 | 1. VCard Parser 386 | 1. VCard Validator 387 | 1. All versions of VCard (extra 2.1 and 3.0) 388 | 389 | ## Crates.io 390 | 391 | https://crates.io/crates/vcard 392 | 393 | ## Documentation 394 | 395 | https://docs.rs/vcard 396 | 397 | ## License 398 | 399 | [MIT](LICENSE) -------------------------------------------------------------------------------- /examples/data/photo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/magiclen/vcard/47c3535036179ece82afaf6affa83234d80ca6a2/examples/data/photo.png -------------------------------------------------------------------------------- /rustfmt.toml: -------------------------------------------------------------------------------- 1 | # array_width = 60 2 | # attr_fn_like_width = 70 3 | binop_separator = "Front" 4 | blank_lines_lower_bound = 0 5 | blank_lines_upper_bound = 1 6 | brace_style = "PreferSameLine" 7 | # chain_width = 60 8 | color = "Auto" 9 | # comment_width = 100 10 | condense_wildcard_suffixes = true 11 | control_brace_style = "AlwaysSameLine" 12 | empty_item_single_line = true 13 | enum_discrim_align_threshold = 80 14 | error_on_line_overflow = false 15 | error_on_unformatted = false 16 | # fn_call_width = 60 17 | fn_params_layout = "Tall" 18 | fn_single_line = false 19 | force_explicit_abi = true 20 | force_multiline_blocks = false 21 | format_code_in_doc_comments = true 22 | doc_comment_code_block_width = 80 23 | format_generated_files = true 24 | format_macro_matchers = true 25 | format_macro_bodies = true 26 | skip_macro_invocations = [] 27 | format_strings = true 28 | hard_tabs = false 29 | hex_literal_case = "Upper" 30 | imports_indent = "Block" 31 | imports_layout = "Mixed" 32 | indent_style = "Block" 33 | inline_attribute_width = 0 34 | match_arm_blocks = true 35 | match_arm_leading_pipes = "Never" 36 | match_block_trailing_comma = true 37 | max_width = 100 38 | merge_derives = true 39 | imports_granularity = "Crate" 40 | newline_style = "Unix" 41 | normalize_comments = false 42 | normalize_doc_attributes = true 43 | overflow_delimited_expr = true 44 | remove_nested_parens = true 45 | reorder_impl_items = true 46 | reorder_imports = true 47 | group_imports = "StdExternalCrate" 48 | reorder_modules = true 49 | short_array_element_width_threshold = 10 50 | # single_line_if_else_max_width = 50 51 | space_after_colon = true 52 | space_before_colon = false 53 | spaces_around_ranges = false 54 | struct_field_align_threshold = 80 55 | struct_lit_single_line = false 56 | # struct_lit_width = 18 57 | # struct_variant_width = 35 58 | tab_spaces = 4 59 | trailing_comma = "Vertical" 60 | trailing_semicolon = true 61 | type_punctuation_density = "Wide" 62 | use_field_init_shorthand = true 63 | use_small_heuristics = "Max" 64 | use_try_shorthand = true 65 | where_single_line = false 66 | wrap_comments = false -------------------------------------------------------------------------------- /src/escaping/mod.rs: -------------------------------------------------------------------------------- 1 | use std::borrow::Cow; 2 | 3 | use regex::Regex; 4 | 5 | lazy_static! { 6 | static ref NEW_LINE_RE: Regex = Regex::new(r"\n\r|\r\n|\n").unwrap(); 7 | static ref COMMA_RE: Regex = Regex::new(r",").unwrap(); 8 | static ref SEMICOLON_RE: Regex = Regex::new(r";").unwrap(); 9 | static ref BACKSLASH_RE: Regex = Regex::new(r"\\").unwrap(); 10 | static ref TAB_RE: Regex = Regex::new("\x09").unwrap(); 11 | } 12 | 13 | pub(crate) fn escape_new_line(s: &str) -> Cow<'_, str> { 14 | NEW_LINE_RE.replace_all(s, "\\n") 15 | } 16 | 17 | pub(crate) fn escape_tab(s: &str) -> Cow<'_, str> { 18 | TAB_RE.replace_all(s, " ") 19 | } 20 | 21 | pub(crate) fn escape_comma(s: &str) -> Cow<'_, str> { 22 | COMMA_RE.replace_all(s, "\\,") 23 | } 24 | 25 | pub(crate) fn escape_semicolon(s: &str) -> Cow<'_, str> { 26 | SEMICOLON_RE.replace_all(s, "\\;") 27 | } 28 | 29 | pub(crate) fn escape_backslash(s: &str) -> Cow<'_, str> { 30 | BACKSLASH_RE.replace_all(s, "\\\\") 31 | } 32 | -------------------------------------------------------------------------------- /src/parameters/alternative_id.rs: -------------------------------------------------------------------------------- 1 | use std::fmt::Display; 2 | 3 | use validators::{Validated, ValidatedWrapper}; 4 | 5 | use super::{ 6 | super::values::{parameter_value::ParameterValue, Value}, 7 | *, 8 | }; 9 | 10 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] 11 | pub struct AlternativeID { 12 | parameter_value: ParameterValue, 13 | } 14 | 15 | impl AlternativeID { 16 | pub fn from_parameter_value(parameter_value: ParameterValue) -> AlternativeID { 17 | AlternativeID { 18 | parameter_value, 19 | } 20 | } 21 | } 22 | 23 | impl AlternativeID { 24 | pub fn get_parameter_value(&self) -> &ParameterValue { 25 | &self.parameter_value 26 | } 27 | } 28 | 29 | impl Parameter for AlternativeID { 30 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 31 | f.write_str(";ALTID=")?; 32 | 33 | Value::fmt(&self.parameter_value, f)?; 34 | 35 | Ok(()) 36 | } 37 | } 38 | 39 | impl Display for AlternativeID { 40 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 41 | Parameter::fmt(self, f) 42 | } 43 | } 44 | 45 | impl Validated for AlternativeID {} 46 | 47 | impl ValidatedWrapper for AlternativeID { 48 | type Error = &'static str; 49 | 50 | fn from_string(_from_string_input: String) -> Result { 51 | unimplemented!(); 52 | } 53 | 54 | fn from_str(_from_str_input: &str) -> Result { 55 | unimplemented!(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/parameters/any.rs: -------------------------------------------------------------------------------- 1 | use std::{ 2 | fmt::{self, Display, Formatter, Write}, 3 | hash::{Hash, Hasher}, 4 | }; 5 | 6 | use validators::{Validated, ValidatedWrapper}; 7 | 8 | use super::{ 9 | super::{ 10 | values::{parameter_value::ParameterValues, Value}, 11 | IanaToken, Set, XName, 12 | }, 13 | Parameter, 14 | }; 15 | 16 | #[derive(Clone, Debug, Eq)] 17 | pub enum Any { 18 | IanaToken(IanaToken, ParameterValues), 19 | XName(XName, ParameterValues), 20 | } 21 | 22 | impl Any { 23 | pub fn is_empty(&self) -> bool { 24 | match self { 25 | Any::IanaToken(_, v) => v.is_empty(), 26 | Any::XName(_, v) => v.is_empty(), 27 | } 28 | } 29 | } 30 | 31 | impl Parameter for Any { 32 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 33 | if self.is_empty() { 34 | return Ok(()); 35 | } 36 | 37 | f.write_char(';')?; 38 | 39 | let set = match self { 40 | Any::IanaToken(a, b) => { 41 | f.write_str(a.as_str())?; 42 | b 43 | }, 44 | Any::XName(a, b) => { 45 | f.write_str(a.as_str())?; 46 | b 47 | }, 48 | }; 49 | 50 | f.write_char('=')?; 51 | 52 | Value::fmt(set, f)?; 53 | 54 | Ok(()) 55 | } 56 | } 57 | 58 | impl Parameter for Set { 59 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 60 | for e in self.as_hash_set() { 61 | Parameter::fmt(e, f)?; 62 | } 63 | 64 | Ok(()) 65 | } 66 | } 67 | 68 | impl Value for Any { 69 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 70 | Parameter::fmt(self, f) 71 | } 72 | } 73 | 74 | impl Display for Any { 75 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 76 | Parameter::fmt(self, f) 77 | } 78 | } 79 | 80 | impl PartialEq for Any { 81 | #[inline] 82 | fn eq(&self, other: &Any) -> bool { 83 | match self { 84 | Any::IanaToken(a, b) => match other { 85 | Any::IanaToken(aa, bb) => a == aa && b == bb, 86 | _ => false, 87 | }, 88 | Any::XName(a, b) => match other { 89 | Any::XName(aa, bb) => a == aa && b == bb, 90 | _ => false, 91 | }, 92 | } 93 | } 94 | } 95 | 96 | impl Hash for Any { 97 | fn hash(&self, state: &mut H) { 98 | match self { 99 | Any::XName(a, b) => { 100 | state.write(a.as_str().as_bytes()); 101 | b.hash(state); 102 | }, 103 | Any::IanaToken(a, b) => { 104 | state.write(a.as_str().as_bytes()); 105 | b.hash(state); 106 | }, 107 | } 108 | } 109 | } 110 | 111 | impl Validated for Any {} 112 | 113 | impl ValidatedWrapper for Any { 114 | type Error = &'static str; 115 | 116 | fn from_string(_from_string_input: String) -> Result { 117 | unimplemented!(); 118 | } 119 | 120 | fn from_str(_from_str_input: &str) -> Result { 121 | unimplemented!(); 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /src/parameters/calscale.rs: -------------------------------------------------------------------------------- 1 | use std::fmt::{self, Display, Formatter}; 2 | 3 | use validators::{Validated, ValidatedWrapper}; 4 | 5 | use super::{ 6 | super::values::{calscale_value::CalscaleValue, Value}, 7 | Parameter, 8 | }; 9 | 10 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] 11 | pub struct Calscale { 12 | calscale_value: CalscaleValue, 13 | } 14 | 15 | impl Calscale { 16 | pub fn from_calscale_value(calscale_value: CalscaleValue) -> Calscale { 17 | Calscale { 18 | calscale_value, 19 | } 20 | } 21 | } 22 | 23 | impl Calscale { 24 | pub fn get_value_type(&self) -> &CalscaleValue { 25 | &self.calscale_value 26 | } 27 | } 28 | 29 | impl Parameter for Calscale { 30 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 31 | f.write_str(";CALSCALE=")?; 32 | 33 | Value::fmt(&self.calscale_value, f)?; 34 | 35 | Ok(()) 36 | } 37 | } 38 | 39 | impl Display for Calscale { 40 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 41 | Parameter::fmt(self, f) 42 | } 43 | } 44 | 45 | impl Validated for Calscale {} 46 | 47 | impl ValidatedWrapper for Calscale { 48 | type Error = &'static str; 49 | 50 | fn from_string(_from_string_input: String) -> Result { 51 | unimplemented!(); 52 | } 53 | 54 | fn from_str(_from_str_input: &str) -> Result { 55 | unimplemented!(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/parameters/geo.rs: -------------------------------------------------------------------------------- 1 | use std::fmt::{Display, Write}; 2 | 3 | use validators::{Validated, ValidatedWrapper}; 4 | 5 | use super::{ 6 | super::values::{geo_value::GeoValue, Value}, 7 | *, 8 | }; 9 | 10 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] 11 | pub struct Geo { 12 | geo_value: GeoValue, 13 | } 14 | 15 | impl Geo { 16 | pub fn from_geo_value(geo_value: GeoValue) -> Geo { 17 | Geo { 18 | geo_value, 19 | } 20 | } 21 | } 22 | 23 | impl Geo { 24 | pub fn get_geo_value(&self) -> &GeoValue { 25 | &self.geo_value 26 | } 27 | } 28 | 29 | impl Parameter for Geo { 30 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 31 | f.write_str(";GEO=\"")?; 32 | 33 | Value::fmt(&self.geo_value, f)?; 34 | 35 | f.write_char('\"')?; 36 | 37 | Ok(()) 38 | } 39 | } 40 | 41 | impl Display for Geo { 42 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 43 | Parameter::fmt(self, f) 44 | } 45 | } 46 | 47 | impl Validated for Geo {} 48 | 49 | impl ValidatedWrapper for Geo { 50 | type Error = &'static str; 51 | 52 | fn from_string(_from_string_input: String) -> Result { 53 | unimplemented!(); 54 | } 55 | 56 | fn from_str(_from_str_input: &str) -> Result { 57 | unimplemented!(); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/parameters/label.rs: -------------------------------------------------------------------------------- 1 | use std::fmt::Display; 2 | 3 | use validators::{Validated, ValidatedWrapper}; 4 | 5 | use super::{ 6 | super::values::{parameter_value::ParameterValue, Value}, 7 | *, 8 | }; 9 | 10 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] 11 | pub struct Label { 12 | parameter_value: ParameterValue, 13 | } 14 | 15 | impl Label { 16 | pub fn from_parameter_value(parameter_value: ParameterValue) -> Label { 17 | Label { 18 | parameter_value, 19 | } 20 | } 21 | 22 | pub fn is_empty(&self) -> bool { 23 | self.parameter_value.is_empty() 24 | } 25 | } 26 | 27 | impl Label { 28 | pub fn get_parameter_value(&self) -> &ParameterValue { 29 | &self.parameter_value 30 | } 31 | } 32 | 33 | impl Parameter for Label { 34 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 35 | if self.is_empty() { 36 | return Ok(()); 37 | } 38 | 39 | f.write_str(";LABEL=")?; 40 | 41 | Value::fmt(&self.parameter_value, f)?; 42 | 43 | Ok(()) 44 | } 45 | } 46 | 47 | impl Display for Label { 48 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 49 | Parameter::fmt(self, f) 50 | } 51 | } 52 | 53 | impl Validated for Label {} 54 | 55 | impl ValidatedWrapper for Label { 56 | type Error = &'static str; 57 | 58 | fn from_string(_from_string_input: String) -> Result { 59 | unimplemented!(); 60 | } 61 | 62 | fn from_str(_from_str_input: &str) -> Result { 63 | unimplemented!(); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/parameters/language.rs: -------------------------------------------------------------------------------- 1 | use std::fmt::Display; 2 | 3 | use validators::{Validated, ValidatedWrapper}; 4 | 5 | use super::{super::values::language_tag::LanguageTag, *}; 6 | 7 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] 8 | pub struct Language { 9 | language_tag: LanguageTag, 10 | } 11 | 12 | impl Language { 13 | pub fn from_language_tag(language_tag: LanguageTag) -> Language { 14 | Language { 15 | language_tag, 16 | } 17 | } 18 | } 19 | 20 | impl Language { 21 | pub fn get_language_tag(&self) -> &LanguageTag { 22 | &self.language_tag 23 | } 24 | } 25 | 26 | impl Parameter for Language { 27 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 28 | f.write_str(";LANGUAGE=")?; 29 | f.write_str(self.language_tag.as_str())?; 30 | 31 | Ok(()) 32 | } 33 | } 34 | 35 | impl Display for Language { 36 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 37 | Parameter::fmt(self, f) 38 | } 39 | } 40 | 41 | impl Validated for Language {} 42 | 43 | impl ValidatedWrapper for Language { 44 | type Error = &'static str; 45 | 46 | fn from_string(_from_string_input: String) -> Result { 47 | unimplemented!(); 48 | } 49 | 50 | fn from_str(_from_str_input: &str) -> Result { 51 | unimplemented!(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/parameters/media_type.rs: -------------------------------------------------------------------------------- 1 | use std::fmt::{Display, Write}; 2 | 3 | use regex::Regex; 4 | use validators::{Validated, ValidatedCustomizedStringError, ValidatedWrapper}; 5 | 6 | use super::{ 7 | super::{ 8 | values::{attribute_value::AttributeValue, Value}, 9 | Set, 10 | }, 11 | *, 12 | }; 13 | use crate::PATH_PERCENT_ENCODE_SET; 14 | 15 | // TODO not implement yet, refer to [RFC4288] 16 | 17 | lazy_static! { 18 | static ref MEDIA_TYPE_SEGMENT_RE: Regex = 19 | Regex::new(r"^[^\x00-\x1F\x22\x3A\x3B\x7F]+$").unwrap(); 20 | } 21 | 22 | validated_customized_regex_string!(pub MediaTypeSegment, ref MEDIA_TYPE_SEGMENT_RE); 23 | 24 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] 25 | pub struct MediaType { 26 | type_name: MediaTypeSegment, 27 | subtype_name: MediaTypeSegment, 28 | attribute_values: Option>, 29 | } 30 | 31 | impl MediaType { 32 | pub fn from_str( 33 | type_name: &str, 34 | subtype_name: &str, 35 | attribute_values: Option>, 36 | ) -> Result { 37 | Ok(Self::from_media_type_segments( 38 | MediaTypeSegment::from_str(type_name)?, 39 | MediaTypeSegment::from_str(subtype_name)?, 40 | attribute_values, 41 | )) 42 | } 43 | 44 | pub fn from_string( 45 | type_name: String, 46 | subtype_name: String, 47 | attribute_values: Option>, 48 | ) -> Result { 49 | Ok(Self::from_media_type_segments( 50 | MediaTypeSegment::from_string(type_name)?, 51 | MediaTypeSegment::from_string(subtype_name)?, 52 | attribute_values, 53 | )) 54 | } 55 | 56 | pub fn from_media_type_segments( 57 | type_name: MediaTypeSegment, 58 | subtype_name: MediaTypeSegment, 59 | attribute_values: Option>, 60 | ) -> MediaType { 61 | MediaType { 62 | type_name, 63 | subtype_name, 64 | attribute_values, 65 | } 66 | } 67 | } 68 | 69 | impl MediaType { 70 | pub fn get_type_name(&self) -> &MediaTypeSegment { 71 | &self.type_name 72 | } 73 | 74 | pub fn get_subtype_name(&self) -> &MediaTypeSegment { 75 | &self.subtype_name 76 | } 77 | 78 | pub fn get_attribute_values(&self) -> Option<&Set> { 79 | self.attribute_values.as_ref() 80 | } 81 | } 82 | 83 | impl Parameter for MediaType { 84 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 85 | f.write_str(";MEDIATYPE=")?; 86 | 87 | f.write_str( 88 | &percent_encoding::utf8_percent_encode( 89 | self.type_name.as_str(), 90 | PATH_PERCENT_ENCODE_SET, 91 | ) 92 | .to_string(), 93 | )?; 94 | 95 | f.write_char('/')?; 96 | 97 | f.write_str( 98 | &percent_encoding::utf8_percent_encode( 99 | self.subtype_name.as_str(), 100 | PATH_PERCENT_ENCODE_SET, 101 | ) 102 | .to_string(), 103 | )?; 104 | 105 | if let Some(attribute_values) = &self.attribute_values { 106 | Value::fmt(attribute_values, f)?; 107 | } 108 | 109 | Ok(()) 110 | } 111 | } 112 | 113 | impl Display for MediaType { 114 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 115 | Parameter::fmt(self, f) 116 | } 117 | } 118 | 119 | impl Validated for MediaType {} 120 | 121 | impl ValidatedWrapper for MediaType { 122 | type Error = &'static str; 123 | 124 | fn from_string(_from_string_input: String) -> Result { 125 | unimplemented!(); 126 | } 127 | 128 | fn from_str(_from_str_input: &str) -> Result { 129 | unimplemented!(); 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /src/parameters/mod.rs: -------------------------------------------------------------------------------- 1 | use std::fmt::{self, Formatter}; 2 | 3 | pub mod alternative_id; 4 | pub mod any; 5 | pub mod calscale; 6 | pub mod geo; 7 | pub mod label; 8 | pub mod language; 9 | pub mod media_type; 10 | pub mod preference; 11 | pub mod property_id; 12 | pub mod sort_as; 13 | pub mod time_zone; 14 | pub mod typ; 15 | pub mod value; 16 | 17 | pub trait Parameter { 18 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error>; 19 | } 20 | -------------------------------------------------------------------------------- /src/parameters/preference.rs: -------------------------------------------------------------------------------- 1 | use std::fmt::Display; 2 | 3 | use validators::{Validated, ValidatedWrapper}; 4 | 5 | use super::{ 6 | super::values::{preference_value::PreferenceValue, Value}, 7 | *, 8 | }; 9 | 10 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] 11 | pub struct Preference { 12 | preference_value: PreferenceValue, 13 | } 14 | 15 | impl Preference { 16 | pub fn from_preference_value(preference_value: PreferenceValue) -> Preference { 17 | Preference { 18 | preference_value, 19 | } 20 | } 21 | } 22 | 23 | impl Preference { 24 | pub fn get_preference_value(&self) -> &PreferenceValue { 25 | &self.preference_value 26 | } 27 | } 28 | 29 | impl Parameter for Preference { 30 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 31 | f.write_str(";PREF=")?; 32 | 33 | Value::fmt(&self.preference_value, f)?; 34 | 35 | Ok(()) 36 | } 37 | } 38 | 39 | impl Display for Preference { 40 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 41 | Parameter::fmt(self, f) 42 | } 43 | } 44 | 45 | impl Validated for Preference {} 46 | 47 | impl ValidatedWrapper for Preference { 48 | type Error = &'static str; 49 | 50 | fn from_string(_from_string_input: String) -> Result { 51 | unimplemented!(); 52 | } 53 | 54 | fn from_str(_from_str_input: &str) -> Result { 55 | unimplemented!(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/parameters/property_id.rs: -------------------------------------------------------------------------------- 1 | use std::fmt::Display; 2 | 3 | use validators::{Validated, ValidatedWrapper}; 4 | 5 | use super::{ 6 | super::{ 7 | values::{property_id_value::PropertyIDValue, Value}, 8 | Set, 9 | }, 10 | *, 11 | }; 12 | 13 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] 14 | pub struct PropertyID { 15 | ids: Set, 16 | } 17 | 18 | impl PropertyID { 19 | pub fn from_ids(ids: Set) -> PropertyID { 20 | PropertyID { 21 | ids, 22 | } 23 | } 24 | } 25 | 26 | impl PropertyID { 27 | pub fn get_ids(&self) -> &Set { 28 | &self.ids 29 | } 30 | } 31 | 32 | impl Parameter for PropertyID { 33 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 34 | f.write_str(";PID=")?; 35 | 36 | Value::fmt(&self.ids, f)?; 37 | 38 | Ok(()) 39 | } 40 | } 41 | 42 | impl Display for PropertyID { 43 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 44 | Parameter::fmt(self, f) 45 | } 46 | } 47 | 48 | impl Validated for PropertyID {} 49 | 50 | impl ValidatedWrapper for PropertyID { 51 | type Error = &'static str; 52 | 53 | fn from_string(_from_string_input: String) -> Result { 54 | unimplemented!(); 55 | } 56 | 57 | fn from_str(_from_str_input: &str) -> Result { 58 | unimplemented!(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/parameters/sort_as.rs: -------------------------------------------------------------------------------- 1 | use std::fmt::Display; 2 | 3 | use validators::{Validated, ValidatedWrapper}; 4 | 5 | use super::{ 6 | super::values::{parameter_value::ParameterValues, Value}, 7 | *, 8 | }; 9 | 10 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] 11 | pub struct SortAs { 12 | parameter_values: ParameterValues, 13 | } 14 | 15 | impl SortAs { 16 | pub fn from_parameter_values(parameter_values: ParameterValues) -> SortAs { 17 | SortAs { 18 | parameter_values, 19 | } 20 | } 21 | 22 | pub fn is_empty(&self) -> bool { 23 | self.parameter_values.is_empty() 24 | } 25 | } 26 | 27 | impl SortAs { 28 | pub fn get_parameter_values(&self) -> &ParameterValues { 29 | &self.parameter_values 30 | } 31 | } 32 | 33 | impl Parameter for SortAs { 34 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 35 | f.write_str(";SORT-AS=")?; 36 | 37 | Value::fmt(&self.parameter_values, f)?; 38 | 39 | Ok(()) 40 | } 41 | } 42 | 43 | impl Display for SortAs { 44 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 45 | Parameter::fmt(self, f) 46 | } 47 | } 48 | 49 | impl Validated for SortAs {} 50 | 51 | impl ValidatedWrapper for SortAs { 52 | type Error = &'static str; 53 | 54 | fn from_string(_from_string_input: String) -> Result { 55 | unimplemented!(); 56 | } 57 | 58 | fn from_str(_from_str_input: &str) -> Result { 59 | unimplemented!(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/parameters/time_zone.rs: -------------------------------------------------------------------------------- 1 | use std::fmt::{Display, Write}; 2 | 3 | use validators::{Validated, ValidatedWrapper}; 4 | 5 | use super::{ 6 | super::values::{time_zone_value::TimeZoneValue, Value}, 7 | *, 8 | }; 9 | 10 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] 11 | pub struct TimeZone { 12 | time_zone_value: TimeZoneValue, 13 | } 14 | 15 | impl TimeZone { 16 | pub fn from_time_zone_value(time_zone_value: TimeZoneValue) -> TimeZone { 17 | TimeZone { 18 | time_zone_value, 19 | } 20 | } 21 | } 22 | 23 | impl Parameter for TimeZone { 24 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 25 | f.write_str(";TZ=")?; 26 | 27 | match &self.time_zone_value { 28 | TimeZoneValue::Tz(_) => { 29 | Value::fmt(&self.time_zone_value, f)?; 30 | }, 31 | TimeZoneValue::URI(_) => { 32 | f.write_char('\"')?; 33 | Value::fmt(&self.time_zone_value, f)?; 34 | f.write_char('\"')?; 35 | }, 36 | } 37 | 38 | Ok(()) 39 | } 40 | } 41 | 42 | impl Display for TimeZone { 43 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 44 | Parameter::fmt(self, f) 45 | } 46 | } 47 | 48 | impl Validated for TimeZone {} 49 | 50 | impl ValidatedWrapper for TimeZone { 51 | type Error = &'static str; 52 | 53 | fn from_string(_from_string_input: String) -> Result { 54 | unimplemented!(); 55 | } 56 | 57 | fn from_str(_from_str_input: &str) -> Result { 58 | unimplemented!(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/parameters/typ.rs: -------------------------------------------------------------------------------- 1 | use std::fmt::{Display, Write}; 2 | 3 | use validators::{Validated, ValidatedWrapper}; 4 | 5 | use super::{ 6 | super::{ 7 | values::{ 8 | type_value::{TypeValue, TypeValueWithRelatedType, TypeValueWithTelephoneType}, 9 | Value, 10 | }, 11 | Set, 12 | }, 13 | *, 14 | }; 15 | 16 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] 17 | pub struct Type { 18 | type_values: Set, 19 | } 20 | 21 | impl Type { 22 | pub fn from_type_values(type_values: Set) -> Type { 23 | Type { 24 | type_values, 25 | } 26 | } 27 | } 28 | 29 | impl Type { 30 | pub fn get_type_values(&self) -> &Set { 31 | &self.type_values 32 | } 33 | } 34 | 35 | impl Parameter for Type { 36 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 37 | f.write_str(";TYPE=")?; 38 | 39 | let has_double_quote = self.type_values.as_hash_set().len() > 1; 40 | 41 | if has_double_quote { 42 | f.write_char('\"')?; 43 | } 44 | 45 | Value::fmt(&self.type_values, f)?; 46 | 47 | if has_double_quote { 48 | f.write_char('\"')?; 49 | } 50 | 51 | Ok(()) 52 | } 53 | } 54 | 55 | impl Display for Type { 56 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 57 | Parameter::fmt(self, f) 58 | } 59 | } 60 | 61 | impl Validated for Type {} 62 | 63 | impl ValidatedWrapper for Type { 64 | type Error = &'static str; 65 | 66 | fn from_string(_from_string_input: String) -> Result { 67 | unimplemented!(); 68 | } 69 | 70 | fn from_str(_from_str_input: &str) -> Result { 71 | unimplemented!(); 72 | } 73 | } 74 | 75 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] 76 | pub struct TypeWithTelType { 77 | type_values: Set, 78 | } 79 | 80 | impl TypeWithTelType { 81 | pub fn from_type_values(type_values: Set) -> TypeWithTelType { 82 | TypeWithTelType { 83 | type_values, 84 | } 85 | } 86 | } 87 | 88 | impl TypeWithTelType { 89 | pub fn get_ids(&self) -> &Set { 90 | &self.type_values 91 | } 92 | } 93 | 94 | impl Parameter for TypeWithTelType { 95 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 96 | f.write_str(";TYPE=")?; 97 | 98 | let has_double_quote = self.type_values.as_hash_set().len() > 1; 99 | 100 | if has_double_quote { 101 | f.write_char('\"')?; 102 | } 103 | 104 | Value::fmt(&self.type_values, f)?; 105 | 106 | if has_double_quote { 107 | f.write_char('\"')?; 108 | } 109 | 110 | Ok(()) 111 | } 112 | } 113 | 114 | impl Display for TypeWithTelType { 115 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 116 | Parameter::fmt(self, f) 117 | } 118 | } 119 | 120 | impl Validated for TypeWithTelType {} 121 | 122 | impl ValidatedWrapper for TypeWithTelType { 123 | type Error = &'static str; 124 | 125 | fn from_string(_from_string_input: String) -> Result { 126 | unimplemented!(); 127 | } 128 | 129 | fn from_str(_from_str_input: &str) -> Result { 130 | unimplemented!(); 131 | } 132 | } 133 | 134 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] 135 | pub struct TypeWithRelatedType { 136 | type_values: Set, 137 | } 138 | 139 | impl TypeWithRelatedType { 140 | pub fn from_type_values(type_values: Set) -> TypeWithRelatedType { 141 | TypeWithRelatedType { 142 | type_values, 143 | } 144 | } 145 | } 146 | 147 | impl TypeWithRelatedType { 148 | pub fn get_type_values(&self) -> &Set { 149 | &self.type_values 150 | } 151 | } 152 | 153 | impl Parameter for TypeWithRelatedType { 154 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 155 | f.write_str(";TYPE=")?; 156 | 157 | let has_double_quote = self.type_values.as_hash_set().len() > 1; 158 | 159 | if has_double_quote { 160 | f.write_char('\"')?; 161 | } 162 | 163 | Value::fmt(&self.type_values, f)?; 164 | 165 | if has_double_quote { 166 | f.write_char('\"')?; 167 | } 168 | 169 | Ok(()) 170 | } 171 | } 172 | 173 | impl Display for TypeWithRelatedType { 174 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 175 | Parameter::fmt(self, f) 176 | } 177 | } 178 | 179 | impl Validated for TypeWithRelatedType {} 180 | 181 | impl ValidatedWrapper for TypeWithRelatedType { 182 | type Error = &'static str; 183 | 184 | fn from_string(_from_string_input: String) -> Result { 185 | unimplemented!(); 186 | } 187 | 188 | fn from_str(_from_str_input: &str) -> Result { 189 | unimplemented!(); 190 | } 191 | } 192 | -------------------------------------------------------------------------------- /src/parameters/value.rs: -------------------------------------------------------------------------------- 1 | use std::fmt::{self, Display, Formatter}; 2 | 3 | use validators::{Validated, ValidatedWrapper}; 4 | 5 | use super::{ 6 | super::{values, values::value_type::ValueType}, 7 | Parameter, 8 | }; 9 | 10 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] 11 | pub struct Value { 12 | value_type: ValueType, 13 | } 14 | 15 | impl Value { 16 | pub fn from_value_type(value_type: ValueType) -> Value { 17 | Value { 18 | value_type, 19 | } 20 | } 21 | } 22 | 23 | impl Value { 24 | pub fn get_value_type(&self) -> &ValueType { 25 | &self.value_type 26 | } 27 | } 28 | 29 | impl Parameter for Value { 30 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 31 | f.write_str(";VALUE=")?; 32 | 33 | values::Value::fmt(&self.value_type, f)?; 34 | 35 | Ok(()) 36 | } 37 | } 38 | 39 | impl Display for Value { 40 | fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { 41 | Parameter::fmt(self, f) 42 | } 43 | } 44 | 45 | impl Validated for Value {} 46 | 47 | impl ValidatedWrapper for Value { 48 | type Error = &'static str; 49 | 50 | fn from_string(_from_string_input: String) -> Result { 51 | unimplemented!(); 52 | } 53 | 54 | fn from_str(_from_str_input: &str) -> Result { 55 | unimplemented!(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/properties/address.rs: -------------------------------------------------------------------------------- 1 | use std::fmt::{self, Display, Formatter, Write}; 2 | 3 | use validators::{Validated, ValidatedWrapper}; 4 | 5 | use super::{ 6 | super::{ 7 | parameters::{ 8 | alternative_id::AlternativeID, any::Any, geo::Geo, label::Label, language::Language, 9 | preference::Preference, property_id::PropertyID, time_zone::TimeZone, typ::Type, 10 | Parameter, 11 | }, 12 | values::{address_value::AddressValue, Value}, 13 | Set, 14 | }, 15 | *, 16 | }; 17 | 18 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] 19 | pub struct Address { 20 | pub label: Option