Skip to content

Commit d34ca8e

Browse files
committed
refactor(from_trait): impl From trait properly
1 parent 56d84b9 commit d34ca8e

File tree

2 files changed

+49
-44
lines changed

2 files changed

+49
-44
lines changed

src/file/toml.rs

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -44,44 +44,33 @@ impl Histories {
4444
Self { histories }
4545
}
4646

47-
// TODO: impl as From trait
48-
fn from(histories: histories::Histories) -> Self {
49-
let mut result: Vec<History> = vec![];
50-
for h in histories.histories {
51-
result.push(History::from(h));
52-
}
53-
Self { histories: result }
54-
}
55-
5647
pub fn into(self) -> histories::Histories {
5748
let mut result: Vec<histories::History> = vec![];
5849
for h in self.histories {
59-
result.push(History::into(h));
50+
result.push(h.into());
6051
}
6152
histories::Histories { histories: result }
6253
}
6354
}
6455

56+
impl From<histories::Histories> for Histories {
57+
fn from(histories: histories::Histories) -> Histories {
58+
let mut result: Vec<History> = vec![];
59+
for h in histories.histories {
60+
result.push(History::from(h));
61+
}
62+
Self { histories: result }
63+
}
64+
}
65+
6566
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
6667
pub struct History {
6768
path: PathBuf,
6869
commands: Vec<HistoryCommand>,
6970
}
7071

7172
impl History {
72-
// TODO: impl as From trait
73-
fn from(history: histories::History) -> Self {
74-
let mut commands: Vec<HistoryCommand> = vec![];
75-
for h in history.commands {
76-
commands.push(HistoryCommand::from(h));
77-
}
78-
79-
History {
80-
path: history.path,
81-
commands,
82-
}
83-
}
84-
73+
// Implementing From<file::toml::History> on the model::History side is not desirable due to dependency direction, so the into method is manually defined.
8574
fn into(self) -> histories::History {
8675
let mut commands: Vec<histories::HistoryCommand> = vec![];
8776
for h in self.commands {
@@ -99,6 +88,20 @@ impl History {
9988
}
10089
}
10190

91+
impl From<histories::History> for History {
92+
fn from(history: histories::History) -> History {
93+
let mut commands: Vec<HistoryCommand> = vec![];
94+
for h in history.commands {
95+
commands.push(HistoryCommand::from(h));
96+
}
97+
98+
History {
99+
path: history.path,
100+
commands,
101+
}
102+
}
103+
}
104+
102105
/// toml representation of histories::HistoryCommand.
103106
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
104107
#[serde(rename_all = "kebab-case")]
@@ -112,14 +115,6 @@ impl HistoryCommand {
112115
Self { runner_type, args }
113116
}
114117

115-
// TODO: impl as From trait
116-
fn from(command: histories::HistoryCommand) -> Self {
117-
Self {
118-
runner_type: command.runner_type,
119-
args: command.args.clone(),
120-
}
121-
}
122-
123118
fn into(self) -> histories::HistoryCommand {
124119
histories::HistoryCommand {
125120
runner_type: self.runner_type,
@@ -128,6 +123,15 @@ impl HistoryCommand {
128123
}
129124
}
130125

126+
impl From<histories::HistoryCommand> for HistoryCommand {
127+
fn from(command: histories::HistoryCommand) -> HistoryCommand {
128+
Self {
129+
runner_type: command.runner_type,
130+
args: command.args.clone(),
131+
}
132+
}
133+
}
134+
131135
// TODO: should return Result not Option(returns when it fails to get the home dir)
132136
pub fn history_file_path() -> Option<(PathBuf, String)> {
133137
const HISTORY_FILE_NAME: &str = "history.toml";

src/model/runner_type.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,13 @@ pub enum JsPackageManager {
2222
impl RunnerType {
2323
pub fn to_runner(&self, runners: &Vec<runner::Runner>) -> Option<runner::Runner> {
2424
for r in runners {
25-
if self.clone() == RunnerType::from(r) {
25+
if self.clone() == RunnerType::from(r.clone()) {
2626
return Some(r.clone());
2727
}
2828
}
2929
None
3030
}
3131

32-
// TODO: impl as From trait
33-
pub fn from(runner: &runner::Runner) -> Self {
34-
match runner {
35-
runner::Runner::MakeCommand(_) => RunnerType::Make,
36-
runner::Runner::JsPackageManager(js) => match js {
37-
js::JsPackageManager::JsPnpm(_) => RunnerType::JsPackageManager(JsPackageManager::Pnpm),
38-
js::JsPackageManager::JsYarn(_) => RunnerType::JsPackageManager(JsPackageManager::Yarn),
39-
},
40-
runner::Runner::Just(_) => RunnerType::Just,
41-
}
42-
}
43-
4432
pub fn get_extension_for_highlighting(&self) -> &str {
4533
match self {
4634
RunnerType::Make => "mk",
@@ -52,6 +40,19 @@ impl RunnerType {
5240
}
5341
}
5442

43+
impl From<runner::Runner> for RunnerType {
44+
fn from(runner: runner::Runner) -> RunnerType {
45+
match runner {
46+
runner::Runner::MakeCommand(_) => RunnerType::Make,
47+
runner::Runner::JsPackageManager(js) => match js {
48+
js::JsPackageManager::JsPnpm(_) => RunnerType::JsPackageManager(JsPackageManager::Pnpm),
49+
js::JsPackageManager::JsYarn(_) => RunnerType::JsPackageManager(JsPackageManager::Yarn),
50+
},
51+
runner::Runner::Just(_) => RunnerType::Just,
52+
}
53+
}
54+
}
55+
5556
impl fmt::Display for RunnerType {
5657
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5758
let name = match self {

0 commit comments

Comments
 (0)