Skip to content
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

improvement(error-handling): main files #2288

Merged
merged 4 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 3 additions & 2 deletions .github/workflows/pr-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
types: |
feat
fix
improvement
chore
docs
deps
Expand All @@ -38,9 +39,9 @@ jobs:
TITLE: ${{ github.event.pull_request.title }}
run: |
title_length=${#TITLE}
if [ $title_length -gt 72 ]
if [ $title_length -gt 85 ]
then
echo "PR title is too long (greater than 72 characters)"
echo "PR title is too long (greater than 85 characters)"
exit 1
fi

Expand Down
39 changes: 32 additions & 7 deletions mm2src/common/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,14 @@ use std::convert::TryInto;
use std::fmt::Write as FmtWrite;
use std::fs::File;
use std::future::Future as Future03;
use std::io::{BufReader, Read, Write};
use std::io::{self, BufReader, Read, Write};
use std::iter::Peekable;
use std::mem::{forget, zeroed};
use std::num::{NonZeroUsize, TryFromIntError};
use std::ops::{Add, Deref, Div, RangeInclusive};
use std::os::raw::c_void;
use std::panic::{set_hook, PanicInfo};
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::ptr::read_volatile;
use std::sync::atomic::Ordering;
use std::time::{Duration, SystemTime, SystemTimeError};
Expand Down Expand Up @@ -792,7 +792,7 @@ pub fn kdf_app_dir() -> Option<PathBuf> {
}

/// Returns path of the coins file.
pub fn kdf_coins_file() -> PathBuf {
pub fn kdf_coins_file() -> Result<PathBuf, io::Error> {
#[cfg(not(target_arch = "wasm32"))]
let value_from_env = env::var("MM_COINS_PATH").ok();

Expand All @@ -803,7 +803,7 @@ pub fn kdf_coins_file() -> PathBuf {
}

/// Returns path of the config file.
pub fn kdf_config_file() -> PathBuf {
pub fn kdf_config_file() -> Result<PathBuf, io::Error> {
#[cfg(not(target_arch = "wasm32"))]
let value_from_env = env::var("MM_CONF_PATH").ok();

Expand All @@ -819,16 +819,41 @@ pub fn kdf_config_file() -> PathBuf {
/// 1- From the environment variable.
/// 2- From the current directory where app is called.
/// 3- From the root application directory.
pub fn find_kdf_dependency_file(value_from_env: Option<String>, path_leaf: &str) -> PathBuf {
fn find_kdf_dependency_file(value_from_env: Option<String>, path_leaf: &str) -> Result<PathBuf, io::Error> {
if let Some(path) = value_from_env {
return PathBuf::from(path);
let path = PathBuf::from(path);
require_file(&path)?;
return Ok(path);
}

let from_current_dir = PathBuf::from(path_leaf);
if from_current_dir.exists() {

let path = if from_current_dir.exists() {
from_current_dir
} else {
kdf_app_dir().unwrap_or_default().join(path_leaf)
};

require_file(&path)?;
return Ok(path);

fn require_file(path: &Path) -> Result<(), io::Error> {
if path.is_dir() {
// TODO: use `IsADirectory` variant which is stabilized with 1.83
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!("Expected file but '{}' is a directory.", path.display()),
));
}

if !path.exists() {
return Err(io::Error::new(
io::ErrorKind::NotFound,
format!("File '{}' is not present.", path.display()),
));
}

Ok(())
}
}

Expand Down
4 changes: 2 additions & 2 deletions mm2src/mm2_main/src/mm2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ pub fn mm2_main(version: String, datetime: String) {
/// Parses and returns the `first_arg` as JSON.
/// Attempts to load the config from `MM2.json` file if `first_arg` is None
pub fn get_mm2config(first_arg: Option<&str>) -> Result<Json, String> {
let conf_path = common::kdf_config_file();
let conf_path = common::kdf_config_file().map_err(|e| e.to_string())?;
let conf_from_file = slurp(&conf_path);
let conf = match first_arg {
Some(s) => s,
shamardy marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -327,7 +327,7 @@ pub fn get_mm2config(first_arg: Option<&str>) -> Result<Json, String> {
};

if conf["coins"].is_null() {
let coins_path = common::kdf_coins_file();
let coins_path = common::kdf_coins_file().map_err(|e| e.to_string())?;

let coins_from_file = slurp(&coins_path);
if coins_from_file.is_empty() {
Expand Down
Loading