From 7582ce73cb9ec376be4b5bafdc70da4d60ad7255 Mon Sep 17 00:00:00 2001 From: Chris Pryer <14341145+cnpryer@users.noreply.github.com> Date: Sat, 28 Oct 2023 20:01:43 -0400 Subject: [PATCH] Macro declarations after comments (#799) --- crates/huak-cli/src/cli.rs | 20 +++++++++++++ crates/huak-cli/src/main.rs | 2 +- crates/huak-home/src/lib.rs | 2 +- crates/huak-package-manager/src/config.rs | 8 +++--- crates/huak-package-manager/src/dependency.rs | 6 ++-- .../huak-package-manager/src/environment.rs | 10 +++---- crates/huak-package-manager/src/metadata.rs | 8 +++--- crates/huak-package-manager/src/ops/python.rs | 2 +- crates/huak-package-manager/src/package.rs | 12 ++++---- .../src/python_environment.rs | 28 +++++++++---------- crates/huak-package-manager/src/version.rs | 2 +- crates/huak-package-manager/src/workspace.rs | 4 +-- crates/huak-python-manager/src/cli.rs | 3 +- crates/huak-python-manager/src/releases.rs | 2 +- crates/huak-python-manager/src/resolve.rs | 4 +-- 15 files changed, 66 insertions(+), 47 deletions(-) diff --git a/crates/huak-cli/src/cli.rs b/crates/huak-cli/src/cli.rs index 9714fc6b..3a378ba5 100644 --- a/crates/huak-cli/src/cli.rs +++ b/crates/huak-cli/src/cli.rs @@ -185,6 +185,26 @@ enum Python { }, } +#[derive(Subcommand)] +enum Toolchain { + /// List available toolchains. + List, + /// Use an available toolchain. + Use { + /// The version of Python to use. + #[arg(required = true)] + version: RequestedVersion, + }, + /// Install a toolchain. + Install { + /// The version of Python to install. + #[arg(required = true)] + version: RequestedVersion, + /// The path to install a toolchain to. + target: PathBuf, + }, +} + // Command gating for Huak. impl Cli { pub fn run(self) -> CliResult { diff --git a/crates/huak-cli/src/main.rs b/crates/huak-cli/src/main.rs index ff4fb2c7..a6d2d2ef 100644 --- a/crates/huak-cli/src/main.rs +++ b/crates/huak-cli/src/main.rs @@ -11,8 +11,8 @@ use std::process::{exit, ExitCode}; mod error; -#[must_use] /// Launch Huak's cli process. +#[must_use] pub fn main() -> ExitCode { setup_panic!(); diff --git a/crates/huak-home/src/lib.rs b/crates/huak-home/src/lib.rs index 717451ed..257f8817 100644 --- a/crates/huak-home/src/lib.rs +++ b/crates/huak-home/src/lib.rs @@ -1,6 +1,5 @@ use std::path::PathBuf; -#[must_use] /// Huak's home directory is located at ~/.huak. /// /// # Unix @@ -10,6 +9,7 @@ use std::path::PathBuf; /// # Windows /// /// On windows the `USERPROFILE` environment variable is used if it exists. +#[must_use] pub fn huak_home_dir() -> Option { home_dir().map(|p| p.join(".huak")) } diff --git a/crates/huak-package-manager/src/config.rs b/crates/huak-package-manager/src/config.rs index ee8795d0..93cb4b6e 100644 --- a/crates/huak-package-manager/src/config.rs +++ b/crates/huak-package-manager/src/config.rs @@ -2,7 +2,6 @@ use std::path::PathBuf; use crate::{sys::Terminal, workspace::Workspace, TerminalOptions}; -#[derive(Clone, Default)] /// The main `Config` for Huak. /// /// The `Config` contains data telling Huak what to do at times. @@ -22,6 +21,7 @@ use crate::{sys::Terminal, workspace::Workspace, TerminalOptions}; /// /// let workspace = config.workspace(); /// ``` +#[derive(Clone, Default)] pub struct Config { /// The configured `Workspace` root path. pub workspace_root: PathBuf, @@ -32,20 +32,20 @@ pub struct Config { } impl Config { - #[must_use] /// Resolve the current `Workspace` based on the `Config` data. + #[must_use] pub fn workspace(&self) -> Workspace { Workspace::new(&self.workspace_root, self) } - #[must_use] /// Get a `Terminal` based on the `Config` data. + #[must_use] pub fn terminal(&self) -> Terminal { Terminal::from_options(self.terminal_options.clone()) } - #[must_use] /// Get a `Config` with a new `Terminal`. + #[must_use] pub fn with_terminal(self, terminal_options: TerminalOptions) -> Self { Config { workspace_root: self.workspace_root, diff --git a/crates/huak-package-manager/src/dependency.rs b/crates/huak-package-manager/src/dependency.rs index 9ebc5b5d..fe8e5ecc 100644 --- a/crates/huak-package-manager/src/dependency.rs +++ b/crates/huak-package-manager/src/dependency.rs @@ -5,7 +5,6 @@ use pep508_rs::{Requirement, VersionOrUrl}; use crate::Error; -#[derive(Clone, Debug)] /// The `Dependency` is an abstraction for `Package` data used as a cheap alternative /// for operations on lots of `Package` data. /// @@ -17,11 +16,12 @@ use crate::Error; /// /// let dependency = Dependency::from_str("my-dependency >= 0.1.0, < 0.2.0").unwrap(); /// ``` +#[derive(Clone, Debug)] pub struct Dependency(Requirement); impl Dependency { - #[must_use] /// Get a reference to the wrapped `Requirement`. + #[must_use] pub fn requirement(&self) -> &Requirement { &self.0 } @@ -31,8 +31,8 @@ impl Dependency { &mut self.0 } - #[must_use] /// Get the `Dependency` name. + #[must_use] pub fn name(&self) -> &str { &self.requirement().name } diff --git a/crates/huak-package-manager/src/environment.rs b/crates/huak-package-manager/src/environment.rs index 8c3c650b..35966bee 100644 --- a/crates/huak-package-manager/src/environment.rs +++ b/crates/huak-package-manager/src/environment.rs @@ -22,8 +22,8 @@ pub struct Environment { } impl Environment { - #[must_use] /// Initialize an `Environment`. + #[must_use] pub fn new() -> Environment { let interpreters = Environment::resolve_python_interpreters(); @@ -38,8 +38,8 @@ impl Environment { .map(Interpreter::path) } - #[must_use] /// Resolve `Interpreters` for the `Environment`. + #[must_use] pub fn resolve_python_interpreters() -> Interpreters { // Note that we filter out any interpreters we can't establish a `Version` for. let interpreters = python_paths().filter_map(|(version, path)| { @@ -57,8 +57,8 @@ impl Environment { Interpreters::new(interpreters) } - #[must_use] /// Get a reference to the environment's resolved Python interpreters. + #[must_use] pub fn interpreters(&self) -> &Interpreters { &self.interpreters } @@ -70,8 +70,8 @@ impl Default for Environment { } } -#[must_use] /// Get a vector of paths from the system `PATH` environment variable. +#[must_use] pub fn env_path_values() -> Option> { if let Some(val) = env_path_string() { return Some(std::env::split_paths(&val).collect()); @@ -80,8 +80,8 @@ pub fn env_path_values() -> Option> { None } -#[must_use] /// Get the `OsString` value of the enrionment variable `PATH`. +#[must_use] pub fn env_path_string() -> Option { std::env::var_os("PATH") } diff --git a/crates/huak-package-manager/src/metadata.rs b/crates/huak-package-manager/src/metadata.rs index 9c4ee6ae..ad26b1e7 100644 --- a/crates/huak-package-manager/src/metadata.rs +++ b/crates/huak-package-manager/src/metadata.rs @@ -11,9 +11,9 @@ use crate::{dependency::Dependency, Error, HuakResult}; const DEFAULT_METADATA_FILE_NAME: &str = "pyproject.toml"; -#[derive(Debug)] /// A `LocalMetadata` struct used to manage local `Metadata` files such as /// the pyproject.toml (). +#[derive(Debug)] pub struct LocalMetadata { /// The core `Metadata`. /// See https://packaging.python.org/en/latest/specifications/core-metadata/. @@ -55,8 +55,8 @@ impl LocalMetadata { } } - #[must_use] /// Get a reference to the core `Metadata`. + #[must_use] pub fn metadata(&self) -> &Metadata { &self.metadata } @@ -111,11 +111,11 @@ fn pyproject_toml_metadata>(path: T) -> HuakResult for more about core metadata. +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(rename_all = "kebab-case")] pub struct Metadata { /// The build system used for the `Package`. build_system: BuildSystem, diff --git a/crates/huak-package-manager/src/ops/python.rs b/crates/huak-package-manager/src/ops/python.rs index 9abc83c0..a081c61e 100644 --- a/crates/huak-package-manager/src/ops/python.rs +++ b/crates/huak-package-manager/src/ops/python.rs @@ -27,8 +27,8 @@ pub fn use_python(version: &str, config: &Config) -> HuakResult<()> { let interpreters = Environment::resolve_python_interpreters(); // TODO(cnpryer): Re-export `Interpreter` as public - #[allow(clippy::redundant_closure_for_method_calls)] // Get a path to an interpreter based on the version provided, excluding any activated Python environment. + #[allow(clippy::redundant_closure_for_method_calls)] let Some(path) = interpreters .interpreters() .iter() diff --git a/crates/huak-package-manager/src/package.rs b/crates/huak-package-manager/src/package.rs index fb1fbedb..4853f67c 100644 --- a/crates/huak-package-manager/src/package.rs +++ b/crates/huak-package-manager/src/package.rs @@ -5,7 +5,6 @@ use std::{fmt::Display, str::FromStr}; const VERSION_OPERATOR_CHARACTERS: [char; 5] = ['=', '~', '!', '>', '<']; -#[derive(Clone)] /// The `Package` contains data about a Python `Package`. /// /// A `Package` contains information like the project's name, its version, authors, @@ -19,6 +18,7 @@ const VERSION_OPERATOR_CHARACTERS: [char; 5] = ['=', '~', '!', '>', '<']; /// /// assert_eq!(package.version, Version::from_str("0.0.1").unwrap())); /// ``` +#[derive(Clone)] pub struct Package { /// Information used to identify the `Package`. id: PackageId, @@ -27,26 +27,25 @@ pub struct Package { } impl Package { - #[must_use] /// Get a reference to the `Package`'s name. + #[must_use] pub fn name(&self) -> &str { &self.id.name } - #[must_use] /// Get a reference to the PEP 440 `Version` of the `Package`. + #[must_use] pub fn version(&self) -> &Version { &self.id.version } - #[must_use] /// Get a reference to the `Package`'s core `Metadata`. + #[must_use] pub fn metadata(&self) -> &Metadata { &self.metadata } // TODO: I want this implemented with `FromStr`. - #[allow(clippy::should_implement_trait)] /// Initialize a `Package` from a `&str`. /// /// ``` @@ -54,6 +53,7 @@ impl Package { /// /// let package = Package::from_str("my-package == 0.0.1").unwrap(); /// ``` + #[allow(clippy::should_implement_trait)] pub fn from_str>(s: T) -> HuakResult { // A naive approach to parsing the name and `VersionSpecifiers` from the `&str`. // Find the first character of the `VersionSpecifiers`. Everything prior is considered @@ -137,8 +137,8 @@ impl PartialEq for Package { impl Eq for Package {} -#[derive(Clone)] /// The `PackageId` struct is used to contain `Package`-identifying data. +#[derive(Clone)] struct PackageId { /// The `Package` name. name: String, diff --git a/crates/huak-package-manager/src/python_environment.rs b/crates/huak-package-manager/src/python_environment.rs index bed0974b..4da8509e 100644 --- a/crates/huak-package-manager/src/python_environment.rs +++ b/crates/huak-package-manager/src/python_environment.rs @@ -86,8 +86,8 @@ impl PythonEnvironment { Ok(env) } - #[must_use] /// Get a reference to the path to the `PythonEnvironment`. + #[must_use] pub fn root(&self) -> &Path { self.root.as_ref() } @@ -97,20 +97,20 @@ impl PythonEnvironment { fs::last_path_component(&self.root) } - #[must_use] /// Get a reference to the Python `Interpreter`'s path that's used by the `PythonEnvironment`. + #[must_use] pub fn python_path(&self) -> &PathBuf { self.interpreter.path() } - #[must_use] /// Get a reference to the `PythonEnvironment`'s executables directory path. + #[must_use] pub fn executables_dir_path(&self) -> &PathBuf { &self.executables_dir_path } - #[must_use] /// Get a reference to the `PythonEnvironment`'s site-packages directory path. + #[must_use] pub fn site_packages_dir_path(&self) -> &PathBuf { &self.site_packages_path } @@ -196,8 +196,8 @@ impl PythonEnvironment { } } - #[must_use] /// Check if the `PythonEnvironment` has a `Package` already installed. + #[must_use] pub fn contains_package(&self, package: &Package) -> bool { self.site_packages_dir_path().join(package.name()).exists() } @@ -219,8 +219,8 @@ impl PythonEnvironment { Ok(packages) } - #[must_use] /// Check if the `PythonEnvironment` is already activated. + #[must_use] pub fn active(&self) -> bool { Some(&self.root) == active_virtual_env_path() @@ -287,8 +287,8 @@ pub fn venv_executables_dir_path>(root: T) -> PathBuf { path } -#[derive(Clone)] /// A struct used to configure Python `Package` installations. +#[derive(Clone)] pub struct InstallOptions { /// A values vector of install options typically used for passing on arguments. pub values: Option>, @@ -361,28 +361,27 @@ impl Interpreters { &self.interpreters } - #[allow(dead_code)] /// Get the latest Python `Interpreter` by `Version`. + #[allow(dead_code)] pub fn latest(&self) -> Option<&Interpreter> { self.interpreters.iter().max() } - #[allow(dead_code)] /// Get a Python `Interpreter` by its `Version`. + #[allow(dead_code)] fn exact(&self, version: &Version) -> Option<&Interpreter> { self.interpreters .iter() .find(|interpreter| &interpreter.version == version) } - #[allow(dead_code)] /// Get a Python `Interpreter` by its path. + #[allow(dead_code)] fn get(&self, path: &PathBuf) -> Option<&Interpreter> { self.interpreters.iter().find(|p| p.path() == path) } } -#[derive(Debug)] /// The Python `Interpreter` is used to interact with installed Python `Interpreter`s. /// /// `Interpreter` contains information like the `Interpreter`'s path, `Version`, etc. @@ -392,6 +391,7 @@ impl Interpreters { /// /// let python = Interpreter::new("path/to/python"); /// ``` +#[derive(Debug)] pub struct Interpreter { /// The `Version` of the Python `Interpreter`. version: Version, @@ -454,8 +454,8 @@ fn compare_interpreters(this: &Interpreter, other: &Interpreter) -> Ordering { Ordering::Equal } -#[must_use] /// Get any activated Python environment. +#[must_use] pub fn active_python_env_path() -> Option { active_virtual_env_path().or(active_conda_env_path()) } @@ -525,8 +525,8 @@ fn python_interpreters_in_paths( }) } -#[cfg(unix)] /// A function for checking if a Python `Interpreter`'s file name is valid. +#[cfg(unix)] fn valid_python_interpreter_file_name(file_name: &str) -> bool { if file_name == "python" { return true; @@ -539,8 +539,8 @@ fn valid_python_interpreter_file_name(file_name: &str) -> bool { file_name.len() >= "python3.0".len() && file_name["python".len()..].parse::().is_ok() } -#[cfg(windows)] /// A function for checking if a Python `Interpreter`'s file name is valid. +#[cfg(windows)] fn valid_python_interpreter_file_name(file_name: &str) -> bool { if file_name == "python.exe" || file_name == "python" { return true; diff --git a/crates/huak-package-manager/src/version.rs b/crates/huak-package-manager/src/version.rs index 91567ba2..b74da2e1 100644 --- a/crates/huak-package-manager/src/version.rs +++ b/crates/huak-package-manager/src/version.rs @@ -10,10 +10,10 @@ trait ToSemVer { fn to_semver(self) -> SemVer; } -#[derive(Debug)] /// A generic `Version` struct. /// /// This struct is mainly used for the Python `Interpreter`. +#[derive(Debug)] pub struct Version { release: Vec, } diff --git a/crates/huak-package-manager/src/workspace.rs b/crates/huak-package-manager/src/workspace.rs index d0b681d5..6be043a3 100644 --- a/crates/huak-package-manager/src/workspace.rs +++ b/crates/huak-package-manager/src/workspace.rs @@ -34,14 +34,14 @@ impl Workspace { } } - #[must_use] /// Get a reference to the path to the `Workspace` root. + #[must_use] pub fn root(&self) -> &PathBuf { &self.root } - #[must_use] /// Get an `Environment` associated with the `Workspace`. + #[must_use] pub fn environment(&self) -> Environment { Environment::new() } diff --git a/crates/huak-python-manager/src/cli.rs b/crates/huak-python-manager/src/cli.rs index 8c820e2d..3aa60dcd 100644 --- a/crates/huak-python-manager/src/cli.rs +++ b/crates/huak-python-manager/src/cli.rs @@ -29,10 +29,9 @@ impl Cli { enum Commands { /// Install a Python interpreter. Install { - #[arg(required = true)] /// Version of Python to install. + #[arg(required = true)] version: RequestedVersion, - /// Target path to install Python to. #[arg(long, required = true)] target: PathBuf, diff --git a/crates/huak-python-manager/src/releases.rs b/crates/huak-python-manager/src/releases.rs index c5df2c89..406a0f70 100644 --- a/crates/huak-python-manager/src/releases.rs +++ b/crates/huak-python-manager/src/releases.rs @@ -2,9 +2,9 @@ use std::{cmp::Ordering, fmt::Display}; +// TODO(cnpryer): Perf #[allow(dead_code)] #[rustfmt::skip] -// TODO(cnpryer): Perf pub(crate) const RELEASES: &[Release] = &[ Release::new("cpython", Version::new(3, 10, 13), "apple", "aarch64", "pgo+lto", "a2635841454295c5bc2c18740346fd8308f2a8adcce2407b87c9faf261fed29c", "https://github.com/indygreg/python-build-standalone/releases/download/20231002/cpython-3.10.13%2B20231002-aarch64-apple-darwin-pgo%2Blto-full.tar.zst"), Release::new("cpython", Version::new(3, 10, 13), "apple", "aarch64", "pgo", "67b64174b8d33aa1b2e3bb3a4a3e475ff96d511c540f46e3c0774f8b77be4d91", "https://github.com/indygreg/python-build-standalone/releases/download/20231002/cpython-3.10.13%2B20231002-aarch64-apple-darwin-pgo-full.tar.zst"), diff --git a/crates/huak-python-manager/src/resolve.rs b/crates/huak-python-manager/src/resolve.rs index e462cf54..efafc7cc 100644 --- a/crates/huak-python-manager/src/resolve.rs +++ b/crates/huak-python-manager/src/resolve.rs @@ -43,8 +43,8 @@ fn resolve_release_with_options(options: &Options) -> Option> { } } -#[derive(Default)] /// The strategy used for resolving a Python releases. +#[derive(Default)] pub enum Strategy<'a> { #[default] /// Resolve with the latest possible Python release version for the current environment. @@ -54,8 +54,8 @@ pub enum Strategy<'a> { Selection(Options<'a>), } -#[derive(Debug)] /// Options criteria used for resolving Python releases. +#[derive(Debug)] pub struct Options<'a> { pub kind: &'a str, pub version: Option, // TODO(cnpryer): Refactor to default as *latest available*