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 fc436ff commit 67d3db3
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 17 deletions.
14 changes: 7 additions & 7 deletions src/file/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,13 +398,13 @@ executed-targets = ["run", "echo1"]
],
},
},
// Case {
// title: "Error",
// content: r#"
// "#
// .to_string(),
// expect: Histories::default(),
// },
Case {
title: "Error",
content: r#"
"#
.to_string(),
expect: Histories::default(),
},
];

for case in cases {
Expand Down
109 changes: 99 additions & 10 deletions src/file/toml_old.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

#[derive(Serialize, Deserialize, Clone)]
struct HistoriesOld {
histories_old: Vec<HistoryOld>,
#[derive(Serialize, Deserialize, Clone, PartialEq)]
struct Histories {
history: Vec<History>,
}

impl HistoriesOld {
impl Histories {
fn into_histories(self) -> fzf_make_toml::Histories {
let mut result: Vec<fzf_make_toml::History> = vec![];
for h in self.histories_old.clone() {
for h in self.history.clone() {
let mut commands: Vec<fzf_make_toml::HistoryCommand> = vec![];
for c in h.executed_targets {
commands.push(fzf_make_toml::HistoryCommand::new(
Expand All @@ -27,15 +27,104 @@ impl HistoriesOld {
}
}

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

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

#[cfg(test)]
mod test {
use super::*;
use crate::file::toml as fzf_make_toml;
use crate::model::runner_type;
use pretty_assertions::assert_eq;

#[test]
fn parse_history_test() {
struct Case {
title: &'static str,
content: String,
expect: Result<fzf_make_toml::Histories>,
}
let cases = vec![
Case {
title: "Success",
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: Ok(fzf_make_toml::Histories::new(vec![
fzf_make_toml::History::new(
PathBuf::from("/Users/user/code/fzf-make"),
vec![
fzf_make_toml::HistoryCommand::new(
runner_type::RunnerType::Make,
"test".to_string(),
),
fzf_make_toml::HistoryCommand::new(
runner_type::RunnerType::Make,
"check".to_string(),
),
fzf_make_toml::HistoryCommand::new(
runner_type::RunnerType::Make,
"spell-check".to_string(),
),
],
),
fzf_make_toml::History::new(
PathBuf::from("/Users/user/code/golang/go-playground"),
vec![
fzf_make_toml::HistoryCommand::new(
runner_type::RunnerType::Make,
"run".to_string(),
),
fzf_make_toml::HistoryCommand::new(
runner_type::RunnerType::Make,
"echo1".to_string(),
),
],
),
])),
},
Case {
title: "Error",
content: r#"
"#
.to_string(),
expect: Err(anyhow::anyhow!("TOML parse error at line 1, column 1\n |\n1 | \n | ^\nmissing field `history`\n")),
},
];

for case in cases {
match case.expect {
Ok(e) => assert_eq!(
e,
parse_history(case.content).unwrap(),
"\nFailed: 🚨{:?}🚨\n",
case.title,
),
Err(_) => assert_eq!(
case.expect.unwrap_err().to_string(),
parse_history(case.content).unwrap_err().to_string(),
"\nFailed: 🚨{:?}🚨\n",
case.title,
),
}
}
}
}

0 comments on commit 67d3db3

Please sign in to comment.