Skip to content

Commit 9898340

Browse files
author
Anciety
committed
drop Eq impl, use arg_types to compare types.
1 parent f388f68 commit 9898340

File tree

2 files changed

+10
-27
lines changed

2 files changed

+10
-27
lines changed

src/command.rs

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,6 @@ pub struct Command<'a> {
2727
pub handler: Box<Handler<'a>>,
2828
}
2929

30-
/// Command equals when types equal
31-
impl<'a> std::cmp::PartialEq for Command<'a> {
32-
fn eq(&self, other: &Self) -> bool {
33-
if self.args_info.len() != other.args_info.len() {
34-
return false;
35-
}
36-
37-
for i in 0..self.args_info.len() {
38-
let my_arg = &self.args_info[i];
39-
let other_arg = &other.args_info[i];
40-
41-
let my_type_str = my_arg.split(":").collect::<Vec<_>>()[1];
42-
let other_type_str = other_arg.split(":").collect::<Vec<_>>()[1];
43-
44-
if my_type_str != other_type_str {
45-
return false;
46-
}
47-
}
48-
49-
true
50-
}
51-
}
52-
53-
impl<'a> std::cmp::Eq for Command<'a> {}
54-
5530
/// Return status of a command.
5631
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5732
pub enum CommandStatus {
@@ -121,6 +96,14 @@ impl<'a> Command<'a> {
12196
pub fn run(&mut self, args: &[&str]) -> anyhow::Result<CommandStatus> {
12297
(self.handler)(args)
12398
}
99+
100+
/// Returns the string description of the argument types
101+
pub fn arg_types(&self) -> Vec<&str> {
102+
self.args_info
103+
.iter()
104+
.map(|info| info.split(":").collect::<Vec<_>>()[1])
105+
.collect()
106+
}
124107
}
125108

126109
impl<'a> std::fmt::Debug for Command<'a> {

src/repl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl<'a> ReplBuilder<'a> {
183183
return Err(BuilderError::InvalidName(name));
184184
} else if RESERVED.iter().any(|(n, _)| *n == name) {
185185
return Err(BuilderError::ReservedName(name));
186-
} else if cmds.iter().any(|c| *c == cmd) {
186+
} else if cmds.iter().any(|c| c.arg_types() == cmd.arg_types()) {
187187
return Err(BuilderError::DuplicateCommands(name));
188188
}
189189
cmds.push(cmd);
@@ -325,7 +325,7 @@ Other commands:
325325
let cmds = self.commands.get_mut(name).unwrap();
326326
writeln!(&mut self.out, "Usage:")?;
327327
for cmd in cmds.iter() {
328-
writeln!(&mut self.out, "{} {}", name, cmd.args_info.join(" "))?;
328+
writeln!(&mut self.out, " {} {}", name, cmd.args_info.join(" "))?;
329329
}
330330
}
331331
Ok(LoopStatus::Continue)

0 commit comments

Comments
 (0)