Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kyu08 committed Nov 24, 2024
1 parent 6829b31 commit fc436ff
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 8 deletions.
139 changes: 139 additions & 0 deletions src/file/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,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"
executed-targets = ["test", "check", "spell-check"]
[[history]]
path = "/Users/user/code/golang/go-playground"
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,
)
}
}
}
20 changes: 12 additions & 8 deletions src/file/toml_old.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::toml::{Histories, History, HistoryCommand};
use super::toml as fzf_make_toml;
use crate::model;
use anyhow::Result;
use serde::{Deserialize, Serialize};
Expand All @@ -10,28 +10,32 @@ struct HistoriesOld {
}

impl HistoriesOld {
fn into_histories(self) -> Histories {
let mut result: Vec<History> = vec![];
fn into_histories(self) -> fzf_make_toml::Histories {
let mut result: Vec<fzf_make_toml::History> = vec![];
for h in self.histories_old.clone() {
let mut commands: Vec<HistoryCommand> = vec![];
let mut commands: Vec<fzf_make_toml::HistoryCommand> = vec![];
for c in h.executed_targets {
commands.push(HistoryCommand::new(model::runner_type::RunnerType::Make, c));
commands.push(fzf_make_toml::HistoryCommand::new(
model::runner_type::RunnerType::Make,
c,
));
}
result.push(History::new(PathBuf::from(h.path), commands));
result.push(fzf_make_toml::History::new(PathBuf::from(h.path), commands));
}

Histories::new(result)
fzf_make_toml::Histories::new(result)
}
}

#[derive(Serialize, Deserialize, Clone)]
#[serde(rename_all = "kebab-case")]
#[serde(rename(deserialize = "history"))]
struct HistoryOld {
path: String,
executed_targets: Vec<String>,
}

pub fn parse_history(content: String) -> Result<Histories> {
pub fn parse_history(content: String) -> Result<fzf_make_toml::Histories> {
let histories: HistoriesOld = toml::from_str(&content)?;
Ok(histories.into_histories())
}

0 comments on commit fc436ff

Please sign in to comment.