From 39612a4c3166ce8ab1475d386acedf09fc7aa760 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 5 Apr 2024 16:38:27 -0700 Subject: [PATCH] Eliminate try_exists call prior to reading hosts.yml --- gh-token/src/lib.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/gh-token/src/lib.rs b/gh-token/src/lib.rs index 4ae96dc..0fc578e 100644 --- a/gh-token/src/lib.rs +++ b/gh-token/src/lib.rs @@ -10,6 +10,7 @@ use serde_derive::Deserialize; use std::env; use std::fmt::{self, Debug, Display}; use std::fs; +use std::io::ErrorKind; use std::path::{Path, PathBuf}; use std::process::Command; @@ -94,15 +95,15 @@ pub fn get() -> Result { return Err(Error::NotConfigured(fallback_path)); }; - match path.try_exists() { - Ok(true) => {} - Ok(false) => return Err(Error::NotConfigured(path)), - Err(io_error) => return Err(Error::Parse(ParseError::Io(path, io_error))), - } - let content = match fs::read(&path) { Ok(content) => content, - Err(io_error) => return Err(Error::Parse(ParseError::Io(path, io_error))), + Err(io_error) => { + return Err(if io_error.kind() == ErrorKind::NotFound { + Error::NotConfigured(path) + } else { + Error::Parse(ParseError::Io(path, io_error)) + }); + } }; let config: Config = match serde_yaml::from_slice(&content) {