Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor custom 'display()' functions to use the 'Display' trait #88

Closed
3 tasks
chshersh opened this issue Sep 15, 2022 · 2 comments · Fixed by #99
Closed
3 tasks

Refactor custom 'display()' functions to use the 'Display' trait #88

chshersh opened this issue Sep 15, 2022 · 2 comments · Fixed by #99
Assignees
Labels

Comments

@chshersh
Copy link
Owner

chshersh commented Sep 15, 2022

See the discussion in #83

The idea is to implement the std::fmt::Display trait for data types where we already use a custom display() function. Specifically:

  • TomlError::display
    • impl TomlError {
      pub fn display(&self) -> String {
      match self {
      TomlError::IO(e) => format!("[IO Error] {}", e),
      TomlError::Parse(e) => format!("[Parsing Error] {}", e),
      TomlError::Decode => "[Decode Error]".to_string(),
      }
      }
      }
  • ToolError::display
    • impl ToolError {
      pub fn display(&self) -> String {
      match self {
      ToolError::Suggestion { perhaps } => {
      format!("[suggestion] Perhaps you meant: '{}'?", perhaps)
      }
      ToolError::Invalid => "[error] Not detailed enough configuration".to_string(),
      }
      }
      }
  • UnpackError::display
    • impl UnpackError {
      pub fn display(&self) -> String {
      match self {
      UnpackError::IOError(e) => format!("{}", e),
      UnpackError::ZipError(e) => format!("{}", e),
      UnpackError::ExeNotFound(archive_name) => {
      format!("Can't find executable in archive: {}", archive_name)
      }
      }
      }
      }

Implementation example:

enum TestEnum {
    Var1(String),
    Var2(String),
}


impl fmt::Display for TestEnum {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            TestEnum::Var1(s) => write!(f, "[VAR1]: {}", s),
            TestEnum::Var2(s) => write!(f, "[VAR2]: {}", s),
        }
    }
}
@zixuan-x
Copy link
Contributor

Sicne I just worked with implementing Display trait, this issue is probably a good one for me to get more familiar with the code base. If it's not taken then I'll start working on it?

@chshersh
Copy link
Owner Author

@zixuanzhang-x Sounds like an excellent next issue 🥇

It's not taken. And I think @MitchellBerend will be really pleased when this one is done so we can unlock:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants