Skip to content

Commit

Permalink
When the history file type is the old format, rewrite it using the ne…
Browse files Browse the repository at this point in the history
…w format
  • Loading branch information
kyu08 committed Nov 24, 2024
1 parent a357a36 commit 6829b31
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
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
37 changes: 27 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
37 changes: 37 additions & 0 deletions src/file/toml_old.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use super::toml::{Histories, History, HistoryCommand};
use crate::model;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

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

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

Histories::new(result)
}
}

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

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

0 comments on commit 6829b31

Please sign in to comment.