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

fix(deps): update rust crate serde to v1.0.215 #311

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 13 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fzf-make"
version = "0.37.0"
version = "0.38.0"
edition = "2021"
authors = ["kyu08"]
description = "A command line tool that executes make target using fuzzy finder with preview window."
Expand Down
10 changes: 10 additions & 0 deletions src/err/any_to_string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// ref: https://qiita.com/kgtkr/items/a17827c4bb704f39c854
pub fn any_to_string(any: &dyn std::any::Any) -> String {
if let Some(s) = any.downcast_ref::<String>() {
s.clone()
} else if let Some(s) = any.downcast_ref::<&str>() {
s.to_string()
} else {
"Any".to_string()
}
}
1 change: 1 addition & 0 deletions src/err/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub(crate) mod any_to_string;
1 change: 1 addition & 0 deletions src/file/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub(crate) mod path_to_content;
pub(crate) mod toml;
mod toml_old;
1 change: 0 additions & 1 deletion src/file/path_to_content.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anyhow::{anyhow, Result};

use std::{fs::read_to_string, path::PathBuf};

pub fn path_to_content(path: PathBuf) -> Result<String> {
Expand Down
176 changes: 166 additions & 10 deletions src/file/toml.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::path_to_content;
use super::{path_to_content, toml_old};
use crate::model::{
histories::{self},
runner_type,
Expand All @@ -23,18 +23,27 @@ impl Histories {
match history_file_path() {
Some((history_file_dir, history_file_name)) => {
match path_to_content::path_to_content(history_file_dir.join(history_file_name)) {
// TODO: Show error message on message pane if parsing history file failed. https://github.com/kyu08/fzf-make/issues/152
Ok(c) => match parse_history(c.to_string()) {
Ok(h) => h,
Err(_) => Histories { histories: vec![] },
},
Err(_) => Histories { histories: vec![] },
Ok(c) => Histories::parse_history_in_considering_history_file_format_version(c),
Err(_) => Histories::default(),
}
}
None => Histories { histories: vec![] },
None => Histories::default(),
}
}

fn parse_history_in_considering_history_file_format_version(content: String) -> Histories {
// NOTE: The history file format has changed after https://github.com/kyu08/fzf-make/pull/324.
// So at first we try to parse it as the new format, and then try to parse it as the old format.
match parse_history(content.to_string()) {
Ok(h) => h,
Err(_) => toml_old::parse_history(content.to_string()).unwrap_or_default(),
}
}

pub fn new(histories: Vec<History>) -> Self {
Self { histories }
}

fn from(histories: histories::Histories) -> Self {
let mut result: Vec<History> = vec![];
for h in histories.histories {
Expand All @@ -53,7 +62,7 @@ impl Histories {
}

#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
struct History {
pub struct History {
path: PathBuf,
commands: Vec<HistoryCommand>,
}
Expand Down Expand Up @@ -82,17 +91,25 @@ impl History {
commands,
}
}

pub fn new(path: PathBuf, commands: Vec<HistoryCommand>) -> Self {
Self { path, commands }
}
}

/// toml representation of histories::HistoryCommand.
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
#[serde(rename_all = "kebab-case")]
struct HistoryCommand {
pub struct HistoryCommand {
runner_type: runner_type::RunnerType,
name: String,
}

impl HistoryCommand {
pub fn new(runner_type: runner_type::RunnerType, name: String) -> Self {
Self { runner_type, name }
}

fn from(command: histories::HistoryCommand) -> Self {
Self {
runner_type: command.runner_type,
Expand Down Expand Up @@ -260,4 +277,143 @@ name = "echo1"
}
}
}

#[test]
fn parse_history_in_considering_history_file_format_version_test() {
struct Case {
title: &'static str,
content: String,
expect: Histories,
}
let cases = vec![
Case {
title: "Success(new format)",
content: r#"
[[histories]]
path = "/Users/user/code/fzf-make"

[[histories.commands]]
runner-type = "make"
name = "test"

[[histories.commands]]
runner-type = "make"
name = "check"

[[histories.commands]]
runner-type = "make"
name = "spell-check"

[[histories]]
path = "/Users/user/code/golang/go-playground"

[[histories.commands]]
runner-type = "make"
name = "run"

[[histories.commands]]
runner-type = "make"
name = "echo1"
"#
.to_string(),
expect: Histories {
histories: vec![
History {
path: PathBuf::from("/Users/user/code/fzf-make"),
commands: vec![
HistoryCommand {
runner_type: runner_type::RunnerType::Make,
name: "test".to_string(),
},
HistoryCommand {
runner_type: runner_type::RunnerType::Make,
name: "check".to_string(),
},
HistoryCommand {
runner_type: runner_type::RunnerType::Make,
name: "spell-check".to_string(),
},
],
},
History {
path: PathBuf::from("/Users/user/code/golang/go-playground"),
commands: vec![
HistoryCommand {
runner_type: runner_type::RunnerType::Make,
name: "run".to_string(),
},
HistoryCommand {
runner_type: runner_type::RunnerType::Make,
name: "echo1".to_string(),
},
],
},
],
},
},
Case {
title: "Success(old format)",
content: r#"
[[history]]
path = "/Users/user/code/fzf-make/Makefile"
executed-targets = ["test", "check", "spell-check"]

[[history]]
path = "/Users/user/code/golang/go-playground/Makefile"
executed-targets = ["run", "echo1"]
"#
.to_string(),
expect: Histories {
histories: vec![
History {
path: PathBuf::from("/Users/user/code/fzf-make"),
commands: vec![
HistoryCommand {
runner_type: runner_type::RunnerType::Make,
name: "test".to_string(),
},
HistoryCommand {
runner_type: runner_type::RunnerType::Make,
name: "check".to_string(),
},
HistoryCommand {
runner_type: runner_type::RunnerType::Make,
name: "spell-check".to_string(),
},
],
},
History {
path: PathBuf::from("/Users/user/code/golang/go-playground"),
commands: vec![
HistoryCommand {
runner_type: runner_type::RunnerType::Make,
name: "run".to_string(),
},
HistoryCommand {
runner_type: runner_type::RunnerType::Make,
name: "echo1".to_string(),
},
],
},
],
},
},
Case {
title: "Error",
content: r#"
"#
.to_string(),
expect: Histories::default(),
},
];

for case in cases {
assert_eq!(
case.expect,
Histories::parse_history_in_considering_history_file_format_version(case.content),
"\nFailed: 🚨{:?}🚨\n",
case.title,
)
}
}
}
Loading