Skip to content

style: simplify string formatting #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions crates/pet-conda/src/environment_locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,12 @@ pub fn get_known_conda_install_locations(env_vars: &EnvVariables) -> Vec<PathBuf
"", // We need to look in `/anaconda3` and `/miniconda3` as well.
];
for directory in directories_to_look_in.iter() {
known_paths.push(PathBuf::from(format!("{}/anaconda", directory)));
known_paths.push(PathBuf::from(format!("{}/anaconda3", directory)));
known_paths.push(PathBuf::from(format!("{}/miniconda", directory)));
known_paths.push(PathBuf::from(format!("{}/miniconda3", directory)));
known_paths.push(PathBuf::from(format!("{}/miniforge", directory)));
known_paths.push(PathBuf::from(format!("{}/miniforge3", directory)));
known_paths.push(PathBuf::from(format!("{directory}/anaconda")));
known_paths.push(PathBuf::from(format!("{directory}/anaconda3")));
known_paths.push(PathBuf::from(format!("{directory}/miniconda")));
known_paths.push(PathBuf::from(format!("{directory}/miniconda3")));
known_paths.push(PathBuf::from(format!("{directory}/miniforge")));
known_paths.push(PathBuf::from(format!("{directory}/miniforge3")));
}
if let Some(ref conda_root) = env_vars.conda_root {
known_paths.push(PathBuf::from(conda_root.clone()));
Expand Down
2 changes: 1 addition & 1 deletion crates/pet-core/src/arch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub enum Architecture {

impl Ord for Architecture {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
format!("{:?}", self).cmp(&format!("{:?}", other))
format!("{self:?}").cmp(&format!("{other:?}"))
}
}
impl PartialOrd for Architecture {
Expand Down
4 changes: 2 additions & 2 deletions crates/pet-core/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub enum EnvManagerType {

impl Ord for EnvManagerType {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
format!("{:?}", self).cmp(&format!("{:?}", other))
format!("{self:?}").cmp(&format!("{other:?}"))
}
}
impl PartialOrd for EnvManagerType {
Expand Down Expand Up @@ -72,7 +72,7 @@ impl std::fmt::Display for EnvManager {
)
.unwrap_or_default();
if let Some(version) = &self.version {
writeln!(f, " Version : {}", version).unwrap_or_default();
writeln!(f, " Version : {version}").unwrap_or_default();
}
Ok(())
}
Expand Down
14 changes: 7 additions & 7 deletions crates/pet-core/src/python_environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub enum PythonEnvironmentCategory {
}
impl Ord for PythonEnvironmentCategory {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
format!("{:?}", self).cmp(&format!("{:?}", other))
format!("{self:?}").cmp(&format!("{other:?}"))
}
}
impl PartialOrd for PythonEnvironmentCategory {
Expand Down Expand Up @@ -140,17 +140,17 @@ impl std::fmt::Display for PythonEnvironment {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
writeln!(f, "Environment ({:?})", self.category).unwrap_or_default();
if let Some(name) = &self.display_name {
writeln!(f, " Display-Name: {}", name).unwrap_or_default();
writeln!(f, " Display-Name: {name}").unwrap_or_default();
}
if let Some(name) = &self.name {
writeln!(f, " Name : {}", name).unwrap_or_default();
writeln!(f, " Name : {name}").unwrap_or_default();
}
if let Some(exe) = &self.executable {
writeln!(f, " Executable : {}", exe.to_str().unwrap_or_default())
.unwrap_or_default();
}
if let Some(version) = &self.version {
writeln!(f, " Version : {}", version).unwrap_or_default();
writeln!(f, " Version : {version}").unwrap_or_default();
}
if let Some(prefix) = &self.prefix {
writeln!(
Expand All @@ -167,7 +167,7 @@ impl std::fmt::Display for PythonEnvironment {
writeln!(f, " Search Path : {}", search_path.to_str().unwrap()).unwrap_or_default();
}
if let Some(arch) = &self.arch {
writeln!(f, " Architecture: {}", arch).unwrap_or_default();
writeln!(f, " Architecture: {arch}").unwrap_or_default();
}
if let Some(manager) = &self.manager {
writeln!(
Expand All @@ -190,9 +190,9 @@ impl std::fmt::Display for PythonEnvironment {
if !symlinks.is_empty() {
for (i, symlink) in symlinks.iter().enumerate() {
if i == 0 {
writeln!(f, " Symlinks : {:?}", symlink).unwrap_or_default();
writeln!(f, " Symlinks : {symlink:?}").unwrap_or_default();
} else {
writeln!(f, " : {:?}", symlink).unwrap_or_default();
writeln!(f, " : {symlink:?}").unwrap_or_default();
}
}
}
Expand Down
66 changes: 29 additions & 37 deletions crates/pet-homebrew/src/sym_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,25 @@ pub fn get_known_symlinks_impl(
let version = version.as_str().to_string();
let mut symlinks = vec![symlink_resolved_python_exe.to_owned()];
for possible_symlink in [
PathBuf::from(format!("/opt/homebrew/bin/python{}", version)),
PathBuf::from(format!("/opt/homebrew/opt/python@{}/bin/python{}", version, version)),
PathBuf::from(format!("/opt/homebrew/Cellar/python@{}/{}/bin/python{}",version, full_version, version)),
PathBuf::from(format!("/opt/homebrew/Cellar/python@{}/{}/Frameworks/Python.framework/Versions/{}/bin/python{}", version, full_version, version, version)),
PathBuf::from(format!("/opt/homebrew/Cellar/python@{}/{}/Frameworks/Python.framework/Versions/Current/bin/python{}", version, full_version, version)),
PathBuf::from(format!("/opt/homebrew/Frameworks/Python.framework/Versions/{}/bin/python{}", version, version)),
PathBuf::from(format!("/opt/homebrew/Frameworks/Python.framework/Versions/Current/bin/python{}", version)),
PathBuf::from(format!("/usr/local/opt/python@{}/bin/python3", version)),
PathBuf::from(format!("/usr/local/opt/python@{}/bin/python{}", version, version)),
PathBuf::from(format!("/opt/homebrew/bin/python{version}")),
PathBuf::from(format!("/opt/homebrew/opt/python@{version}/bin/python{version}")),
PathBuf::from(format!("/opt/homebrew/Cellar/python@{version}/{full_version}/bin/python{version}")),
PathBuf::from(format!("/opt/homebrew/Cellar/python@{version}/{full_version}/Frameworks/Python.framework/Versions/{version}/bin/python{version}")),
PathBuf::from(format!("/opt/homebrew/Cellar/python@{version}/{full_version}/Frameworks/Python.framework/Versions/Current/bin/python{version}")),
PathBuf::from(format!("/opt/homebrew/Frameworks/Python.framework/Versions/{version}/bin/python{version}")),
PathBuf::from(format!("/opt/homebrew/Frameworks/Python.framework/Versions/Current/bin/python{version}")),
PathBuf::from(format!("/usr/local/opt/python@{version}/bin/python3")),
PathBuf::from(format!("/usr/local/opt/python@{version}/bin/python{version}")),
PathBuf::from("/opt/homebrew/opt/python/bin/python3"),
PathBuf::from(format!("/opt/homebrew/opt/python/bin/python{}", version)),
PathBuf::from(format!("/opt/homebrew/opt/python/bin/python{version}")),
PathBuf::from("/opt/homebrew/opt/python@3/bin/python3"),
PathBuf::from(format!("/opt/homebrew/opt/python@3/bin/python{}", version)),
PathBuf::from(format!("/opt/homebrew/opt/python@{}/bin/python3", version)),
PathBuf::from(format!("/opt/homebrew/opt/python@{}/bin/python{}", version, version)),
PathBuf::from(format!("/opt/homebrew/opt/python@3/bin/python{version}")),
PathBuf::from(format!("/opt/homebrew/opt/python@{version}/bin/python3")),
PathBuf::from(format!("/opt/homebrew/opt/python@{version}/bin/python{version}")),
PathBuf::from("/usr/local/opt/python@3/bin/python3"),
PathBuf::from(format!("/usr/local/opt/python@3/bin/python{}", version)),
PathBuf::from(format!("/usr/local/opt/python@3/bin/python{version}")),
// Check if this symlink is pointing to the same place as the resolved python exe
PathBuf::from(format!("/opt/homebrew/opt/python3/bin/python{}", version)),
PathBuf::from(format!("/opt/homebrew/opt/python3/bin/python{version}")),
// Check if this symlink is pointing to the same place as the resolved python exe
PathBuf::from("/opt/homebrew/bin/python3"),
// Check if this symlink is pointing to the same place as the resolved python exe
Expand Down Expand Up @@ -148,21 +148,19 @@ pub fn get_known_symlinks_impl(
// 1. python 3.8 has sysprefix in /usr/local/Cellar/[email protected]/3.9.19/Frameworks/Python.framework/Versions/3.9
// 2. python 3.9 has sysprefix in /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9
// 3. python 3.11 has sysprefix in /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.11
PathBuf::from(format!("/usr/local/opt/python@{}/bin/python3", version)),
PathBuf::from(format!("/usr/local/opt/python@{}/bin/python{}", version, version)),
PathBuf::from(format!("/usr/local/opt/python@{version}/bin/python3")),
PathBuf::from(format!("/usr/local/opt/python@{version}/bin/python{version}")),
PathBuf::from("/usr/local/opt/python@3/bin/python3"),
PathBuf::from(format!("/usr/local/opt/python@3/bin/python{}", version)),
PathBuf::from(format!("/usr/local/opt/python@3/bin/python{version}")),
PathBuf::from(format!(
"/usr/local/Cellar/python@{}/{}/bin/python{}",
version, full_version, version
"/usr/local/Cellar/python@{version}/{full_version}/bin/python{version}"
)),
PathBuf::from(format!(
"/usr/local/Cellar/python@{}/{}/Frameworks/Python.framework/Versions/{}/bin/python{}",
version, full_version, version, version
"/usr/local/Cellar/python@{version}/{full_version}/Frameworks/Python.framework/Versions/{version}/bin/python{version}"
)),
// This is a special folder, if users install python using other means, this file
// might get overridden. So we should only add this if this files points to the same place
PathBuf::from(format!("/usr/local/bin/python{}", version)),
PathBuf::from(format!("/usr/local/bin/python{version}")),
// Check if this symlink is pointing to the same place as the resolved python exe
PathBuf::from("/usr/local/bin/python3"),
// Check if this symlink is pointing to the same place as the resolved python exe
Expand Down Expand Up @@ -201,36 +199,30 @@ pub fn get_known_symlinks_impl(
let mut symlinks = vec![symlink_resolved_python_exe.to_owned()];
for possible_symlink in [
PathBuf::from("/home/linuxbrew/.linuxbrew/bin/python3"),
PathBuf::from(format!("/home/linuxbrew/.linuxbrew/bin/python{}", version)),
PathBuf::from(format!("/home/linuxbrew/.linuxbrew/bin/python{version}")),
PathBuf::from(format!(
"/home/linuxbrew/.linuxbrew/Cellar/python@{}/{}/bin/python{}",
version, full_version, version
"/home/linuxbrew/.linuxbrew/Cellar/python@{version}/{full_version}/bin/python{version}"
)),
PathBuf::from(format!(
"/home/linuxbrew/.linuxbrew/Cellar/python@{}/{}/bin/python3",
version, full_version
"/home/linuxbrew/.linuxbrew/Cellar/python@{version}/{full_version}/bin/python3"
)),
PathBuf::from(format!(
"/home/linuxbrew/.linuxbrew/opt/python@{}/bin/python{}",
version, version
"/home/linuxbrew/.linuxbrew/opt/python@{version}/bin/python{version}"
)),
PathBuf::from(format!(
"/home/linuxbrew/.linuxbrew/opt/python@{}/bin/python3",
version
"/home/linuxbrew/.linuxbrew/opt/python@{version}/bin/python3"
)),
PathBuf::from(format!(
"/home/linuxbrew/.linuxbrew/opt/python3/bin/python{}",
version
"/home/linuxbrew/.linuxbrew/opt/python3/bin/python{version}"
)),
PathBuf::from("/home/linuxbrew/.linuxbrew/opt/python3/bin/python3"),
PathBuf::from(format!(
"/home/linuxbrew/.linuxbrew/opt/python@3/bin/python{}",
version
"/home/linuxbrew/.linuxbrew/opt/python@3/bin/python{version}"
)),
PathBuf::from("/home/linuxbrew/.linuxbrew/opt/python@3/bin/python3"),
// This is a special folder, if users install python using other means, this file
// might get overridden. So we should only add this if this files points to the same place
PathBuf::from(format!("/usr/local/bin/python{}", version)),
PathBuf::from(format!("/usr/local/bin/python{version}")),
// Check if this symlink is pointing to the same place as the resolved python exe
PathBuf::from("/usr/local/bin/python3"),
// Check if this symlink is pointing to the same place as the resolved python exe
Expand Down
27 changes: 10 additions & 17 deletions crates/pet-jsonrpc/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,36 +58,33 @@ impl<C> HandlersKeyedByMethodName<C> {
if let Some(handler) = self.requests.get(method) {
handler(self.context.clone(), id as u32, message["params"].clone());
} else {
eprint!("Failed to find handler for method: {}", method);
eprint!("Failed to find handler for method: {method}");
send_error(
Some(id as u32),
-1,
format!("Failed to find handler for request {}", method),
format!("Failed to find handler for request {method}"),
);
}
} else {
// No id, so this is a notification
if let Some(handler) = self.notifications.get(method) {
handler(self.context.clone(), message["params"].clone());
} else {
eprint!("Failed to find handler for method: {}", method);
eprint!("Failed to find handler for method: {method}");
send_error(
None,
-2,
format!("Failed to find handler for notification {}", method),
format!("Failed to find handler for notification {method}"),
);
}
}
}
None => {
eprint!("Failed to get method from message: {}", message);
eprint!("Failed to get method from message: {message}");
send_error(
None,
-3,
format!(
"Failed to extract method from JSONRPC payload {:?}",
message
),
format!("Failed to extract method from JSONRPC payload {message:?}"),
);
}
};
Expand Down Expand Up @@ -115,20 +112,17 @@ pub fn start_server<C>(handlers: &HandlersKeyedByMethodName<C>) -> ! {
match serde_json::from_str(&request) {
Ok(request) => handlers.handle_request(request),
Err(err) => {
eprint!("Failed to parse LINE: {}, {:?}", request, err)
eprint!("Failed to parse LINE: {request}, {err:?}")
}
}
continue;
}
Err(err) => {
eprint!(
"Failed to read exactly {} bytes, {:?}",
content_length, err
)
eprint!("Failed to read exactly {content_length} bytes, {err:?}")
}
}
}
Err(err) => eprint!("Failed to get content length from {}, {:?}", input, err),
Err(err) => eprint!("Failed to get content length from {input}, {err:?}"),
};
}
Err(error) => eprint!("Error in reading a line from stdin: {error}"),
Expand All @@ -152,8 +146,7 @@ fn get_content_length(line: &str) -> Result<usize, String> {
}
} else {
Err(format!(
"String 'Content-Length' not found in input => {}",
line
"String 'Content-Length' not found in input => {line}"
))
}
}
3 changes: 1 addition & 2 deletions crates/pet-mac-xcode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ impl Locator for MacXCode {
let xcode_folder_name = exe_str.split('/').nth(2).unwrap_or_default();

let bin = PathBuf::from(format!(
"/Applications/{}/Contents/Developer/usr/bin",
xcode_folder_name
"/Applications/{xcode_folder_name}/Contents/Developer/usr/bin"
));
let exe = bin.join("python3");
if let Some(symlink) = resolve_symlink(&exe) {
Expand Down
2 changes: 1 addition & 1 deletion crates/pet-poetry/src/environment_locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ pub fn generate_env_name(name: &str, cwd: &PathBuf) -> String {
.chars()
.take(8)
.collect::<String>();
format!("{}-{}-py", sanitized_name, h_str)
format!("{sanitized_name}-{h_str}-py")
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions crates/pet-reporter/src/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Reporter for StdioReporter {
let count = managers.get(&manager.tool).unwrap_or(&0) + 1;
managers.insert(manager.tool, count);
if self.print_list {
println!("{}", manager)
println!("{manager}")
}
}

Expand All @@ -50,7 +50,7 @@ impl Reporter for StdioReporter {
let count = environments.get(&env.category).unwrap_or(&0) + 1;
environments.insert(env.category, count);
if self.print_list {
println!("{}", env)
println!("{env}")
}
}
}
Expand Down
Loading
Loading