├── .github ├── dependabot.yml └── workflows │ └── ci.yml ├── .gitignore ├── CHANGELOG.md ├── Cargo.toml ├── LICENSE ├── README.md ├── rustfmt.toml ├── src ├── lib.rs ├── macros.rs └── url.rs └── tests ├── snapshots ├── tests__basic-2.snap ├── tests__basic-3.snap ├── tests__basic-4.snap ├── tests__basic.snap ├── tests__pep-10.snap ├── tests__pep-11.snap ├── tests__pep-12.snap ├── tests__pep-13.snap ├── tests__pep-14.snap ├── tests__pep-15.snap ├── tests__pep-2.snap ├── tests__pep-3.snap ├── tests__pep-4.snap ├── tests__pep-5.snap ├── tests__pep-6.snap ├── tests__pep-7.snap ├── tests__pep-8.snap ├── tests__pep-9.snap └── tests__pep.snap └── tests.rs /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | 3 | updates: 4 | - package-ecosystem: "cargo" 5 | directory: "/" 6 | schedule: 7 | interval: "daily" 8 | 9 | - package-ecosystem: "github-actions" 10 | directory: "/" 11 | schedule: 12 | interval: "daily" 13 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: ci 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | ci: 7 | name: ci 8 | runs-on: ubuntu-latest 9 | steps: 10 | - name: Checkout 11 | uses: actions/checkout@v4 12 | 13 | - name: "Cargo: test, clippy, fmt" 14 | run: | 15 | rustup toolchain install stable --profile minimal -c clippy 16 | rustup toolchain install nightly --profile minimal -c rustfmt 17 | cargo +stable test 18 | cargo +stable clippy -- -D warnings 19 | cargo +nightly fmt -- --check 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | Cargo.lock 3 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## v0.4.0 - 2023-11-19 4 | 5 | ### Fixes 6 | 7 | - Fix compatibility with chumsky 1.0.0-alpha.6 and pin chumsky version 8 | 9 | ## v0.3.0 - 2023-03-11 10 | 11 | ### Fixes 12 | 13 | - Fix compatibility with chumsky 1.0.0-alpha.3 14 | 15 | ## v0.2.0 - 2023-03-04 16 | 17 | ### Features 18 | 19 | - Zero copy parsing, `parse` now only accepts `&str`. 20 | 21 | ## v0.1.0 - 2023-02-18 22 | 23 | First release 24 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "pep-508" 3 | version = "0.4.0" 4 | authors = ["figsoda "] 5 | edition = "2021" 6 | description = "Python dependency parser for PEP 508" 7 | documentation = "https://docs.rs/pep-508" 8 | readme = "README.md" 9 | homepage = "https://github.com/figsoda/pep-508" 10 | repository = "https://github.com/figsoda/pep-508" 11 | license = "MPL-2.0" 12 | keywords = ["dependency", "parser", "pep508", "python", "requirements"] 13 | categories = ["parser-implementations"] 14 | 15 | [dependencies] 16 | chumsky = "=1.0.0-alpha.6" 17 | 18 | [dev-dependencies] 19 | insta = "1.34.0" 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pep-508 2 | 3 | Rust implementation of Python dependency parser for PEP 508 4 | 5 | [![version](https://img.shields.io/crates/v/pep-508?logo=rust&style=flat-square)](https://crates.io/crates/pep-508) 6 | [![deps](https://deps.rs/repo/github/figsoda/pep-508/status.svg?style=flat-square&compact=true)](https://deps.rs/repo/github/figsoda/pep-508) 7 | [![license](https://img.shields.io/badge/license-MPL--2.0-blue?style=flat-square)](https://www.mozilla.org/en-US/MPL/2.0) 8 | [![ci](https://img.shields.io/github/actions/workflow/status/figsoda/pep-508/ci.yml?label=ci&logo=github-actions&style=flat-square)](https://github.com/<>/actions/workflows/ci.yml) 9 | 10 | [Documentation](https://docs.rs/pep-508) 11 | 12 | ## Usage 13 | 14 | ```rust 15 | let dep = "requests[security, socks] <= 2.28.1, == 2.28.*; python_version > '3.7' and extra == 'http'"; 16 | let parsed = parse(dep).unwrap(); 17 | let expected = Dependency { 18 | name: "requests", 19 | extras: vec!["security", "socks"], 20 | spec: Some(Spec::Version(vec![ 21 | VersionSpec { 22 | comparator: Comparator::Le, 23 | version: "2.28.1", 24 | }, 25 | VersionSpec { 26 | comparator: Comparator::Eq, 27 | version: "2.28.*", 28 | }, 29 | ])), 30 | marker: Some(Marker::And( 31 | Box::new(Marker::Operator( 32 | Variable::PythonVersion, 33 | Operator::Comparator(Comparator::Gt), 34 | Variable::String("3.7"), 35 | )), 36 | Box::new(Marker::Operator( 37 | Variable::Extra, 38 | Operator::Comparator(Comparator::Eq), 39 | Variable::String("http"), 40 | )), 41 | )), 42 | }; 43 | assert_eq!(parsed, expected); 44 | ``` 45 | 46 | ## Changelog 47 | 48 | See [CHANGELOG.md](CHANGELOG.md) 49 | -------------------------------------------------------------------------------- /rustfmt.toml: -------------------------------------------------------------------------------- 1 | unstable_features = true 2 | 3 | condense_wildcard_suffixes = true 4 | format_code_in_doc_comments = true 5 | group_imports = "StdExternalCrate" 6 | imports_granularity = "Crate" 7 | newline_style = "Unix" 8 | normalize_comments = true 9 | normalize_doc_attributes = true 10 | reorder_impl_items = true 11 | spaces_around_ranges = true 12 | use_field_init_shorthand = true 13 | use_try_shorthand = true 14 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! Python dependency parser for [PEP 508](https://peps.python.org/pep-0508) 2 | //! 3 | //! ``` 4 | //! # use pep_508::*; 5 | //! let dep = "requests[security, socks] <= 2.28.1, == 2.28.*; python_version > '3.7' and extra == 'http'"; 6 | //! let parsed = parse(dep).unwrap(); 7 | //! let expected = Dependency { 8 | //! name: "requests", 9 | //! extras: vec!["security", "socks"], 10 | //! spec: Some(Spec::Version(vec![ 11 | //! VersionSpec { 12 | //! comparator: Comparator::Le, 13 | //! version: "2.28.1", 14 | //! }, 15 | //! VersionSpec { 16 | //! comparator: Comparator::Eq, 17 | //! version: "2.28.*", 18 | //! }, 19 | //! ])), 20 | //! marker: Some(Marker::And( 21 | //! Box::new(Marker::Operator( 22 | //! Variable::PythonVersion, 23 | //! Operator::Comparator(Comparator::Gt), 24 | //! Variable::String("3.7"), 25 | //! )), 26 | //! Box::new(Marker::Operator( 27 | //! Variable::Extra, 28 | //! Operator::Comparator(Comparator::Eq), 29 | //! Variable::String("http"), 30 | //! )), 31 | //! )), 32 | //! }; 33 | //! assert_eq!(parsed, expected); 34 | //! ``` 35 | mod macros; 36 | mod url; 37 | 38 | use chumsky::{ 39 | error::Error, 40 | extra::Full, 41 | prelude::Simple, 42 | primitive::{any, choice, empty, end, group, just}, 43 | recursive::recursive, 44 | IterParser, Parser, 45 | }; 46 | 47 | use crate::macros::set; 48 | 49 | /// Python dependency specified by [PEP 508](https://peps.python.org/pep-0508) 50 | #[derive(Clone, Debug, Eq, PartialEq)] 51 | pub struct Dependency<'a> { 52 | /// Name of the dependency 53 | pub name: &'a str, 54 | /// Extras for the dependency, things that go inside `[]` 55 | pub extras: Vec<&'a str>, 56 | /// Version specification or URL 57 | pub spec: Option>, 58 | /// Environment markers, conditions that go after `;` 59 | pub marker: Option>, 60 | } 61 | 62 | #[derive(Clone, Debug, Eq, PartialEq)] 63 | pub enum Spec<'a> { 64 | /// `foo @ https://example.com` 65 | Url(&'a str), 66 | /// `foo >= 0.1.0, < 0.2.0` 67 | Version(Vec>), 68 | } 69 | 70 | #[derive(Clone, Debug, Eq, PartialEq)] 71 | pub struct VersionSpec<'a> { 72 | pub comparator: Comparator, 73 | pub version: &'a str, 74 | } 75 | 76 | #[derive(Clone, Debug, Eq, PartialEq)] 77 | pub enum Marker<'a> { 78 | And(Box>, Box>), 79 | Or(Box>, Box>), 80 | Operator(Variable<'a>, Operator, Variable<'a>), 81 | } 82 | 83 | #[derive(Clone, Debug, Eq, PartialEq)] 84 | pub enum Variable<'a> { 85 | PythonVersion, 86 | PythonFullVersion, 87 | OsName, 88 | SysPlatform, 89 | PlatformRelease, 90 | PlatformSystem, 91 | PlatformVersion, 92 | PlatformMachine, 93 | PlatformPythonImplementation, 94 | ImplementationName, 95 | ImplementationVersion, 96 | Extra, 97 | String(&'a str), 98 | } 99 | 100 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] 101 | pub enum Operator { 102 | Comparator(Comparator), 103 | /// `in` 104 | In, 105 | /// `not in` 106 | NotIn, 107 | } 108 | 109 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] 110 | pub enum Comparator { 111 | /// `foo < '0.1.0'` 112 | Lt, 113 | /// `foo < '0.1.0'` 114 | Le, 115 | /// `foo != '0.1.0'` 116 | Ne, 117 | /// `foo == '0.1.0'` 118 | Eq, 119 | /// `foo >= '0.1.0'` 120 | Ge, 121 | /// `foo > '0.1.0'` 122 | Gt, 123 | /// `foo ~= '0.1.0'` 124 | Cp, 125 | /// `foo === '0.1.0'` 126 | Ae, 127 | } 128 | 129 | /// Parse a [PEP 508](https://peps.python.org/pep-0508) string into a [Dependency] 130 | /// ``` 131 | /// # use pep_508::parse; 132 | /// assert_eq!(parse("requests >= 2").unwrap().name, "requests"); 133 | /// assert_eq!(parse("numpy").unwrap().name, "numpy"); 134 | /// ``` 135 | pub fn parse(dependency: &str) -> Result>> { 136 | parser().then_ignore(end()).parse(dependency).into_result() 137 | } 138 | 139 | /// Create a [chumsky](https://docs.rs/chumsky) parser, 140 | /// allows more customization than [parse] 141 | pub fn parser<'a, E: Error<'a, &'a str> + 'a>( 142 | ) -> impl Parser<'a, &'a str, Dependency<'a>, Full> { 143 | let ws = set!(' ' | '\t').repeated().ignored(); 144 | let ident = any() 145 | .filter(char::is_ascii_alphanumeric) 146 | .then( 147 | set!('-' | '_' | '.') 148 | .or_not() 149 | .then(any().filter(char::is_ascii_alphanumeric)) 150 | .repeated(), 151 | ) 152 | .to_slice(); 153 | 154 | let cmp = choice(( 155 | just("===").to(Comparator::Ae), 156 | just("<=").to(Comparator::Le), 157 | just("!=").to(Comparator::Ne), 158 | just("==").to(Comparator::Eq), 159 | just(">=").to(Comparator::Ge), 160 | just("~=").to(Comparator::Cp), 161 | just('<').to(Comparator::Lt), 162 | just('>').to(Comparator::Gt), 163 | )); 164 | 165 | let version_spec = cmp 166 | .then_ignore(ws) 167 | .then( 168 | set!( 169 | 'A' ..= 'Z' | 'a' ..= 'z' | '0' ..= '9' | '-' | '_' | '.' | '*' | '+' | '!' 170 | ) 171 | .repeated() 172 | .at_least(1) 173 | .to_slice(), 174 | ) 175 | .map(|(comparator, version)| VersionSpec { 176 | comparator, 177 | version, 178 | }) 179 | .then_ignore(ws) 180 | .separated_by(just(',').ignore_then(ws)) 181 | .at_least(1) 182 | .collect(); 183 | 184 | group(( 185 | ws.ignore_then(ident).then_ignore(ws), 186 | ident 187 | .then_ignore(ws) 188 | .separated_by(just(',').ignore_then(ws)) 189 | .at_least(1) 190 | .collect() 191 | .delimited_by(just('[').ignore_then(ws), just(']')) 192 | .then_ignore(ws) 193 | .or(empty().map(|_| Vec::new())), 194 | just('@') 195 | .ignore_then(ws) 196 | .ignore_then(url::parser()) 197 | .map(Spec::Url) 198 | .or(version_spec 199 | .delimited_by(just('(').then_ignore(ws), just(')')) 200 | .or(version_spec) 201 | .map(Spec::Version)) 202 | .then_ignore(ws) 203 | .or_not(), 204 | just(';') 205 | .ignore_then(ws) 206 | .ignore_then(recursive(|marker_or| { 207 | macro_rules! c { 208 | () => { 209 | ' ' | '\t' | 'A' ..= 'Z' | 'a' ..= 'z' | '0' ..= '9' | '(' | ')' | '.' | 210 | '{' | '}' | '-' | '_' | '*' | '#' | ':' | ';' | ',' | '/' | '?' | '[' | 211 | ']' | '!' | '~' | '`' | '@' | '$' | '%' | '^' | '&' | '=' | '+' | '|' | 212 | '<' | '>' 213 | }; 214 | } 215 | 216 | let marker_var = choice(( 217 | just('\'') 218 | .ignore_then(set!(c!() | '"').repeated().to_slice()) 219 | .then_ignore(just('\'')) 220 | .map(Variable::String), 221 | just('"') 222 | .ignore_then(set!(c!() | '\'').repeated().to_slice()) 223 | .then_ignore(just('"')) 224 | .map(Variable::String), 225 | just("python_version").to(Variable::PythonVersion), 226 | just("python_full_version").to(Variable::PythonFullVersion), 227 | just("os_name").to(Variable::OsName), 228 | just("sys_platform").to(Variable::SysPlatform), 229 | just("platform_release").to(Variable::PlatformRelease), 230 | just("platform_system").to(Variable::PlatformSystem), 231 | just("platform_version").to(Variable::PlatformVersion), 232 | just("platform_machine").to(Variable::PlatformMachine), 233 | just("platform_python_implementation") 234 | .to(Variable::PlatformPythonImplementation), 235 | just("implementation_name").to(Variable::ImplementationName), 236 | just("implementation_version").to(Variable::ImplementationVersion), 237 | just("extra").to(Variable::Extra), 238 | )); 239 | 240 | let marker_expr = group(( 241 | marker_var.clone().then_ignore(ws), 242 | cmp.map(Operator::Comparator) 243 | .or(just("in").to(Operator::In).or(just("not") 244 | .ignore_then(set!(' ' | '\t').repeated().at_least(1)) 245 | .ignore_then(just("in")) 246 | .to(Operator::NotIn))) 247 | .then_ignore(ws), 248 | marker_var, 249 | )) 250 | .map(|(lhs, op, rhs)| Marker::Operator(lhs, op, rhs)) 251 | .or(marker_or 252 | .then_ignore(ws) 253 | .delimited_by(just('(').then_ignore(ws), just(')'))); 254 | 255 | let marker_and = marker_expr 256 | .clone() 257 | .then( 258 | ws.ignore_then(just("and")) 259 | .ignore_then(ws) 260 | .ignore_then(marker_expr) 261 | .or_not(), 262 | ) 263 | .map(|(lhs, rhs)| match rhs { 264 | Some(rhs) => Marker::And(Box::new(lhs), Box::new(rhs)), 265 | None => lhs, 266 | }); 267 | 268 | marker_and 269 | .clone() 270 | .then( 271 | ws.ignore_then(just("or")) 272 | .ignore_then(ws) 273 | .ignore_then(marker_and) 274 | .or_not(), 275 | ) 276 | .map(|(lhs, rhs)| match rhs { 277 | Some(rhs) => Marker::Or(Box::new(lhs), Box::new(rhs)), 278 | None => lhs, 279 | }) 280 | })) 281 | .or_not(), 282 | )) 283 | .then_ignore(ws) 284 | .map(|(name, extras, spec, marker)| Dependency { 285 | name, 286 | extras, 287 | spec, 288 | marker, 289 | }) 290 | } 291 | -------------------------------------------------------------------------------- /src/macros.rs: -------------------------------------------------------------------------------- 1 | macro_rules! set { 2 | ($($tt:tt)+) => { 3 | ::chumsky::primitive::any().filter(|c| matches!(c, $($tt)+)) 4 | }; 5 | } 6 | 7 | pub(crate) use set; 8 | -------------------------------------------------------------------------------- /src/url.rs: -------------------------------------------------------------------------------- 1 | use chumsky::{ 2 | error::Error, 3 | extra::Full, 4 | primitive::{any, choice, group, just}, 5 | Parser, 6 | }; 7 | 8 | use crate::macros::set; 9 | 10 | macro_rules! c { 11 | () => { 12 | 'A' ..= 'Z' | 'a' ..= 'z' | '0' ..= '9' | '-' | '.' | '_' | '~' | 13 | '!' | '$' | '&' | '\'' | '(' | ')' | '*' | '+' | ',' | ';' | '=' 14 | }; 15 | } 16 | 17 | pub(crate) fn parser<'a, E: Error<'a, &'a str> + 'a>( 18 | ) -> impl Parser<'a, &'a str, &'a str, Full> { 19 | let c = set!(c!()); 20 | let digit = any().filter(char::is_ascii_digit); 21 | let hex = any().filter(char::is_ascii_hexdigit); 22 | let percent = just('%').then_ignore(hex.repeated().exactly(2).rewind()); 23 | let reg = percent.or(c); 24 | let pchar = percent.or(set!(c!() | ':' | '@')); 25 | 26 | let octet = choice(( 27 | group((just('1'), digit, digit)).ignored(), 28 | just('2') 29 | .then( 30 | set!('0' ..= '4') 31 | .then(digit) 32 | .or(just('5').then(set!('0' ..= '5'))), 33 | ) 34 | .ignored(), 35 | set!('1' ..= '9').then(digit).ignored(), 36 | digit.map(|c| vec![c]).ignored(), 37 | )); 38 | let ipv4 = group((octet, just('.'), octet, just('.'), octet, just('.'), octet)).ignored(); 39 | let h16 = hex.repeated().at_least(1).at_most(4); 40 | let h16r = just(':').then(h16).repeated(); 41 | let ls32 = group((h16, just(':'), h16)).ignored().or(ipv4); 42 | 43 | let segments = just('/').then(pchar.repeated()).repeated(); 44 | let frag = percent.or(set!(c!() | ':' | '@' | '/' | '?')).repeated(); 45 | let frags = just('?') 46 | .then(frag) 47 | .or_not() 48 | .then(just('#').then(frag).or_not()); 49 | 50 | let path = just('/') 51 | .then( 52 | group(( 53 | just('/'), 54 | percent 55 | .or(set!(c!() | ':')) 56 | .repeated() 57 | .then(just('@')) 58 | .or_not(), 59 | choice(( 60 | just('[') 61 | .then(choice(( 62 | group(( 63 | just('v'), 64 | hex.repeated().at_least(1), 65 | just('.'), 66 | set!(c!() | ':').repeated().at_least(1), 67 | )) 68 | .ignored(), 69 | h16.then(just(':')) 70 | .repeated() 71 | .exactly(6) 72 | .then(ls32) 73 | .ignored(), 74 | group((just("::"), h16r.exactly(5), ls32)).ignored(), 75 | group((h16.or_not(), just("::"), h16r.exactly(4), ls32)).ignored(), 76 | group(( 77 | h16.then(just(':')).or_not().then(h16).or_not(), 78 | just("::"), 79 | h16r.exactly(3), 80 | ls32, 81 | )) 82 | .ignored(), 83 | group(( 84 | h16.then(h16r.at_most(2)).or_not(), 85 | just(':'), 86 | just(':'), 87 | h16r.exactly(2), 88 | ls32, 89 | )) 90 | .ignored(), 91 | group(( 92 | h16.then(h16r.at_most(3)).or_not(), 93 | just("::"), 94 | h16, 95 | just(':'), 96 | ls32, 97 | )) 98 | .ignored(), 99 | group((h16.then(h16r.at_most(4)).or_not(), just("::"), ls32)).ignored(), 100 | group((h16.then(h16r.at_most(5)).or_not(), just("::"), h16)).ignored(), 101 | h16.then(h16r.at_most(6)) 102 | .or_not() 103 | .then(just("::")) 104 | .ignored(), 105 | ))) 106 | .then(just(']')) 107 | .ignored(), 108 | ipv4, 109 | reg.repeated(), 110 | )), 111 | just(':').then(digit.repeated()).or_not(), 112 | segments, 113 | )) 114 | .ignored() 115 | .or(pchar 116 | .repeated() 117 | .at_least(1) 118 | .then(segments) 119 | .or_not() 120 | .ignored()) 121 | .or_not(), 122 | ) 123 | .ignored(); 124 | 125 | choice(( 126 | group(( 127 | any().filter(char::is_ascii_alphabetic), 128 | set!('A' ..= 'Z' | 'a' ..= 'z' | '0' ..= '9' | '+' | '-' | '.').repeated(), 129 | just(':'), 130 | path.or(pchar.repeated().at_least(1).then(segments).ignored()), 131 | )) 132 | .ignored(), 133 | path, 134 | percent 135 | .or(set!(c!() | '@')) 136 | .repeated() 137 | .at_least(1) 138 | .then(segments) 139 | .ignored(), 140 | )) 141 | .then(frags) 142 | .to_slice() 143 | } 144 | 145 | #[cfg(test)] 146 | mod tests { 147 | use chumsky::{prelude::Rich, primitive::end, Parser}; 148 | 149 | use super::parser; 150 | 151 | fn parse(s: &str) -> Result<&str, Vec>> { 152 | parser().then_ignore(end()).parse(s).into_result() 153 | } 154 | 155 | fn check(urls: impl IntoIterator>) { 156 | for url in urls { 157 | let url = url.as_ref(); 158 | assert_eq!(url, parse(url).expect(url)); 159 | } 160 | } 161 | 162 | #[test] 163 | fn basic() { 164 | check([ 165 | "https://github.com/figsoda/pep-508", 166 | "https://crates.io/search?q=pep-508&sort=recent-downloads", 167 | "http://127.0.0.1:8000?some=query#anchor", 168 | "/relative/url?query=good", 169 | "another/relative/url#this", 170 | ]); 171 | 172 | assert!(parse("").is_err()); 173 | assert!(parse("https://example.com ").is_err()); 174 | } 175 | 176 | // examples from https://datatracker.ietf.org/doc/html/rfc3986.htm 177 | #[test] 178 | fn examples() { 179 | check([ 180 | "ftp://ftp.is.co.za/rfc/rfc1808.txt", 181 | "http://www.ietf.org/rfc/rfc2396.txt", 182 | "ldap://[2001:db8::7]/c=GB?objectClass?one", 183 | "mailto:John.Doe@example.com", 184 | "news:comp.infosystems.www.servers.unix", 185 | "tel:+1-816-555-1212", 186 | "telnet://192.0.2.16:80/", 187 | "urn:oasis:names:specification:docbook:dtd:xml:4.1.2", 188 | ]); 189 | } 190 | 191 | #[test] 192 | fn ipv6() { 193 | check([ 194 | "https://[::]", 195 | "https://[::1]", 196 | "https://[0:0:0:0:0:0:0:0]", 197 | "https://[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]", 198 | ]); 199 | 200 | assert!(parse("[::]").is_err()); 201 | assert!(parse("https://[:::1]").is_err()); 202 | assert!(parse("https://[ffff:ffff:ffff:ffff:ffff:ffff:ffff]").is_err()); 203 | assert!(parse("https://[0:0:0:0:0:0:0:0:0]").is_err()); 204 | } 205 | } 206 | -------------------------------------------------------------------------------- /tests/snapshots/tests__basic-2.snap: -------------------------------------------------------------------------------- 1 | --- 2 | source: tests/tests.rs 3 | expression: "parse(\"beautifulsoup4\")" 4 | --- 5 | Ok( 6 | Dependency { 7 | name: "beautifulsoup4", 8 | extras: [], 9 | spec: None, 10 | marker: None, 11 | }, 12 | ) 13 | -------------------------------------------------------------------------------- /tests/snapshots/tests__basic-3.snap: -------------------------------------------------------------------------------- 1 | --- 2 | source: tests/tests.rs 3 | expression: "parse(\"Click>=7.0\")" 4 | --- 5 | Ok( 6 | Dependency { 7 | name: "Click", 8 | extras: [], 9 | spec: Some( 10 | Version( 11 | [ 12 | VersionSpec { 13 | comparator: Ge, 14 | version: "7.0", 15 | }, 16 | ], 17 | ), 18 | ), 19 | marker: None, 20 | }, 21 | ) 22 | -------------------------------------------------------------------------------- /tests/snapshots/tests__basic-4.snap: -------------------------------------------------------------------------------- 1 | --- 2 | source: tests/tests.rs 3 | expression: "parse(\"tomli;python_version<'3.11'\")" 4 | --- 5 | Ok( 6 | Dependency { 7 | name: "tomli", 8 | extras: [], 9 | spec: None, 10 | marker: Some( 11 | Operator( 12 | PythonVersion, 13 | Comparator( 14 | Lt, 15 | ), 16 | String( 17 | "3.11", 18 | ), 19 | ), 20 | ), 21 | }, 22 | ) 23 | -------------------------------------------------------------------------------- /tests/snapshots/tests__basic.snap: -------------------------------------------------------------------------------- 1 | --- 2 | source: tests/tests.rs 3 | expression: "parse(\"requests\")" 4 | --- 5 | Ok( 6 | Dependency { 7 | name: "requests", 8 | extras: [], 9 | spec: None, 10 | marker: None, 11 | }, 12 | ) 13 | -------------------------------------------------------------------------------- /tests/snapshots/tests__pep-10.snap: -------------------------------------------------------------------------------- 1 | --- 2 | source: tests/tests.rs 3 | expression: "parse(\"name[quux, strange];python_version<'3.7' and platform_version=='2'\")" 4 | --- 5 | Ok( 6 | Dependency { 7 | name: "name", 8 | extras: [ 9 | "quux", 10 | "strange", 11 | ], 12 | spec: None, 13 | marker: Some( 14 | And( 15 | Operator( 16 | PythonVersion, 17 | Comparator( 18 | Lt, 19 | ), 20 | String( 21 | "3.7", 22 | ), 23 | ), 24 | Operator( 25 | PlatformVersion, 26 | Comparator( 27 | Eq, 28 | ), 29 | String( 30 | "2", 31 | ), 32 | ), 33 | ), 34 | ), 35 | }, 36 | ) 37 | -------------------------------------------------------------------------------- /tests/snapshots/tests__pep-11.snap: -------------------------------------------------------------------------------- 1 | --- 2 | source: tests/tests.rs 3 | expression: "parse(\"name; os_name=='a' or os_name=='b'\")" 4 | --- 5 | Ok( 6 | Dependency { 7 | name: "name", 8 | extras: [], 9 | spec: None, 10 | marker: Some( 11 | Or( 12 | Operator( 13 | OsName, 14 | Comparator( 15 | Eq, 16 | ), 17 | String( 18 | "a", 19 | ), 20 | ), 21 | Operator( 22 | OsName, 23 | Comparator( 24 | Eq, 25 | ), 26 | String( 27 | "b", 28 | ), 29 | ), 30 | ), 31 | ), 32 | }, 33 | ) 34 | -------------------------------------------------------------------------------- /tests/snapshots/tests__pep-12.snap: -------------------------------------------------------------------------------- 1 | --- 2 | source: tests/tests.rs 3 | expression: "parse(\"name; os_name=='a' and os_name=='b' or os_name=='c'\")" 4 | --- 5 | Ok( 6 | Dependency { 7 | name: "name", 8 | extras: [], 9 | spec: None, 10 | marker: Some( 11 | Or( 12 | And( 13 | Operator( 14 | OsName, 15 | Comparator( 16 | Eq, 17 | ), 18 | String( 19 | "a", 20 | ), 21 | ), 22 | Operator( 23 | OsName, 24 | Comparator( 25 | Eq, 26 | ), 27 | String( 28 | "b", 29 | ), 30 | ), 31 | ), 32 | Operator( 33 | OsName, 34 | Comparator( 35 | Eq, 36 | ), 37 | String( 38 | "c", 39 | ), 40 | ), 41 | ), 42 | ), 43 | }, 44 | ) 45 | -------------------------------------------------------------------------------- /tests/snapshots/tests__pep-13.snap: -------------------------------------------------------------------------------- 1 | --- 2 | source: tests/tests.rs 3 | expression: "parse(\"name; os_name=='a' and (os_name=='b' or os_name=='c')\")" 4 | --- 5 | Ok( 6 | Dependency { 7 | name: "name", 8 | extras: [], 9 | spec: None, 10 | marker: Some( 11 | And( 12 | Operator( 13 | OsName, 14 | Comparator( 15 | Eq, 16 | ), 17 | String( 18 | "a", 19 | ), 20 | ), 21 | Or( 22 | Operator( 23 | OsName, 24 | Comparator( 25 | Eq, 26 | ), 27 | String( 28 | "b", 29 | ), 30 | ), 31 | Operator( 32 | OsName, 33 | Comparator( 34 | Eq, 35 | ), 36 | String( 37 | "c", 38 | ), 39 | ), 40 | ), 41 | ), 42 | ), 43 | }, 44 | ) 45 | -------------------------------------------------------------------------------- /tests/snapshots/tests__pep-14.snap: -------------------------------------------------------------------------------- 1 | --- 2 | source: tests/tests.rs 3 | expression: "parse(\"name; os_name=='a' or os_name=='b' and os_name=='c'\")" 4 | --- 5 | Ok( 6 | Dependency { 7 | name: "name", 8 | extras: [], 9 | spec: None, 10 | marker: Some( 11 | Or( 12 | Operator( 13 | OsName, 14 | Comparator( 15 | Eq, 16 | ), 17 | String( 18 | "a", 19 | ), 20 | ), 21 | And( 22 | Operator( 23 | OsName, 24 | Comparator( 25 | Eq, 26 | ), 27 | String( 28 | "b", 29 | ), 30 | ), 31 | Operator( 32 | OsName, 33 | Comparator( 34 | Eq, 35 | ), 36 | String( 37 | "c", 38 | ), 39 | ), 40 | ), 41 | ), 42 | ), 43 | }, 44 | ) 45 | -------------------------------------------------------------------------------- /tests/snapshots/tests__pep-15.snap: -------------------------------------------------------------------------------- 1 | --- 2 | source: tests/tests.rs 3 | expression: "parse(\"name; (os_name=='a' or os_name=='b') and os_name=='c'\")" 4 | --- 5 | Ok( 6 | Dependency { 7 | name: "name", 8 | extras: [], 9 | spec: None, 10 | marker: Some( 11 | And( 12 | Or( 13 | Operator( 14 | OsName, 15 | Comparator( 16 | Eq, 17 | ), 18 | String( 19 | "a", 20 | ), 21 | ), 22 | Operator( 23 | OsName, 24 | Comparator( 25 | Eq, 26 | ), 27 | String( 28 | "b", 29 | ), 30 | ), 31 | ), 32 | Operator( 33 | OsName, 34 | Comparator( 35 | Eq, 36 | ), 37 | String( 38 | "c", 39 | ), 40 | ), 41 | ), 42 | ), 43 | }, 44 | ) 45 | -------------------------------------------------------------------------------- /tests/snapshots/tests__pep-2.snap: -------------------------------------------------------------------------------- 1 | --- 2 | source: tests/tests.rs 3 | expression: "parse(\"A.B-C_D\")" 4 | --- 5 | Ok( 6 | Dependency { 7 | name: "A.B-C_D", 8 | extras: [], 9 | spec: None, 10 | marker: None, 11 | }, 12 | ) 13 | -------------------------------------------------------------------------------- /tests/snapshots/tests__pep-3.snap: -------------------------------------------------------------------------------- 1 | --- 2 | source: tests/tests.rs 3 | expression: "parse(\"aa\")" 4 | --- 5 | Ok( 6 | Dependency { 7 | name: "aa", 8 | extras: [], 9 | spec: None, 10 | marker: None, 11 | }, 12 | ) 13 | -------------------------------------------------------------------------------- /tests/snapshots/tests__pep-4.snap: -------------------------------------------------------------------------------- 1 | --- 2 | source: tests/tests.rs 3 | expression: "parse(\"name\")" 4 | --- 5 | Ok( 6 | Dependency { 7 | name: "name", 8 | extras: [], 9 | spec: None, 10 | marker: None, 11 | }, 12 | ) 13 | -------------------------------------------------------------------------------- /tests/snapshots/tests__pep-5.snap: -------------------------------------------------------------------------------- 1 | --- 2 | source: tests/tests.rs 3 | expression: "parse(\"name<=1\")" 4 | --- 5 | Ok( 6 | Dependency { 7 | name: "name", 8 | extras: [], 9 | spec: Some( 10 | Version( 11 | [ 12 | VersionSpec { 13 | comparator: Le, 14 | version: "1", 15 | }, 16 | ], 17 | ), 18 | ), 19 | marker: None, 20 | }, 21 | ) 22 | -------------------------------------------------------------------------------- /tests/snapshots/tests__pep-6.snap: -------------------------------------------------------------------------------- 1 | --- 2 | source: tests/tests.rs 3 | expression: "parse(\"name>=4\")" 4 | --- 5 | Ok( 6 | Dependency { 7 | name: "name", 8 | extras: [], 9 | spec: Some( 10 | Version( 11 | [ 12 | VersionSpec { 13 | comparator: Ge, 14 | version: "4", 15 | }, 16 | ], 17 | ), 18 | ), 19 | marker: None, 20 | }, 21 | ) 22 | -------------------------------------------------------------------------------- /tests/snapshots/tests__pep-7.snap: -------------------------------------------------------------------------------- 1 | --- 2 | source: tests/tests.rs 3 | expression: "parse(\"name>=3,<2\")" 4 | --- 5 | Ok( 6 | Dependency { 7 | name: "name", 8 | extras: [], 9 | spec: Some( 10 | Version( 11 | [ 12 | VersionSpec { 13 | comparator: Ge, 14 | version: "3", 15 | }, 16 | VersionSpec { 17 | comparator: Lt, 18 | version: "2", 19 | }, 20 | ], 21 | ), 22 | ), 23 | marker: None, 24 | }, 25 | ) 26 | -------------------------------------------------------------------------------- /tests/snapshots/tests__pep-8.snap: -------------------------------------------------------------------------------- 1 | --- 2 | source: tests/tests.rs 3 | expression: "parse(\"name@http://foo.com\")" 4 | --- 5 | Ok( 6 | Dependency { 7 | name: "name", 8 | extras: [], 9 | spec: Some( 10 | Url( 11 | "http://foo.com", 12 | ), 13 | ), 14 | marker: None, 15 | }, 16 | ) 17 | -------------------------------------------------------------------------------- /tests/snapshots/tests__pep-9.snap: -------------------------------------------------------------------------------- 1 | --- 2 | source: tests/tests.rs 3 | expression: "parse(\"name [fred,bar] @ http://foo.com ; python_version=='3.7'\")" 4 | --- 5 | Ok( 6 | Dependency { 7 | name: "name", 8 | extras: [ 9 | "fred", 10 | "bar", 11 | ], 12 | spec: Some( 13 | Url( 14 | "http://foo.com", 15 | ), 16 | ), 17 | marker: Some( 18 | Operator( 19 | PythonVersion, 20 | Comparator( 21 | Eq, 22 | ), 23 | String( 24 | "3.7", 25 | ), 26 | ), 27 | ), 28 | }, 29 | ) 30 | -------------------------------------------------------------------------------- /tests/snapshots/tests__pep.snap: -------------------------------------------------------------------------------- 1 | --- 2 | source: tests/tests.rs 3 | expression: "parse(\"A\")" 4 | --- 5 | Ok( 6 | Dependency { 7 | name: "A", 8 | extras: [], 9 | spec: None, 10 | marker: None, 11 | }, 12 | ) 13 | -------------------------------------------------------------------------------- /tests/tests.rs: -------------------------------------------------------------------------------- 1 | use insta::assert_debug_snapshot; 2 | use pep_508::parse; 3 | 4 | #[test] 5 | fn basic() { 6 | assert_debug_snapshot!(parse("requests")); 7 | assert_debug_snapshot!(parse("beautifulsoup4")); 8 | assert_debug_snapshot!(parse("Click>=7.0")); 9 | assert_debug_snapshot!(parse("tomli;python_version<'3.11'")); 10 | 11 | assert!(parse("").is_err()); 12 | assert!(parse("# comment").is_err()) 13 | } 14 | 15 | // tests from the PEP 16 | #[test] 17 | fn pep() { 18 | assert_debug_snapshot!(parse("A")); 19 | assert_debug_snapshot!(parse("A.B-C_D")); 20 | assert_debug_snapshot!(parse("aa")); 21 | assert_debug_snapshot!(parse("name")); 22 | assert_debug_snapshot!(parse("name<=1")); 23 | assert_debug_snapshot!(parse("name>=4")); 24 | assert_debug_snapshot!(parse("name>=3,<2")); 25 | assert_debug_snapshot!(parse("name@http://foo.com")); 26 | assert_debug_snapshot!(parse( 27 | "name [fred,bar] @ http://foo.com ; python_version=='3.7'" 28 | )); 29 | assert_debug_snapshot!(parse( 30 | "name[quux, strange];python_version<'3.7' and platform_version=='2'" 31 | )); 32 | assert_debug_snapshot!(parse("name; os_name=='a' or os_name=='b'")); 33 | assert_debug_snapshot!(parse("name; os_name=='a' and os_name=='b' or os_name=='c'")); 34 | assert_debug_snapshot!(parse( 35 | "name; os_name=='a' and (os_name=='b' or os_name=='c')" 36 | )); 37 | assert_debug_snapshot!(parse("name; os_name=='a' or os_name=='b' and os_name=='c'")); 38 | assert_debug_snapshot!(parse( 39 | "name; (os_name=='a' or os_name=='b') and os_name=='c'" 40 | )); 41 | } 42 | --------------------------------------------------------------------------------