jid-0.12.1/.cargo_vcs_info.json0000644000000001410000000000100116570ustar { "git": { "sha1": "239a019174d6d57e858ad04b87eb0b09781ac2ba" }, "path_in_vcs": "jid" }jid-0.12.1/CHANGELOG.md000064400000000000000000000165311046102023000122720ustar 00000000000000Version NEXT: Version 0.12.1, release 2025-11-02: * Changes: - The 'quote' feature now uses `jid::Jid` instead of `::jid::Jid` to stop requiring importing the module as a dependency of the project. The `jid` module just needs to be made available, for example: `use foo::jid;` - Bump minidom to 0.18 Version 0.12.0, release 2025-01-25: * Breaking: - domainparts are now checked much more in-depth, using the idna crate for domains and `Ipv*Addr` checks for IP domainparts. This removes some prior errors which are now covered by `Error::Idna`: `Error::DomainEmpty` and `Error::DomainTooLong` * Additions: - xso::FromXmlText and xso::AsXmlText are now implemented for NodePart, DomainPart, and ResourcePart (!485) - serde::Serialize and serde::Deserialize are now derived for NodePart, DomainPart, and ResourcePart when serde feature flag is enabled (!499) - This crate is now entirely `no_std`, with no remaining dependency on std! - When using the crate from a cloned git repository, the minidom dependency is now relative too, which simplifies working on a different project while making modifications to this workspace. - Make Debug more readable, just `Jid("foo@bar/baz")` instead of exposing the internally-cached position of the '@' and '/' signs. Version 0.11.1, release 2024-07-23: * Breaking: - Move InnerJid into Jid and reformulate BareJid and FullJid in terms of Jid. - With the addition of `str`-like types for `DomainPart`, `NodePart` and `ResourcePart`, the functions on `Jid`, `BareJid` and `FullJid` which return the respective types have been changed to return references instead. Use `ToOwned::to_owned` to obtain a copy or `.as_str()` to obtain a plain `str` reference. - The `node_str`, `domain_str` and `resource_str` functions returning str references have been removed from the JID types. Use `.as_str()` or `.map(|x| x.as_str())` on the corresponding `node`/`domain`/`resource` functions instead. * Additions: - Add optional quote support. Implement quote::ToTokens for Jid, FullJid and BareJid. - `str`-like reference types have been added for `DomainPart`, `NodePart` and `ResourcePart`, called `DomainRef`, `NodeRef` and `ResourceRef` respectively. - Convenience methods to combine `DomainPart` and `NodePart` to a `BareJid` have been added, including `impl From for BareJid` and `impl From for Jid`, both of which are (unlike `::from_parts`) copy-free. - `as_str` methods have been added on all Jid types. - Derive PartialOrd/Ord for Jid types. - Implement `Borrow` on `FullJid` and `BareJid`. - Make the crate no_std - Update to edition 2021. - Use debug_tuple instead of string formatting to get standard-like output. - Improve perfs of comparison operators (#123) - Add more test cases - Fix clippy lints, cargo doc, and other warnings Version 0.11.0, release 2024-07-23 [YANKED] Version 0.10.0, release 2023-08-17: * Breaking - serde: Jid is now using untagged enum representation (#66) - JidParseError has been renamed Error - Jid::new, FullJid::new, and BareJid::new now take a single stringy argument and return a Result ; to build JIDs from separate parts, use the from_parts() method instead (#204) * Additions - Parsing invalid JIDs with stringprep feature no longer results in panic, returning Error with NodePrep, NamePrep or ResourcePrep variant instead (#84) - Parsing already-normalized JIDs with stringprep is much faster, about 20 times. - JID parts are now typed as NodePart, DomainPart and ResourcePart ; once part into those types, JID operations cannot fail - BareJid::with_resource appends a ResourcePart to a BareJid to produce a FullJid (#204) - BareJid::with_resource_str appends a stringy resource to a BareJid to produce a FullJid, and can fail in case of resource stringprep or length check error (#204) - Jid::from_parts, BareJid::from_parts, and FullJid::from_parts enable to build JIDs from typed Parts and cannot fail (#204) - Jid::node(), BareJid::node(), and FullJid::node() now return an option of the typed NodePart ; the node_str() method returns the same information as a string slice (#204) - Jid::domain(), BareJid::domain(), and FullJid::domain() now return the typed DomainPart ; the domain_str() method returns the same information as a string slice (#204) - FullJid::resource() returns the typed ResourcePart ; the resource_str() method returns the same information as a string slice (#204) - Jid::resource() returns an optional typed ResourcePart ; the resource_str() method returns the same information as a string slice (#204) - Add serde_test in tests to ensure correctness of Serialize / Deserialize implementations. Version 0.9.3, release 2022-03-07: * Updates - Bumped minidom to 0.14 Version 0.9.2, release 2021-01-13: * Updates - Bumped minidom to 0.13 Version 0.9.1, release 2021-01-13: * Updates - Added serde support behind "serde" feature - Added equality operators between Jid, BareJid and FullJid. Version 0.9.0, release 2020-02-15: * Breaking - Update minidom dependency to 0.12 Version 0.8, released 2019-10-15: * Updates - CI: Split jobs, add tests, and caching * Breaking - 0.7.1 was actually a breaking release Version 0.7.2, released 2019-09-13: * Updates - Impl Error for JidParseError again, it got removed due to the failure removal but is still wanted. Version 0.7.1, released 2019-09-06: * Updates - Remove failure dependency, to keep compilation times in check - Impl Display for Jid Version 0.7.0, released 2019-07-26: * Breaking - Update minidom dependency to 0.11 Version 0.6.2, released 2019-07-20: * Updates - Implement From and From for Jid - Add node and domain getters on Jid Version 0.6.1, released 2019-06-10: * Updates - Change the license from LGPLv3 to MPL-2.0. Version 0.6.0, released 2019-06-10: * Updates - Jid is now an enum, with two variants, Bare(BareJid) and Full(FullJid). - BareJid and FullJid are two specialised variants of a JID. Version 0.5.3, released 2019-01-16: * Updates - Link Mauve bumped the minidom dependency version. - Use Edition 2018, putting the baseline rustc version to 1.31. - Run cargo-fmt on the code, to lower the barrier of entry. Version 0.5.2, released 2018-07-31: * Updates - Astro bumped the minidom dependency version. - Updated the changelog to reflect that 0.5.1 was never actually released. Version 0.5.1, "released" 2018-03-01: * Updates - Link Mauve implemented failure::Fail on JidParseError. - Link Mauve simplified the code a bit. Version 0.5.0, released 2018-02-18: * Updates - Link Mauve has updated the optional `minidom` dependency. - Link Mauve has added tests for invalid JIDs, which adds more error cases. Version 0.4.0, released 2017-12-27: * Updates - Maxime Buquet has updated the optional `minidom` dependency. - The repository has been transferred to xmpp-rs/jid-rs. Version 0.3.1, released 2017-10-31: * Additions - Link Mauve added a minidom::IntoElements implementation on Jid behind the "minidom" feature. ( https://gitlab.com/lumi/jid-rs/merge_requests/9 ) jid-0.12.1/Cargo.lock0000644000000311000000000000100076310ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 4 [[package]] name = "bytes" version = "1.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a" [[package]] name = "castaway" version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dec551ab6e7578819132c713a93c022a05d60159dc86e7a7050223577484c55a" dependencies = [ "rustversion", ] [[package]] name = "cfg-if" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" [[package]] name = "compact_str" version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f86b9c4c00838774a6d902ef931eff7470720c51d90c2e32cfe15dc304737b3f" dependencies = [ "castaway", "cfg-if", "itoa", "ryu", "static_assertions", ] [[package]] name = "displaydoc" version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "futures-core" version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" [[package]] name = "icu_collections" version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c6b649701667bbe825c3b7e6388cb521c23d88644678e83c0c4d0a621a34b43" dependencies = [ "displaydoc", "potential_utf", "yoke", "zerofrom", "zerovec", ] [[package]] name = "icu_locale_core" version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "edba7861004dd3714265b4db54a3c390e880ab658fec5f7db895fae2046b5bb6" dependencies = [ "displaydoc", "litemap", "tinystr", "writeable", "zerovec", ] [[package]] name = "icu_normalizer" version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f6c8828b67bf8908d82127b2054ea1b4427ff0230ee9141c54251934ab1b599" dependencies = [ "icu_collections", "icu_normalizer_data", "icu_properties", "icu_provider", "smallvec", "zerovec", ] [[package]] name = "icu_normalizer_data" version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7aedcccd01fc5fe81e6b489c15b247b8b0690feb23304303a9e560f37efc560a" [[package]] name = "icu_properties" version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e93fcd3157766c0c8da2f8cff6ce651a31f0810eaa1c51ec363ef790bbb5fb99" dependencies = [ "icu_collections", "icu_locale_core", "icu_properties_data", "icu_provider", "zerotrie", "zerovec", ] [[package]] name = "icu_properties_data" version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "02845b3647bb045f1100ecd6480ff52f34c35f82d9880e029d329c21d1054899" [[package]] name = "icu_provider" version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "85962cf0ce02e1e0a629cc34e7ca3e373ce20dda4c4d7294bbd0bf1fdb59e614" dependencies = [ "displaydoc", "icu_locale_core", "writeable", "yoke", "zerofrom", "zerotrie", "zerovec", ] [[package]] name = "idna" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de" dependencies = [ "idna_adapter", "smallvec", "utf8_iter", ] [[package]] name = "idna_adapter" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3acae9609540aa318d1bc588455225fb2085b9ed0c4f6bd0d9d5bcd86f1a0344" dependencies = [ "icu_normalizer", "icu_properties", ] [[package]] name = "itoa" version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" [[package]] name = "jid" version = "0.12.1" dependencies = [ "idna", "memchr", "minidom", "proc-macro2", "quote", "serde", "serde_test", "stringprep", ] [[package]] name = "litemap" version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6373607a59f0be73a39b6fe456b8192fcc3585f602af20751600e974dd455e77" [[package]] name = "memchr" version = "2.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" [[package]] name = "minidom" version = "0.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "040b4e38f8abd85ddf9ea00646accc716b9d15e283b0aacb16f697ddba432c97" dependencies = [ "rxml", "thiserror", ] [[package]] name = "potential_utf" version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b73949432f5e2a09657003c25bca5e19a0e9c84f8058ca374f49e0ebe605af77" dependencies = [ "zerovec", ] [[package]] name = "proc-macro2" version = "1.0.103" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8" dependencies = [ "unicode-ident", ] [[package]] name = "quote" version = "1.0.41" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce25767e7b499d1b604768e7cde645d14cc8584231ea6b295e9c9eb22c02e1d1" dependencies = [ "proc-macro2", ] [[package]] name = "rustversion" version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" [[package]] name = "rxml" version = "0.13.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f288457ad3607c08953ca5604229ebb03878a0b4491d8f545d547281c2b3e0c5" dependencies = [ "bytes", "futures-core", "rxml_validation", ] [[package]] name = "rxml_validation" version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "826e80413b9a35e9d33217b3dcac04cf95f6559d15944b93887a08be5496c4a4" dependencies = [ "compact_str", ] [[package]] name = "ryu" version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" [[package]] name = "serde" version = "1.0.228" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" dependencies = [ "serde_core", "serde_derive", ] [[package]] name = "serde_core" version = "1.0.228" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" version = "1.0.228" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "serde_test" version = "1.0.177" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f901ee573cab6b3060453d2d5f0bae4e6d628c23c0a962ff9b5f1d7c8d4f1ed" dependencies = [ "serde", ] [[package]] name = "smallvec" version = "1.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" [[package]] name = "stable_deref_trait" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" [[package]] name = "static_assertions" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" [[package]] name = "stringprep" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b4df3d392d81bd458a8a621b8bffbd2302a12ffe288a9d931670948749463b1" dependencies = [ "unicode-bidi", "unicode-normalization", "unicode-properties", ] [[package]] name = "syn" version = "2.0.108" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da58917d35242480a05c2897064da0a80589a2a0476c9a3f2fdc83b53502e917" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "synstructure" version = "0.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "thiserror" version = "2.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f63587ca0f12b72a0600bcba1d40081f830876000bb46dd2337a3051618f4fc8" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" version = "2.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ff15c8ecd7de3849db632e14d18d2571fa09dfc5ed93479bc4485c7a517c913" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "tinystr" version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42d3e9c45c09de15d06dd8acf5f4e0e399e85927b7f00711024eb7ae10fa4869" dependencies = [ "displaydoc", "zerovec", ] [[package]] name = "tinyvec" version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bfa5fdc3bce6191a1dbc8c02d5c8bffcf557bafa17c124c5264a458f1b0613fa" dependencies = [ "tinyvec_macros", ] [[package]] name = "tinyvec_macros" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "unicode-bidi" version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c1cb5db39152898a79168971543b1cb5020dff7fe43c8dc468b0885f5e29df5" [[package]] name = "unicode-ident" version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" [[package]] name = "unicode-normalization" version = "0.1.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5fd4f6878c9cb28d874b009da9e8d183b5abc80117c40bbd187a1fde336be6e8" dependencies = [ "tinyvec", ] [[package]] name = "unicode-properties" version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7df058c713841ad818f1dc5d3fd88063241cc61f49f5fbea4b951e8cf5a8d71d" [[package]] name = "utf8_iter" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" [[package]] name = "writeable" version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9edde0db4769d2dc68579893f2306b26c6ecfbe0ef499b013d731b7b9247e0b9" [[package]] name = "yoke" version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72d6e5c6afb84d73944e5cedb052c4680d5657337201555f9f2a16b7406d4954" dependencies = [ "stable_deref_trait", "yoke-derive", "zerofrom", ] [[package]] name = "yoke-derive" version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b659052874eb698efe5b9e8cf382204678a0086ebf46982b79d6ca3182927e5d" dependencies = [ "proc-macro2", "quote", "syn", "synstructure", ] [[package]] name = "zerofrom" version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "50cc42e0333e05660c3587f3bf9d0478688e15d870fab3346451ce7f8c9fbea5" dependencies = [ "zerofrom-derive", ] [[package]] name = "zerofrom-derive" version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502" dependencies = [ "proc-macro2", "quote", "syn", "synstructure", ] [[package]] name = "zerotrie" version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a59c17a5562d507e4b54960e8569ebee33bee890c70aa3fe7b97e85a9fd7851" dependencies = [ "displaydoc", "yoke", "zerofrom", ] [[package]] name = "zerovec" version = "0.11.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c28719294829477f525be0186d13efa9a3c602f7ec202ca9e353d310fb9a002" dependencies = [ "yoke", "zerofrom", "zerovec-derive", ] [[package]] name = "zerovec-derive" version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eadce39539ca5cb3985590102671f2567e659fca9666581ad3411d59207951f3" dependencies = [ "proc-macro2", "quote", "syn", ] jid-0.12.1/Cargo.toml0000644000000031360000000000100076640ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" name = "jid" version = "0.12.1" authors = [ "lumi ", "Emmanuel Gil Peyrot ", "Maxime “pep” Buquet ", ] build = false autolib = false autobins = false autoexamples = false autotests = false autobenches = false description = "A crate which provides a Jid struct for Jabber IDs." homepage = "https://gitlab.com/xmpp-rs/xmpp-rs" documentation = "https://docs.rs/jid" readme = "README.md" keywords = [ "xmpp", "jid", ] license = "MPL-2.0" repository = "https://gitlab.com/xmpp-rs/xmpp-rs" [badges.gitlab] repository = "xmpp-rs/xmpp-rs" [features] default = [] quote = [ "dep:quote", "dep:proc-macro2", ] [lib] name = "jid" path = "src/lib.rs" [dependencies.idna] version = "1" [dependencies.memchr] version = "2.5" [dependencies.minidom] version = "0.18" optional = true [dependencies.proc-macro2] version = "1.0" optional = true [dependencies.quote] version = "1.0" optional = true [dependencies.serde] version = "1.0" features = ["derive"] optional = true [dependencies.stringprep] version = "0.1.3" [dev-dependencies.serde_test] version = "1" jid-0.12.1/Cargo.toml.orig000064400000000000000000000017411046102023000133450ustar 00000000000000[package] name = "jid" version = "0.12.1" authors = [ "lumi ", "Emmanuel Gil Peyrot ", "Maxime “pep” Buquet ", ] description = "A crate which provides a Jid struct for Jabber IDs." homepage = "https://gitlab.com/xmpp-rs/xmpp-rs" repository = "https://gitlab.com/xmpp-rs/xmpp-rs" documentation = "https://docs.rs/jid" readme = "README.md" keywords = ["xmpp", "jid"] license = "MPL-2.0" edition = "2021" [badges] gitlab = { repository = "xmpp-rs/xmpp-rs" } [dependencies] memchr = "2.5" serde = { version = "1.0", features = ["derive"], optional = true } stringprep = "0.1.3" quote = { version = "1.0", optional = true } proc-macro2 = { version = "1.0", optional = true } idna = "1" # same repository dependencies minidom = { version = "0.18", path = "../minidom", optional = true } [dev-dependencies] serde_test = "1" jid = { path = ".", features = [ "serde" ] } [features] default = [] quote = ["dep:quote", "dep:proc-macro2"] jid-0.12.1/LICENSE000064400000000000000000000405261046102023000114670ustar 00000000000000Mozilla Public License Version 2.0 ================================== 1. Definitions -------------- 1.1. "Contributor" means each individual or legal entity that creates, contributes to the creation of, or owns Covered Software. 1.2. "Contributor Version" means the combination of the Contributions of others (if any) used by a Contributor and that particular Contributor's Contribution. 1.3. "Contribution" means Covered Software of a particular Contributor. 1.4. "Covered Software" means Source Code Form to which the initial Contributor has attached the notice in Exhibit A, the Executable Form of such Source Code Form, and Modifications of such Source Code Form, in each case including portions thereof. 1.5. "Incompatible With Secondary Licenses" means (a) that the initial Contributor has attached the notice described in Exhibit B to the Covered Software; or (b) that the Covered Software was made available under the terms of version 1.1 or earlier of the License, but not also under the terms of a Secondary License. 1.6. "Executable Form" means any form of the work other than Source Code Form. 1.7. "Larger Work" means a work that combines Covered Software with other material, in a separate file or files, that is not Covered Software. 1.8. "License" means this document. 1.9. "Licensable" means having the right to grant, to the maximum extent possible, whether at the time of the initial grant or subsequently, any and all of the rights conveyed by this License. 1.10. "Modifications" means any of the following: (a) any file in Source Code Form that results from an addition to, deletion from, or modification of the contents of Covered Software; or (b) any new file in Source Code Form that contains any Covered Software. 1.11. "Patent Claims" of a Contributor means any patent claim(s), including without limitation, method, process, and apparatus claims, in any patent Licensable by such Contributor that would be infringed, but for the grant of the License, by the making, using, selling, offering for sale, having made, import, or transfer of either its Contributions or its Contributor Version. 1.12. "Secondary License" means either the GNU General Public License, Version 2.0, the GNU Lesser General Public License, Version 2.1, the GNU Affero General Public License, Version 3.0, or any later versions of those licenses. 1.13. "Source Code Form" means the form of the work preferred for making modifications. 1.14. "You" (or "Your") means an individual or a legal entity exercising rights under this License. For legal entities, "You" includes any entity that controls, is controlled by, or is under common control with You. For purposes of this definition, "control" means (a) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (b) ownership of more than fifty percent (50%) of the outstanding shares or beneficial ownership of such entity. 2. License Grants and Conditions -------------------------------- 2.1. Grants Each Contributor hereby grants You a world-wide, royalty-free, non-exclusive license: (a) under intellectual property rights (other than patent or trademark) Licensable by such Contributor to use, reproduce, make available, modify, display, perform, distribute, and otherwise exploit its Contributions, either on an unmodified basis, with Modifications, or as part of a Larger Work; and (b) under Patent Claims of such Contributor to make, use, sell, offer for sale, have made, import, and otherwise transfer either its Contributions or its Contributor Version. 2.2. Effective Date The licenses granted in Section 2.1 with respect to any Contribution become effective for each Contribution on the date the Contributor first distributes such Contribution. 2.3. Limitations on Grant Scope The licenses granted in this Section 2 are the only rights granted under this License. No additional rights or licenses will be implied from the distribution or licensing of Covered Software under this License. Notwithstanding Section 2.1(b) above, no patent license is granted by a Contributor: (a) for any code that a Contributor has removed from Covered Software; or (b) for infringements caused by: (i) Your and any other third party's modifications of Covered Software, or (ii) the combination of its Contributions with other software (except as part of its Contributor Version); or (c) under Patent Claims infringed by Covered Software in the absence of its Contributions. This License does not grant any rights in the trademarks, service marks, or logos of any Contributor (except as may be necessary to comply with the notice requirements in Section 3.4). 2.4. Subsequent Licenses No Contributor makes additional grants as a result of Your choice to distribute the Covered Software under a subsequent version of this License (see Section 10.2) or under the terms of a Secondary License (if permitted under the terms of Section 3.3). 2.5. Representation Each Contributor represents that the Contributor believes its Contributions are its original creation(s) or it has sufficient rights to grant the rights to its Contributions conveyed by this License. 2.6. Fair Use This License is not intended to limit any rights You have under applicable copyright doctrines of fair use, fair dealing, or other equivalents. 2.7. Conditions Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in Section 2.1. 3. Responsibilities ------------------- 3.1. Distribution of Source Form All distribution of Covered Software in Source Code Form, including any Modifications that You create or to which You contribute, must be under the terms of this License. You must inform recipients that the Source Code Form of the Covered Software is governed by the terms of this License, and how they can obtain a copy of this License. You may not attempt to alter or restrict the recipients' rights in the Source Code Form. 3.2. Distribution of Executable Form If You distribute Covered Software in Executable Form then: (a) such Covered Software must also be made available in Source Code Form, as described in Section 3.1, and You must inform recipients of the Executable Form how they can obtain a copy of such Source Code Form by reasonable means in a timely manner, at a charge no more than the cost of distribution to the recipient; and (b) You may distribute such Executable Form under the terms of this License, or sublicense it under different terms, provided that the license for the Executable Form does not attempt to limit or alter the recipients' rights in the Source Code Form under this License. 3.3. Distribution of a Larger Work You may create and distribute a Larger Work under terms of Your choice, provided that You also comply with the requirements of this License for the Covered Software. If the Larger Work is a combination of Covered Software with a work governed by one or more Secondary Licenses, and the Covered Software is not Incompatible With Secondary Licenses, this License permits You to additionally distribute such Covered Software under the terms of such Secondary License(s), so that the recipient of the Larger Work may, at their option, further distribute the Covered Software under the terms of either this License or such Secondary License(s). 3.4. Notices You may not remove or alter the substance of any license notices (including copyright notices, patent notices, disclaimers of warranty, or limitations of liability) contained within the Source Code Form of the Covered Software, except that You may alter any license notices to the extent required to remedy known factual inaccuracies. 3.5. Application of Additional Terms You may choose to offer, and to charge a fee for, warranty, support, indemnity or liability obligations to one or more recipients of Covered Software. However, You may do so only on Your own behalf, and not on behalf of any Contributor. You must make it absolutely clear that any such warranty, support, indemnity, or liability obligation is offered by You alone, and You hereby agree to indemnify every Contributor for any liability incurred by such Contributor as a result of warranty, support, indemnity or liability terms You offer. You may include additional disclaimers of warranty and limitations of liability specific to any jurisdiction. 4. Inability to Comply Due to Statute or Regulation --------------------------------------------------- If it is impossible for You to comply with any of the terms of this License with respect to some or all of the Covered Software due to statute, judicial order, or regulation then You must: (a) comply with the terms of this License to the maximum extent possible; and (b) describe the limitations and the code they affect. Such description must be placed in a text file included with all distributions of the Covered Software under this License. Except to the extent prohibited by statute or regulation, such description must be sufficiently detailed for a recipient of ordinary skill to be able to understand it. 5. Termination -------------- 5.1. The rights granted under this License will terminate automatically if You fail to comply with any of its terms. However, if You become compliant, then the rights granted under this License from a particular Contributor are reinstated (a) provisionally, unless and until such Contributor explicitly and finally terminates Your grants, and (b) on an ongoing basis, if such Contributor fails to notify You of the non-compliance by some reasonable means prior to 60 days after You have come back into compliance. Moreover, Your grants from a particular Contributor are reinstated on an ongoing basis if such Contributor notifies You of the non-compliance by some reasonable means, this is the first time You have received notice of non-compliance with this License from such Contributor, and You become compliant prior to 30 days after Your receipt of the notice. 5.2. If You initiate litigation against any entity by asserting a patent infringement claim (excluding declaratory judgment actions, counter-claims, and cross-claims) alleging that a Contributor Version directly or indirectly infringes any patent, then the rights granted to You by any and all Contributors for the Covered Software under Section 2.1 of this License shall terminate. 5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user license agreements (excluding distributors and resellers) which have been validly granted by You or Your distributors under this License prior to termination shall survive termination. ************************************************************************ * * * 6. Disclaimer of Warranty * * ------------------------- * * * * Covered Software is provided under this License on an "as is" * * basis, without warranty of any kind, either expressed, implied, or * * statutory, including, without limitation, warranties that the * * Covered Software is free of defects, merchantable, fit for a * * particular purpose or non-infringing. The entire risk as to the * * quality and performance of the Covered Software is with You. * * Should any Covered Software prove defective in any respect, You * * (not any Contributor) assume the cost of any necessary servicing, * * repair, or correction. This disclaimer of warranty constitutes an * * essential part of this License. No use of any Covered Software is * * authorized under this License except under this disclaimer. * * * ************************************************************************ ************************************************************************ * * * 7. Limitation of Liability * * -------------------------- * * * * Under no circumstances and under no legal theory, whether tort * * (including negligence), contract, or otherwise, shall any * * Contributor, or anyone who distributes Covered Software as * * permitted above, be liable to You for any direct, indirect, * * special, incidental, or consequential damages of any character * * including, without limitation, damages for lost profits, loss of * * goodwill, work stoppage, computer failure or malfunction, or any * * and all other commercial damages or losses, even if such party * * shall have been informed of the possibility of such damages. This * * limitation of liability shall not apply to liability for death or * * personal injury resulting from such party's negligence to the * * extent applicable law prohibits such limitation. Some * * jurisdictions do not allow the exclusion or limitation of * * incidental or consequential damages, so this exclusion and * * limitation may not apply to You. * * * ************************************************************************ 8. Litigation ------------- Any litigation relating to this License may be brought only in the courts of a jurisdiction where the defendant maintains its principal place of business and such litigation shall be governed by laws of that jurisdiction, without reference to its conflict-of-law provisions. Nothing in this Section shall prevent a party's ability to bring cross-claims or counter-claims. 9. Miscellaneous ---------------- This License represents the complete agreement concerning the subject matter hereof. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable. Any law or regulation which provides that the language of a contract shall be construed against the drafter shall not be used to construe this License against a Contributor. 10. Versions of the License --------------------------- 10.1. New Versions Mozilla Foundation is the license steward. Except as provided in Section 10.3, no one other than the license steward has the right to modify or publish new versions of this License. Each version will be given a distinguishing version number. 10.2. Effect of New Versions You may distribute the Covered Software under the terms of the version of the License under which You originally received the Covered Software, or under the terms of any subsequent version published by the license steward. 10.3. Modified Versions If you create software not governed by this License, and you want to create a new license for such software, you may create and use a modified version of this License if you rename the license and remove any references to the name of the license steward (except to note that such modified license differs from this License). 10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses If You choose to distribute Source Code Form that is Incompatible With Secondary Licenses under the terms of this version of the License, the notice described in Exhibit B of this License must be attached. Exhibit A - Source Code Form License Notice ------------------------------------------- This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. If it is not possible or desirable to put the notice in a particular file, then You may include the notice in a location (such as a LICENSE file in a relevant directory) where a recipient would be likely to look for such a notice. You may add additional accurate notices of copyright ownership. Exhibit B - "Incompatible With Secondary Licenses" Notice --------------------------------------------------------- This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. jid-0.12.1/README.md000064400000000000000000000004711046102023000117340ustar 00000000000000jid === What's this? ------------ A crate which provides a struct Jid for Jabber IDs. It's used in xmpp-rs but other XMPP libraries can of course use this. What license is it under? ------------------------- MPL-2.0 or later, see the `LICENSE` file. Notes ----- This library does not yet implement RFC7622. jid-0.12.1/src/error.rs000064400000000000000000000051541046102023000127460ustar 00000000000000// Copyright (c) 2017, 2018 lumi // Copyright (c) 2017, 2018, 2019 Emmanuel Gil Peyrot // Copyright (c) 2017, 2018, 2019 Maxime “pep” Buquet // Copyright (c) 2017, 2018 Astro // Copyright (c) 2017 Bastien Orivel // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. use core::fmt; /// An error that signifies that a `Jid` cannot be parsed from a string. #[derive(Debug, PartialEq, Eq)] pub enum Error { /// Happens when the node is empty, that is the string starts with a @. NodeEmpty, /// Happens when the resource is empty, that is the string ends with a /. ResourceEmpty, /// Happens when the localpart is longer than 1023 bytes. NodeTooLong, /// Happens when the resource is longer than 1023 bytes. ResourceTooLong, /// Happens when the localpart is invalid according to nodeprep. NodePrep, /// Happens when the domain is invalid according to nameprep. NamePrep, /// Happens when the resource is invalid according to resourceprep. ResourcePrep, /// Happens when there is no resource, that is string contains no /. ResourceMissingInFullJid, /// Happens when parsing a bare JID and there is a resource. ResourceInBareJid, /// Happens when parsing a JID which has two @ before the resource. TooManyAts, /// Happens when the domain is invalid according to idna. Idna, } impl core::error::Error for Error {} impl fmt::Display for Error { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.write_str(match self { Error::NodeEmpty => "nodepart empty despite the presence of a @", Error::ResourceEmpty => "resource empty despite the presence of a /", Error::NodeTooLong => "localpart longer than 1023 bytes", Error::ResourceTooLong => "resource longer than 1023 bytes", Error::NodePrep => "localpart doesn’t pass nodeprep validation", Error::NamePrep => "domain doesn’t pass nameprep validation", Error::ResourcePrep => "resource doesn’t pass resourceprep validation", Error::ResourceMissingInFullJid => "no resource found in this full JID", Error::ResourceInBareJid => "resource found while parsing a bare JID", Error::TooManyAts => "second @ found before parsing the resource", Error::Idna => "domain doesn’t pass idna validation", }) } } jid-0.12.1/src/lib.rs000064400000000000000000001307221046102023000123630ustar 00000000000000// Copyright (c) 2017, 2018 lumi // Copyright (c) 2017, 2018, 2019 Emmanuel Gil Peyrot // Copyright (c) 2017, 2018, 2019 Maxime “pep” Buquet // Copyright (c) 2017, 2018 Astro // Copyright (c) 2017 Bastien Orivel // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. #![no_std] #![deny(missing_docs)] #![cfg_attr(docsrs, feature(doc_cfg))] #![cfg_attr(docsrs, doc(auto_cfg))] //! Represents XMPP addresses, also known as JabberIDs (JIDs) for the [XMPP](https://xmpp.org/) //! protocol. A [`Jid`] can have between one and three parts in the form `node@domain/resource`: //! - the (optional) node part designates a specific account/service on a server, for example //! `username@server.com` //! - the domain part designates a server, for example `irc.jabberfr.org` //! - the (optional) resource part designates a more specific client, such as a participant in a //! groupchat (`jabberfr@chat.jabberfr.org/user`) or a specific client device associated with an //! account (`user@example.com/dino`) //! //! The [`Jid`] enum can be one of two variants, containing a more specific type: //! - [`BareJid`] (`Jid::Bare` variant): a JID without a resource //! - [`FullJid`] (`Jid::Full` variant): a JID with a resource //! //! Jids as per the XMPP protocol only ever contain valid UTF-8. However, creating any form of Jid //! can fail in one of the following cases: //! - wrong syntax: creating a Jid with an empty (yet declared) node or resource part, such as //! `@example.com` or `user@example.com/` //! - stringprep error: some characters were invalid according to the stringprep algorithm, such as //! mixing left-to-write and right-to-left characters extern crate alloc; #[cfg(test)] extern crate std; use alloc::borrow::Cow; use alloc::format; use alloc::string::{String, ToString}; use core::borrow::Borrow; use core::cmp::Ordering; use core::fmt; use core::hash::{Hash, Hasher}; use core::mem; use core::net::{Ipv4Addr, Ipv6Addr}; use core::num::NonZeroU16; use core::ops::Deref; use core::str::FromStr; use memchr::memchr2_iter; use idna::uts46::{AsciiDenyList, DnsLength, Hyphens, Uts46}; use stringprep::{nameprep, nodeprep, resourceprep}; #[cfg(feature = "serde")] use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; #[cfg(feature = "quote")] use proc_macro2::TokenStream; #[cfg(feature = "quote")] use quote::{quote, ToTokens}; #[cfg(feature = "minidom")] use minidom::{IntoAttributeValue, Node}; mod error; mod parts; pub use crate::error::Error; pub use parts::{DomainPart, DomainRef, NodePart, NodeRef, ResourcePart, ResourceRef}; fn length_check(len: usize, error_empty: Error, error_too_long: Error) -> Result<(), Error> { if len == 0 { Err(error_empty) } else if len > 1023 { Err(error_too_long) } else { Ok(()) } } fn node_check(node: &str) -> Result, Error> { let node = nodeprep(node).map_err(|_| Error::NodePrep)?; length_check(node.len(), Error::NodeEmpty, Error::NodeTooLong)?; Ok(node) } fn resource_check(resource: &str) -> Result, Error> { let resource = resourceprep(resource).map_err(|_| Error::ResourcePrep)?; length_check(resource.len(), Error::ResourceEmpty, Error::ResourceTooLong)?; Ok(resource) } fn domain_check(mut domain: &str) -> Result, Error> { // First, check if this is an IPv4 address. if Ipv4Addr::from_str(domain).is_ok() { return Ok(Cow::Borrowed(domain)); } // Then if this is an IPv6 address. if domain.starts_with('[') && domain.ends_with(']') && Ipv6Addr::from_str(&domain[1..domain.len() - 1]).is_ok() { return Ok(Cow::Borrowed(domain)); } // idna can handle the root dot for us, but we still want to remove it for normalization // purposes. if domain.ends_with('.') { domain = &domain[..domain.len() - 1]; } Uts46::new() .to_ascii( domain.as_bytes(), AsciiDenyList::URL, Hyphens::Check, DnsLength::Verify, ) .map_err(|_| Error::Idna)?; let domain = nameprep(domain).map_err(|_| Error::NamePrep)?; Ok(domain) } /// A struct representing a Jabber ID (JID). /// /// This JID can either be "bare" (without a `/resource` suffix) or full (with /// a resource suffix). /// /// In many APIs, it is appropriate to use the more specific types /// ([`BareJid`] or [`FullJid`]) instead, as these two JID types are generally /// used in different contexts within XMPP. /// /// This dynamic type on the other hand can be used in contexts where it is /// not known, at compile-time, whether a JID is full or bare. #[derive(Clone, Eq)] pub struct Jid { normalized: String, at: Option, slash: Option, } impl PartialEq for Jid { fn eq(&self, other: &Jid) -> bool { self.normalized == other.normalized } } impl PartialOrd for Jid { fn partial_cmp(&self, other: &Jid) -> Option { Some(self.cmp(other)) } } impl Ord for Jid { fn cmp(&self, other: &Jid) -> Ordering { self.normalized.cmp(&other.normalized) } } impl Hash for Jid { fn hash(&self, state: &mut H) { self.normalized.hash(state) } } impl FromStr for Jid { type Err = Error; fn from_str(s: &str) -> Result { Self::new(s) } } impl From for Jid { fn from(other: BareJid) -> Self { other.inner } } impl From for Jid { fn from(other: FullJid) -> Self { other.inner } } impl fmt::Display for Jid { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.write_str(&self.normalized) } } impl Jid { /// Constructs a Jabber ID from a string. This is of the form /// `node`@`domain`/`resource`, where node and resource parts are optional. /// If you want a non-fallible version, use [`Jid::from_parts`] instead. /// /// # Examples /// /// ``` /// use jid::Jid; /// # use jid::Error; /// /// # fn main() -> Result<(), Error> { /// let jid = Jid::new("node@domain/resource")?; /// /// assert_eq!(jid.node().map(|x| x.as_str()), Some("node")); /// assert_eq!(jid.domain().as_str(), "domain"); /// assert_eq!(jid.resource().map(|x| x.as_str()), Some("resource")); /// # Ok(()) /// # } /// ``` pub fn new(unnormalized: &str) -> Result { let bytes = unnormalized.as_bytes(); let orig_at; let orig_slash; let mut iter = memchr2_iter(b'@', b'/', bytes); let normalized = if let Some(first_index) = iter.next() { let byte = bytes[first_index]; if byte == b'@' { if let Some(second_index) = iter.next() { let byte = bytes[second_index]; if byte == b'/' { let node = node_check(&unnormalized[..first_index])?; let domain = domain_check(&unnormalized[first_index + 1..second_index])?; let resource = resource_check(&unnormalized[second_index + 1..])?; orig_at = Some(node.len()); orig_slash = Some(node.len() + domain.len() + 1); match (node, domain, resource) { (Cow::Borrowed(_), Cow::Borrowed(_), Cow::Borrowed(_)) => { unnormalized.to_string() } (node, domain, resource) => format!("{node}@{domain}/{resource}"), } } else /* This is another '@' character. */ { return Err(Error::TooManyAts); } } else { // That is a node@domain JID. let node = node_check(&unnormalized[..first_index])?; let domain = domain_check(&unnormalized[first_index + 1..])?; orig_at = Some(node.len()); orig_slash = None; match (node, domain) { (Cow::Borrowed(_), Cow::Borrowed(_)) => unnormalized.to_string(), (node, domain) => format!("{node}@{domain}"), } } } else /* This is a '/' character. */ { // The JID is of the form domain/resource, we can stop looking for further // characters. let domain = domain_check(&unnormalized[..first_index])?; let resource = resource_check(&unnormalized[first_index + 1..])?; orig_at = None; orig_slash = Some(domain.len()); match (domain, resource) { (Cow::Borrowed(_), Cow::Borrowed(_)) => unnormalized.to_string(), (domain, resource) => format!("{domain}/{resource}"), } } } else { // Last possible case, just a domain JID. let domain = domain_check(unnormalized)?; orig_at = None; orig_slash = None; domain.into_owned() }; Ok(Self { normalized, at: orig_at.and_then(|x| NonZeroU16::new(x as u16)), slash: orig_slash.and_then(|x| NonZeroU16::new(x as u16)), }) } /// Returns the inner String of this JID. pub fn into_inner(self) -> String { self.normalized } /// Build a [`Jid`] from typed parts. This method cannot fail because it uses parts that have /// already been parsed and stringprepped into [`NodePart`], [`DomainPart`], and [`ResourcePart`]. /// /// This method allocates and does not consume the typed parts. To avoid /// allocation if both `node` and `resource` are known to be `None` and /// `domain` is owned, you can use `domain.into()`. pub fn from_parts( node: Option<&NodeRef>, domain: &DomainRef, resource: Option<&ResourceRef>, ) -> Self { match resource { Some(resource) => FullJid::from_parts(node, domain, resource).into(), None => BareJid::from_parts(node, domain).into(), } } /// The optional node part of the JID as reference. pub fn node(&self) -> Option<&NodeRef> { self.at.map(|at| { let at = u16::from(at) as usize; NodeRef::from_str_unchecked(&self.normalized[..at]) }) } /// The domain part of the JID as reference pub fn domain(&self) -> &DomainRef { match (self.at, self.slash) { (Some(at), Some(slash)) => { let at = u16::from(at) as usize; let slash = u16::from(slash) as usize; DomainRef::from_str_unchecked(&self.normalized[at + 1..slash]) } (Some(at), None) => { let at = u16::from(at) as usize; DomainRef::from_str_unchecked(&self.normalized[at + 1..]) } (None, Some(slash)) => { let slash = u16::from(slash) as usize; DomainRef::from_str_unchecked(&self.normalized[..slash]) } (None, None) => DomainRef::from_str_unchecked(&self.normalized), } } /// The optional resource of the Jabber ID. It is guaranteed to be present when the JID is /// a Full variant, which you can check with [`Jid::is_full`]. pub fn resource(&self) -> Option<&ResourceRef> { self.slash.map(|slash| { let slash = u16::from(slash) as usize; ResourceRef::from_str_unchecked(&self.normalized[slash + 1..]) }) } /// Allocate a new [`BareJid`] from this JID, discarding the resource. pub fn to_bare(&self) -> BareJid { BareJid::from_parts(self.node(), self.domain()) } /// Transforms this JID into a [`BareJid`], throwing away the resource. /// /// ``` /// # use jid::{BareJid, Jid}; /// let jid: Jid = "foo@bar/baz".parse().unwrap(); /// let bare = jid.into_bare(); /// assert_eq!(bare.to_string(), "foo@bar"); /// ``` pub fn into_bare(mut self) -> BareJid { if let Some(slash) = self.slash { // truncate the string self.normalized.truncate(slash.get() as usize); self.slash = None; } BareJid { inner: self } } /// Checks if the JID is a full JID. pub fn is_full(&self) -> bool { self.slash.is_some() } /// Checks if the JID is a bare JID. pub fn is_bare(&self) -> bool { self.slash.is_none() } /// Return a reference to the canonical string representation of the JID. pub fn as_str(&self) -> &str { &self.normalized } /// Try to convert this Jid to a [`FullJid`] if it contains a resource /// and return a [`BareJid`] otherwise. /// /// This is useful for match blocks: /// /// ``` /// # use jid::Jid; /// let jid: Jid = "foo@bar".parse().unwrap(); /// match jid.try_into_full() { /// Ok(full) => println!("it is full: {:?}", full), /// Err(bare) => println!("it is bare: {:?}", bare), /// } /// ``` pub fn try_into_full(self) -> Result { if self.slash.is_some() { Ok(FullJid { inner: self }) } else { Err(BareJid { inner: self }) } } /// Try to convert this Jid reference to a [`&FullJid`][`FullJid`] if it /// contains a resource and return a [`&BareJid`][`BareJid`] otherwise. /// /// This is useful for match blocks: /// /// ``` /// # use jid::Jid; /// let jid: Jid = "foo@bar".parse().unwrap(); /// match jid.try_as_full() { /// Ok(full) => println!("it is full: {:?}", full), /// Err(bare) => println!("it is bare: {:?}", bare), /// } /// ``` pub fn try_as_full(&self) -> Result<&FullJid, &BareJid> { if self.slash.is_some() { Ok(unsafe { // SAFETY: FullJid is #[repr(transparent)] of Jid // SOUNDNESS: we asserted that self.slash is set above mem::transmute::<&Jid, &FullJid>(self) }) } else { Err(unsafe { // SAFETY: BareJid is #[repr(transparent)] of Jid // SOUNDNESS: we asserted that self.slash is unset above mem::transmute::<&Jid, &BareJid>(self) }) } } /// Try to convert this mutable Jid reference to a /// [`&mut FullJid`][`FullJid`] if it contains a resource and return a /// [`&mut BareJid`][`BareJid`] otherwise. pub fn try_as_full_mut(&mut self) -> Result<&mut FullJid, &mut BareJid> { if self.slash.is_some() { Ok(unsafe { // SAFETY: FullJid is #[repr(transparent)] of Jid // SOUNDNESS: we asserted that self.slash is set above mem::transmute::<&mut Jid, &mut FullJid>(self) }) } else { Err(unsafe { // SAFETY: BareJid is #[repr(transparent)] of Jid // SOUNDNESS: we asserted that self.slash is unset above mem::transmute::<&mut Jid, &mut BareJid>(self) }) } } #[doc(hidden)] #[allow(non_snake_case)] #[deprecated( since = "0.11.0", note = "use Jid::from (for construction of Jid values) or Jid::try_into_full/Jid::try_as_full (for match blocks) instead" )] pub fn Bare(other: BareJid) -> Self { Self::from(other) } #[doc(hidden)] #[allow(non_snake_case)] #[deprecated( since = "0.11.0", note = "use Jid::from (for construction of Jid values) or Jid::try_into_full/Jid::try_as_full (for match blocks) instead" )] pub fn Full(other: FullJid) -> Self { Self::from(other) } } impl TryFrom for FullJid { type Error = Error; fn try_from(inner: Jid) -> Result { if inner.slash.is_none() { return Err(Error::ResourceMissingInFullJid); } Ok(Self { inner }) } } impl TryFrom for BareJid { type Error = Error; fn try_from(inner: Jid) -> Result { if inner.slash.is_some() { return Err(Error::ResourceInBareJid); } Ok(Self { inner }) } } impl PartialEq for FullJid { fn eq(&self, other: &Jid) -> bool { &self.inner == other } } impl PartialEq for BareJid { fn eq(&self, other: &Jid) -> bool { &self.inner == other } } impl PartialEq for Jid { fn eq(&self, other: &FullJid) -> bool { self == &other.inner } } impl PartialEq for Jid { fn eq(&self, other: &BareJid) -> bool { self == &other.inner } } /// A struct representing a full Jabber ID, with a resource part. /// /// A full JID is composed of 3 components, of which only the node is optional: /// /// - the (optional) node part is the part before the (optional) `@`. /// - the domain part is the mandatory part between the (optional) `@` and before the `/`. /// - the resource part after the `/`. /// /// Unlike a [`BareJid`], it always contains a resource, and should only be used when you are /// certain there is no case where a resource can be missing. Otherwise, use a [`Jid`] or /// [`BareJid`]. #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[repr(transparent)] // WARNING: Jid::try_as_* relies on this for safety! pub struct FullJid { inner: Jid, } /// A struct representing a bare Jabber ID, without a resource part. /// /// A bare JID is composed of 2 components, of which only the node is optional: /// - the (optional) node part is the part before the (optional) `@`. /// - the domain part is the mandatory part between the (optional) `@` and before the `/`. /// /// Unlike a [`FullJid`], it can’t contain a resource, and should only be used when you are certain /// there is no case where a resource can be set. Otherwise, use a [`Jid`] or [`FullJid`]. #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[repr(transparent)] // WARNING: Jid::try_as_* relies on this for safety! pub struct BareJid { inner: Jid, } impl Deref for FullJid { type Target = Jid; fn deref(&self) -> &Self::Target { &self.inner } } impl Deref for BareJid { type Target = Jid; fn deref(&self) -> &Self::Target { &self.inner } } impl Borrow for FullJid { fn borrow(&self) -> &Jid { &self.inner } } impl Borrow for BareJid { fn borrow(&self) -> &Jid { &self.inner } } impl fmt::Debug for Jid { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.debug_tuple("Jid").field(&self.normalized).finish() } } impl fmt::Debug for FullJid { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.debug_tuple("FullJid") .field(&self.inner.normalized) .finish() } } impl fmt::Debug for BareJid { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.debug_tuple("BareJid") .field(&self.inner.normalized) .finish() } } impl fmt::Display for FullJid { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self.inner, fmt) } } impl fmt::Display for BareJid { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self.inner, fmt) } } #[cfg(feature = "serde")] impl Serialize for Jid { fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_str(&self.normalized) } } #[cfg(feature = "serde")] impl Serialize for FullJid { fn serialize(&self, serializer: S) -> Result where S: Serializer, { self.inner.serialize(serializer) } } #[cfg(feature = "serde")] impl Serialize for BareJid { fn serialize(&self, serializer: S) -> Result where S: Serializer, { self.inner.serialize(serializer) } } impl FromStr for FullJid { type Err = Error; fn from_str(s: &str) -> Result { Self::new(s) } } #[cfg(feature = "serde")] impl<'de> Deserialize<'de> for Jid { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { let s = String::deserialize(deserializer)?; Jid::new(&s).map_err(de::Error::custom) } } #[cfg(feature = "serde")] impl<'de> Deserialize<'de> for FullJid { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { let jid = Jid::deserialize(deserializer)?; jid.try_into().map_err(de::Error::custom) } } #[cfg(feature = "serde")] impl<'de> Deserialize<'de> for BareJid { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { let jid = Jid::deserialize(deserializer)?; jid.try_into().map_err(de::Error::custom) } } #[cfg(feature = "quote")] impl ToTokens for Jid { fn to_tokens(&self, tokens: &mut TokenStream) { let s = &self.normalized; tokens.extend(quote! { jid::Jid::new(#s).unwrap() }); } } #[cfg(feature = "quote")] impl ToTokens for FullJid { fn to_tokens(&self, tokens: &mut TokenStream) { let s = &self.inner.normalized; tokens.extend(quote! { jid::FullJid::new(#s).unwrap() }); } } #[cfg(feature = "quote")] impl ToTokens for BareJid { fn to_tokens(&self, tokens: &mut TokenStream) { let s = &self.inner.normalized; tokens.extend(quote! { jid::BareJid::new(#s).unwrap() }); } } impl FullJid { /// Constructs a full Jabber ID containing all three components. This is of the form /// `node@domain/resource`, where node part is optional. /// If you want a non-fallible version, use [`FullJid::from_parts`] instead. /// /// # Examples /// /// ``` /// use jid::FullJid; /// # use jid::Error; /// /// # fn main() -> Result<(), Error> { /// let jid = FullJid::new("node@domain/resource")?; /// /// assert_eq!(jid.node().map(|x| x.as_str()), Some("node")); /// assert_eq!(jid.domain().as_str(), "domain"); /// assert_eq!(jid.resource().as_str(), "resource"); /// # Ok(()) /// # } /// ``` pub fn new(unnormalized: &str) -> Result { Jid::new(unnormalized)?.try_into() } /// Build a [`FullJid`] from typed parts. This method cannot fail because it uses parts that have /// already been parsed and stringprepped into [`NodePart`], [`DomainPart`], and [`ResourcePart`]. /// This method allocates and does not consume the typed parts. pub fn from_parts( node: Option<&NodeRef>, domain: &DomainRef, resource: &ResourceRef, ) -> FullJid { let (at, slash, normalized); if let Some(node) = node { // Parts are never empty so len > 0 for NonZeroU16::new is always Some at = NonZeroU16::new(node.len() as u16); slash = NonZeroU16::new((node.len() + 1 + domain.len()) as u16); normalized = format!("{node}@{domain}/{resource}"); } else { at = None; slash = NonZeroU16::new(domain.len() as u16); normalized = format!("{domain}/{resource}"); } let inner = Jid { normalized, at, slash, }; Self { inner } } /// The optional resource of the Jabber ID. Since this is a full JID it is always present. pub fn resource(&self) -> &ResourceRef { self.inner.resource().unwrap() } /// Return the inner String of this full JID. pub fn into_inner(self) -> String { self.inner.into_inner() } /// Transforms this full JID into a [`BareJid`], throwing away the /// resource. /// /// ``` /// # use jid::{BareJid, FullJid}; /// let jid: FullJid = "foo@bar/baz".parse().unwrap(); /// let bare = jid.into_bare(); /// assert_eq!(bare.to_string(), "foo@bar"); /// ``` pub fn into_bare(self) -> BareJid { self.inner.into_bare() } } impl FromStr for BareJid { type Err = Error; fn from_str(s: &str) -> Result { Self::new(s) } } impl BareJid { /// Constructs a bare Jabber ID, containing two components. This is of the form /// `node`@`domain`, where node part is optional. /// If you want a non-fallible version, use [`BareJid::from_parts`] instead. /// /// # Examples /// /// ``` /// use jid::BareJid; /// # use jid::Error; /// /// # fn main() -> Result<(), Error> { /// let jid = BareJid::new("node@domain")?; /// /// assert_eq!(jid.node().map(|x| x.as_str()), Some("node")); /// assert_eq!(jid.domain().as_str(), "domain"); /// # Ok(()) /// # } /// ``` pub fn new(unnormalized: &str) -> Result { Jid::new(unnormalized)?.try_into() } /// Build a [`BareJid`] from typed parts. This method cannot fail because it uses parts that have /// already been parsed and stringprepped into [`NodePart`] and [`DomainPart`]. /// /// This method allocates and does not consume the typed parts. To avoid /// allocation if `node` is known to be `None` and `domain` is owned, you /// can use `domain.into()`. pub fn from_parts(node: Option<&NodeRef>, domain: &DomainRef) -> Self { let (at, normalized); if let Some(node) = node { // Parts are never empty so len > 0 for NonZeroU16::new is always Some at = NonZeroU16::new(node.len() as u16); normalized = format!("{node}@{domain}"); } else { at = None; normalized = domain.to_string(); } let inner = Jid { normalized, at, slash: None, }; Self { inner } } /// Constructs a [`BareJid`] from the bare JID, by specifying a [`ResourcePart`]. /// If you'd like to specify a stringy resource, use [`BareJid::with_resource_str`] instead. /// /// # Examples /// /// ``` /// use jid::{BareJid, ResourcePart}; /// /// let resource = ResourcePart::new("resource").unwrap(); /// let bare = BareJid::new("node@domain").unwrap(); /// let full = bare.with_resource(&resource); /// /// assert_eq!(full.node().map(|x| x.as_str()), Some("node")); /// assert_eq!(full.domain().as_str(), "domain"); /// assert_eq!(full.resource().as_str(), "resource"); /// ``` pub fn with_resource(&self, resource: &ResourceRef) -> FullJid { let slash = NonZeroU16::new(self.inner.normalized.len() as u16); let normalized = format!("{}/{resource}", self.inner.normalized); let inner = Jid { normalized, at: self.inner.at, slash, }; FullJid { inner } } /// Constructs a [`FullJid`] from the bare JID, by specifying a stringy `resource`. /// If your resource has already been parsed into a [`ResourcePart`], use [`BareJid::with_resource`]. /// /// # Examples /// /// ``` /// use jid::BareJid; /// /// let bare = BareJid::new("node@domain").unwrap(); /// let full = bare.with_resource_str("resource").unwrap(); /// /// assert_eq!(full.node().map(|x| x.as_str()), Some("node")); /// assert_eq!(full.domain().as_str(), "domain"); /// assert_eq!(full.resource().as_str(), "resource"); /// ``` pub fn with_resource_str(&self, resource: &str) -> Result { let resource = ResourcePart::new(resource)?; Ok(self.with_resource(&resource)) } /// Return the inner String of this bare JID. pub fn into_inner(self) -> String { self.inner.into_inner() } } #[cfg(feature = "minidom")] impl IntoAttributeValue for Jid { fn into_attribute_value(self) -> Option { Some(self.to_string()) } } #[cfg(feature = "minidom")] impl From for Node { fn from(jid: Jid) -> Self { Node::Text(jid.to_string()) } } #[cfg(feature = "minidom")] impl IntoAttributeValue for FullJid { fn into_attribute_value(self) -> Option { self.inner.into_attribute_value() } } #[cfg(feature = "minidom")] impl From for Node { fn from(jid: FullJid) -> Self { jid.inner.into() } } #[cfg(feature = "minidom")] impl IntoAttributeValue for BareJid { fn into_attribute_value(self) -> Option { self.inner.into_attribute_value() } } #[cfg(feature = "minidom")] impl From for Node { fn from(other: BareJid) -> Self { other.inner.into() } } #[cfg(test)] mod tests { use super::*; use alloc::{ collections::{BTreeMap, BTreeSet}, vec::Vec, }; macro_rules! assert_size ( ($t:ty, $sz:expr) => ( assert_eq!(::core::mem::size_of::<$t>(), $sz); ); ); #[cfg(target_pointer_width = "32")] #[test] fn test_size() { assert_size!(BareJid, 16); assert_size!(FullJid, 16); assert_size!(Jid, 16); } #[cfg(target_pointer_width = "64")] #[test] fn test_size() { assert_size!(BareJid, 32); assert_size!(FullJid, 32); assert_size!(Jid, 32); } #[test] fn can_parse_full_jids() { assert_eq!( FullJid::from_str("a@b.c/d"), Ok(FullJid::new("a@b.c/d").unwrap()) ); assert_eq!( FullJid::from_str("b.c/d"), Ok(FullJid::new("b.c/d").unwrap()) ); assert_eq!( FullJid::from_str("a@b.c"), Err(Error::ResourceMissingInFullJid) ); assert_eq!( FullJid::from_str("b.c"), Err(Error::ResourceMissingInFullJid) ); } #[test] fn can_parse_bare_jids() { assert_eq!( BareJid::from_str("a@b.c"), Ok(BareJid::new("a@b.c").unwrap()) ); assert_eq!(BareJid::from_str("b.c"), Ok(BareJid::new("b.c").unwrap())); } #[test] fn can_parse_jids() { let full = FullJid::from_str("a@b.c/d").unwrap(); let bare = BareJid::from_str("e@f.g").unwrap(); assert_eq!(Jid::from_str("a@b.c/d").unwrap(), full); assert_eq!(Jid::from_str("e@f.g").unwrap(), bare); } #[test] fn full_to_bare_jid() { let bare: BareJid = FullJid::new("a@b.c/d").unwrap().to_bare(); assert_eq!(bare, BareJid::new("a@b.c").unwrap()); } #[test] fn bare_to_full_jid_str() { assert_eq!( BareJid::new("a@b.c") .unwrap() .with_resource_str("d") .unwrap(), FullJid::new("a@b.c/d").unwrap() ); } #[test] fn bare_to_full_jid() { assert_eq!( BareJid::new("a@b.c") .unwrap() .with_resource(&ResourcePart::new("d").unwrap()), FullJid::new("a@b.c/d").unwrap() ) } #[test] fn node_from_jid() { let jid = Jid::new("a@b.c/d").unwrap(); assert_eq!(jid.node().map(|x| x.as_str()), Some("a"),); } #[test] fn domain_from_jid() { let jid = Jid::new("a@b.c").unwrap(); assert_eq!(jid.domain().as_str(), "b.c"); } #[test] fn resource_from_jid() { let jid = Jid::new("a@b.c/d").unwrap(); assert_eq!(jid.resource().map(|x| x.as_str()), Some("d"),); } #[test] fn jid_to_full_bare() { let full = FullJid::new("a@b.c/d").unwrap(); let bare = BareJid::new("a@b.c").unwrap(); assert_eq!(FullJid::try_from(Jid::from(full.clone())), Ok(full.clone())); assert_eq!( FullJid::try_from(Jid::from(bare.clone())), Err(Error::ResourceMissingInFullJid), ); assert_eq!(Jid::from(full.clone().to_bare()), bare.clone()); assert_eq!(Jid::from(bare.clone()), bare); } #[test] fn serialise() { assert_eq!(FullJid::new("a@b/c").unwrap().to_string(), "a@b/c"); assert_eq!(BareJid::new("a@b").unwrap().to_string(), "a@b"); } #[test] fn hash() { let _map: BTreeMap = BTreeMap::new(); } #[test] fn invalid_jids() { assert_eq!(BareJid::from_str(""), Err(Error::Idna)); assert_eq!(BareJid::from_str("/c"), Err(Error::Idna)); assert_eq!(BareJid::from_str("a@/c"), Err(Error::Idna)); assert_eq!(BareJid::from_str("@b"), Err(Error::NodeEmpty)); assert_eq!(BareJid::from_str("b/"), Err(Error::ResourceEmpty)); assert_eq!(FullJid::from_str(""), Err(Error::Idna)); assert_eq!(FullJid::from_str("/c"), Err(Error::Idna)); assert_eq!(FullJid::from_str("a@/c"), Err(Error::Idna)); assert_eq!(FullJid::from_str("@b"), Err(Error::NodeEmpty)); assert_eq!(FullJid::from_str("b/"), Err(Error::ResourceEmpty)); assert_eq!( FullJid::from_str("a@b"), Err(Error::ResourceMissingInFullJid) ); assert_eq!(BareJid::from_str("a@b/c"), Err(Error::ResourceInBareJid)); assert_eq!(BareJid::from_str("a@b@c"), Err(Error::TooManyAts)); assert_eq!(FullJid::from_str("a@b@c/d"), Err(Error::TooManyAts)); } #[test] fn display_jids() { assert_eq!(FullJid::new("a@b/c").unwrap().to_string(), "a@b/c"); assert_eq!(BareJid::new("a@b").unwrap().to_string(), "a@b"); assert_eq!( Jid::from(FullJid::new("a@b/c").unwrap()).to_string(), "a@b/c" ); assert_eq!(Jid::from(BareJid::new("a@b").unwrap()).to_string(), "a@b"); } #[cfg(feature = "minidom")] #[test] fn minidom() { let elem: minidom::Element = "".parse().unwrap(); let to: Jid = elem.attr("from").unwrap().parse().unwrap(); assert_eq!(to, Jid::from(FullJid::new("a@b/c").unwrap())); let elem: minidom::Element = "".parse().unwrap(); let to: Jid = elem.attr("from").unwrap().parse().unwrap(); assert_eq!(to, Jid::from(BareJid::new("a@b").unwrap())); let elem: minidom::Element = "".parse().unwrap(); let to: FullJid = elem.attr("from").unwrap().parse().unwrap(); assert_eq!(to, FullJid::new("a@b/c").unwrap()); let elem: minidom::Element = "".parse().unwrap(); let to: BareJid = elem.attr("from").unwrap().parse().unwrap(); assert_eq!(to, BareJid::new("a@b").unwrap()); } #[cfg(feature = "minidom")] #[test] fn minidom_into_attr() { let full = FullJid::new("a@b/c").unwrap(); let elem = minidom::Element::builder("message", "jabber:client") .attr("from".try_into().unwrap(), full.clone()) .build(); assert_eq!(elem.attr("from"), Some(full.to_string().as_str())); let bare = BareJid::new("a@b").unwrap(); let elem = minidom::Element::builder("message", "jabber:client") .attr("from".try_into().unwrap(), bare.clone()) .build(); assert_eq!(elem.attr("from"), Some(bare.to_string().as_str())); let jid = Jid::from(bare.clone()); let _elem = minidom::Element::builder("message", "jabber:client") .attr("from".try_into().unwrap(), jid) .build(); assert_eq!(elem.attr("from"), Some(bare.to_string().as_str())); } #[test] fn stringprep() { let full = FullJid::from_str("Test@☃.coM/Test™").unwrap(); let equiv = FullJid::new("test@☃.com/TestTM").unwrap(); assert_eq!(full, equiv); } #[test] fn invalid_stringprep() { FullJid::from_str("a@b/🎉").unwrap_err(); } #[test] fn idna() { let bare = BareJid::from_str("Weiß.com.").unwrap(); let equiv = BareJid::new("weiss.com").unwrap(); assert_eq!(bare, equiv); BareJid::from_str("127.0.0.1").unwrap(); BareJid::from_str("[::1]").unwrap(); BareJid::from_str("domain.tld.").unwrap(); } #[test] fn invalid_idna() { BareJid::from_str("a@b@c").unwrap_err(); FullJid::from_str("a@b@c/d").unwrap_err(); BareJid::from_str("[::1234").unwrap_err(); BareJid::from_str("1::1234]").unwrap_err(); BareJid::from_str("domain.tld:5222").unwrap_err(); BareJid::from_str("-domain.tld").unwrap_err(); BareJid::from_str("domain.tld-").unwrap_err(); BareJid::from_str("domain..tld").unwrap_err(); BareJid::from_str("domain.tld..").unwrap_err(); BareJid::from_str("1234567890123456789012345678901234567890123456789012345678901234.com") .unwrap_err(); } #[test] fn jid_from_parts() { let node = NodePart::new("node").unwrap(); let domain = DomainPart::new("domain").unwrap(); let resource = ResourcePart::new("resource").unwrap(); let jid = Jid::from_parts(Some(&node), &domain, Some(&resource)); assert_eq!(jid, Jid::new("node@domain/resource").unwrap()); let barejid = BareJid::from_parts(Some(&node), &domain); assert_eq!(barejid, BareJid::new("node@domain").unwrap()); let fulljid = FullJid::from_parts(Some(&node), &domain, &resource); assert_eq!(fulljid, FullJid::new("node@domain/resource").unwrap()); } #[test] #[cfg(feature = "serde")] fn jid_ser_de() { let jid: Jid = Jid::new("node@domain").unwrap(); serde_test::assert_tokens(&jid, &[serde_test::Token::Str("node@domain")]); let jid: Jid = Jid::new("node@domain/resource").unwrap(); serde_test::assert_tokens(&jid, &[serde_test::Token::Str("node@domain/resource")]); let jid: BareJid = BareJid::new("node@domain").unwrap(); serde_test::assert_tokens(&jid, &[serde_test::Token::Str("node@domain")]); let jid: FullJid = FullJid::new("node@domain/resource").unwrap(); serde_test::assert_tokens(&jid, &[serde_test::Token::Str("node@domain/resource")]); } #[test] fn jid_into_parts_and_from_parts() { let node = NodePart::new("node").unwrap(); let domain = DomainPart::new("domain").unwrap(); let jid1 = domain.with_node(&node); let jid2 = node.with_domain(&domain); let jid3 = BareJid::new("node@domain").unwrap(); assert_eq!(jid1, jid2); assert_eq!(jid2, jid3); } #[test] fn jid_match_replacement_try_as() { let jid1 = Jid::new("foo@bar").unwrap(); let jid2 = Jid::new("foo@bar/baz").unwrap(); match jid1.try_as_full() { Err(_) => (), other => panic!("unexpected result: {:?}", other), }; match jid2.try_as_full() { Ok(_) => (), other => panic!("unexpected result: {:?}", other), }; } #[test] fn jid_match_replacement_try_as_mut() { let mut jid1 = Jid::new("foo@bar").unwrap(); let mut jid2 = Jid::new("foo@bar/baz").unwrap(); match jid1.try_as_full_mut() { Err(_) => (), other => panic!("unexpected result: {:?}", other), }; match jid2.try_as_full_mut() { Ok(_) => (), other => panic!("unexpected result: {:?}", other), }; } #[test] fn jid_match_replacement_try_into() { let jid1 = Jid::new("foo@bar").unwrap(); let jid2 = Jid::new("foo@bar/baz").unwrap(); match jid1.try_as_full() { Err(_) => (), other => panic!("unexpected result: {:?}", other), }; match jid2.try_as_full() { Ok(_) => (), other => panic!("unexpected result: {:?}", other), }; } #[test] fn lookup_jid_by_full_jid() { let mut map: BTreeSet = BTreeSet::new(); let jid1 = Jid::new("foo@bar").unwrap(); let jid2 = Jid::new("foo@bar/baz").unwrap(); let jid3 = FullJid::new("foo@bar/baz").unwrap(); map.insert(jid1); assert!(!map.contains(&jid2)); assert!(!map.contains(&jid3)); map.insert(jid2); assert!(map.contains(&jid3)); } #[test] fn lookup_full_jid_by_jid() { let mut map: BTreeSet = BTreeSet::new(); let jid1 = FullJid::new("foo@bar/baz").unwrap(); let jid2 = FullJid::new("foo@bar/fnord").unwrap(); let jid3 = Jid::new("foo@bar/fnord").unwrap(); map.insert(jid1); assert!(!map.contains(&jid2)); assert!(!map.contains(&jid3)); map.insert(jid2); assert!(map.contains(&jid3)); } #[test] fn lookup_bare_jid_by_jid() { let mut map: BTreeSet = BTreeSet::new(); let jid1 = BareJid::new("foo@bar").unwrap(); let jid2 = BareJid::new("foo@baz").unwrap(); let jid3 = Jid::new("foo@baz").unwrap(); map.insert(jid1); assert!(!map.contains(&jid2)); assert!(!map.contains(&jid3)); map.insert(jid2); assert!(map.contains(&jid3)); } #[test] fn normalizes_all_parts() { assert_eq!( Jid::new("ßA@IX.test/\u{2168}").unwrap().as_str(), "ssa@ix.test/IX" ); } #[test] fn rejects_unassigned_codepoints() { match Jid::new("\u{01f601}@example.com") { Err(Error::NodePrep) => (), other => panic!("unexpected result: {:?}", other), }; match Jid::new("foo@\u{01f601}.example.com") { Err(Error::NamePrep) => (), other => panic!("unexpected result: {:?}", other), }; match Jid::new("foo@example.com/\u{01f601}") { Err(Error::ResourcePrep) => (), other => panic!("unexpected result: {:?}", other), }; } #[test] fn accepts_domain_only_jid() { match Jid::new("example.com") { Ok(_) => (), other => panic!("unexpected result: {:?}", other), }; match BareJid::new("example.com") { Ok(_) => (), other => panic!("unexpected result: {:?}", other), }; match FullJid::new("example.com/x") { Ok(_) => (), other => panic!("unexpected result: {:?}", other), }; } #[test] fn is_bare_returns_true_iff_bare() { let bare = Jid::new("foo@bar").unwrap(); let full = Jid::new("foo@bar/baz").unwrap(); assert!(bare.is_bare()); assert!(!full.is_bare()); } #[test] fn is_full_returns_true_iff_full() { let bare = Jid::new("foo@bar").unwrap(); let full = Jid::new("foo@bar/baz").unwrap(); assert!(!bare.is_full()); assert!(full.is_full()); } #[test] fn reject_long_localpart() { let mut long = Vec::with_capacity(1028); long.resize(1024, b'a'); let mut long = String::from_utf8(long).unwrap(); long.push_str("@foo"); match Jid::new(&long) { Err(Error::NodeTooLong) => (), other => panic!("unexpected result: {:?}", other), } match BareJid::new(&long) { Err(Error::NodeTooLong) => (), other => panic!("unexpected result: {:?}", other), } } #[test] fn reject_long_domainpart() { let mut long = Vec::with_capacity(66); long.push(b'x'); long.push(b'@'); long.resize(66, b'a'); let long = String::from_utf8(long).unwrap(); Jid::new(&long).unwrap_err(); BareJid::new(&long).unwrap_err(); // A domain can be up to 253 bytes. let mut long = Vec::with_capacity(256); long.push(b'x'); long.push(b'@'); long.resize(65, b'a'); long.push(b'.'); long.resize(129, b'b'); long.push(b'.'); long.resize(193, b'c'); long.push(b'.'); long.resize(256, b'd'); let long = String::from_utf8(long).unwrap(); Jid::new(&long).unwrap_err(); BareJid::new(&long).unwrap_err(); } #[test] fn reject_long_resourcepart() { let mut long = Vec::with_capacity(1028); long.push(b'x'); long.push(b'@'); long.push(b'y'); long.push(b'/'); long.resize(1028, b'a'); let long = String::from_utf8(long).unwrap(); match Jid::new(&long) { Err(Error::ResourceTooLong) => (), other => panic!("unexpected result: {:?}", other), } match FullJid::new(&long) { Err(Error::ResourceTooLong) => (), other => panic!("unexpected result: {:?}", other), } } } jid-0.12.1/src/parts.rs000064400000000000000000000202751046102023000127470ustar 00000000000000use alloc::borrow::{Cow, ToOwned}; use alloc::string::{String, ToString}; use core::borrow::Borrow; use core::fmt; use core::mem; use core::ops::Deref; use core::str::FromStr; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; use crate::{domain_check, node_check, resource_check}; use crate::{BareJid, Error, Jid}; macro_rules! def_part_parse_doc { ($name:ident, $other:ident, $more:expr) => { concat!( "Parse a [`", stringify!($name), "`] from a `", stringify!($other), "`, copying its contents.\n", "\n", "If the given `", stringify!($other), "` does not conform to the restrictions imposed by `", stringify!($name), "`, an error is returned.\n", $more, ) }; } macro_rules! def_part_into_inner_doc { ($name:ident, $other:ident, $more:expr) => { concat!( "Consume the `", stringify!($name), "` and return the inner `", stringify!($other), "`.\n", $more, ) }; } macro_rules! def_part_types { ( $(#[$mainmeta:meta])* pub struct $name:ident(String) use $check_fn:ident(); $(#[$refmeta:meta])* pub struct ref $borrowed:ident(str); ) => { $(#[$mainmeta])* #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] #[repr(transparent)] pub struct $name(pub(crate) String); impl $name { #[doc = def_part_parse_doc!($name, str, "Depending on whether the contents are changed by normalisation operations, this function either returns a copy or a reference to the original data.")] #[allow(clippy::new_ret_no_self)] pub fn new(s: &str) -> Result, Error> { let part = $check_fn(s)?; match part { Cow::Borrowed(v) => Ok(Cow::Borrowed($borrowed::from_str_unchecked(v))), Cow::Owned(v) => Ok(Cow::Owned(Self(v))), } } #[doc = def_part_into_inner_doc!($name, String, "")] pub fn into_inner(self) -> String { self.0 } } impl FromStr for $name { type Err = Error; fn from_str(s: &str) -> Result { Ok(Self::new(s)?.into_owned()) } } impl fmt::Display for $name { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { <$borrowed as fmt::Display>::fmt(Borrow::<$borrowed>::borrow(self), f) } } impl Deref for $name { type Target = $borrowed; fn deref(&self) -> &Self::Target { Borrow::<$borrowed>::borrow(self) } } impl AsRef<$borrowed> for $name { fn as_ref(&self) -> &$borrowed { Borrow::<$borrowed>::borrow(self) } } impl AsRef for $name { fn as_ref(&self) -> &String { &self.0 } } impl Borrow<$borrowed> for $name { fn borrow(&self) -> &$borrowed { $borrowed::from_str_unchecked(self.0.as_str()) } } // useful for use in hashmaps impl Borrow for $name { fn borrow(&self) -> &String { &self.0 } } // useful for use in hashmaps impl Borrow for $name { fn borrow(&self) -> &str { self.0.as_str() } } impl<'x> TryFrom<&'x str> for $name { type Error = Error; fn try_from(s: &str) -> Result { Self::from_str(s) } } impl From<&$borrowed> for $name { fn from(other: &$borrowed) -> Self { other.to_owned() } } impl<'x> From> for $name { fn from(other: Cow<'x, $borrowed>) -> Self { other.into_owned() } } $(#[$refmeta])* #[repr(transparent)] #[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] pub struct $borrowed(pub(crate) str); impl $borrowed { pub(crate) fn from_str_unchecked(s: &str) -> &Self { // SAFETY: repr(transparent) thing can be transmuted to/from // its inner. unsafe { mem::transmute(s) } } /// Access the contents as [`str`] slice. pub fn as_str(&self) -> &str { &self.0 } } impl Deref for $borrowed { type Target = str; fn deref(&self) -> &Self::Target { &self.0 } } impl ToOwned for $borrowed { type Owned = $name; fn to_owned(&self) -> Self::Owned { $name(self.0.to_string()) } } impl AsRef for $borrowed { fn as_ref(&self) -> &str { &self.0 } } impl fmt::Display for $borrowed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", &self.0) } } } } def_part_types! { /// The [`NodePart`] is the optional part before the (optional) `@` in any /// [`Jid`][crate::Jid], whether [`BareJid`][crate::BareJid] or /// [`FullJid`][crate::FullJid]. /// /// The corresponding slice type is [`NodeRef`]. #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct NodePart(String) use node_check(); /// `str`-like type which conforms to the requirements of [`NodePart`]. /// /// See [`NodePart`] for details. #[cfg_attr(feature = "serde", derive(Serialize))] pub struct ref NodeRef(str); } def_part_types! { /// The [`DomainPart`] is the part between the (optional) `@` and the /// (optional) `/` in any [`Jid`][crate::Jid], whether /// [`BareJid`][crate::BareJid] or [`FullJid`][crate::FullJid]. #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct DomainPart(String) use domain_check(); /// `str`-like type which conforms to the requirements of [`DomainPart`]. /// /// See [`DomainPart`] for details. #[cfg_attr(feature = "serde", derive(Serialize))] pub struct ref DomainRef(str); } def_part_types! { /// The [`ResourcePart`] is the optional part after the `/` in a /// [`Jid`][crate::Jid]. It is mandatory in [`FullJid`][crate::FullJid]. #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ResourcePart(String) use resource_check(); /// `str`-like type which conforms to the requirements of /// [`ResourcePart`]. /// /// See [`ResourcePart`] for details. #[cfg_attr(feature = "serde", derive(Serialize))] pub struct ref ResourceRef(str); } impl DomainRef { /// Construct a bare JID (a JID without a resource) from this domain and /// the given node (local part). pub fn with_node(&self, node: &NodeRef) -> BareJid { BareJid::from_parts(Some(node), self) } } impl From for BareJid { fn from(other: DomainPart) -> Self { BareJid { inner: other.into(), } } } impl From for Jid { fn from(other: DomainPart) -> Self { Jid { normalized: other.0, at: None, slash: None, } } } impl<'x> From<&'x DomainRef> for BareJid { fn from(other: &'x DomainRef) -> Self { Self::from_parts(None, other) } } impl NodeRef { /// Construct a bare JID (a JID without a resource) from this node (the /// local part) and the given domain. pub fn with_domain(&self, domain: &DomainRef) -> BareJid { BareJid::from_parts(Some(self), domain) } } #[cfg(test)] mod tests { use super::*; #[test] fn nodepart_comparison() { let n1 = NodePart::new("foo").unwrap(); let n2 = NodePart::new("bar").unwrap(); let n3 = NodePart::new("foo").unwrap(); assert_eq!(n1, n3); assert_ne!(n1, n2); } }