Repository: mitsuhiko/rye
File: rye/src/cli/add.rs (Rust)

Code before:
use pep440_rs::VersionSpecifiers;
use pep508_rs::{Requirement, VersionOrUrl};
use serde::Deserialize;
use url::Url;

use crate::bootstrap::ensure_self_venv;
use crate::pyproject::{DependencyKind, PyProject};
use crate::utils::{format_requirement, CommandOutput};

const PACKAGE_FINDER_SCRIPT: &str = r#"
import sys
import json
from unearth.finder import PackageFinder

py_ver = tuple(map(int, sys.argv[1].split('.')))
package = sys.argv[2]
if len(sys.argv) > 3 and sys.argv[3] == "--pre=True":
    allow_prereleases = True
else:
    allow_prereleases = False

finder = PackageFinder(
    index_urls=["https://pypi.org/simple/"],
)
finder.target_python.py_ver = py_ver
m = finder.find_matches(package, allow_prereleases=allow_prereleases)
if not m:
    sys.exit(1)
print(json.dumps(m[0].as_json()))
"#;

#[derive(Deserialize, Debug)]
struct Match {
    name: String,
    version: String,
}

#[derive(Parser, Debug)]
pub struct ReqExtras {
    /// Install the given package from this git repository
    #[arg(long)]
    git: Option<String>,
    /// Install the given package from this URL
    #[arg(long, conflicts_with = "git", conflicts_with = "path")]
    url: Option<String>,
    /// Install the given package from this local path
    #[arg(long, conflicts_with = "git", conflicts_with = "url")]
    path: Option<PathBuf>,
    /// Force non interpolated absolute paths.
    #[arg(long, requires = "path")]
    absolute: bool,

Review comment (suggestion):
This would be nice but I can't seem to get it to work. A workaround for now would be 
```py
import sys
import json
from unearth.finder import PackageFinder
from packaging.version import Version

py_ver = tuple(map(int, sys.argv[1].split('.')))
package = sys.argv[2]
if len(sys.argv) > 3 and sys.argv[3] == "--pre=True":
    include_prerelease = True
else:
    include_prerelease = False

finder = PackageFinder(
    index_urls=["https://pypi.org/simple/"],
)
finder.target_python.py_ver = py_ver
m = finder.find_matches(package)
if not m:
    sys.exit(1)
if include_prerelease:
    print(json.dumps(m[0].as_json()))
else:
    # Filter out any pre-releases (TODO: next(filter(...)))
    m = [match for match in m if not Version(match.version).is_prerelease]
    print(json.dumps(m[0].as_json()))
```

Code after:
use pep440_rs::VersionSpecifiers;
use pep508_rs::{Requirement, VersionOrUrl};
use serde::Deserialize;
use url::Url;

use crate::bootstrap::ensure_self_venv;
use crate::pyproject::{DependencyKind, PyProject};
use crate::utils::{format_requirement, CommandOutput};

const PACKAGE_FINDER_SCRIPT: &str = r#"
import sys
import json
from unearth.finder import PackageFinder
from packaging.version import Version

py_ver = tuple(map(int, sys.argv[1].split('.')))
package = sys.argv[2]
pre = len(sys.argv) > 3 and sys.argv[3] == "--pre"

finder = PackageFinder(
    index_urls=["https://pypi.org/simple/"],
)
finder.target_python.py_ver = py_ver
choices = iter(finder.find_matches(package))
if not pre:
    choices = (m for m in choices if not Version(m.version).is_prerelease)

best = next(choices, None)
if best is None:
    sys.exit(1)
print(json.dumps(best.as_json()))

"#;

#[derive(Deserialize, Debug)]
struct Match {
    name: String,
    version: String,
}

#[derive(Parser, Debug)]
pub struct ReqExtras {
    /// Install the given package from this git repository
    #[arg(long)]
    git: Option<String>,
    /// Install the given package from this URL
    #[arg(long, conflicts_with = "git", conflicts_with = "path")]
    url: Option<String>,
    /// Install the given package from this local path
    #[arg(long, conflicts_with = "git", conflicts_with = "url")]
    path: Option<PathBuf>,