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 code #70

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

refactor code #70

wants to merge 1 commit into from

Conversation

LorenzSchueler
Copy link

@LorenzSchueler LorenzSchueler commented Nov 26, 2023

While debugging a problem (which turns out to be caused by my password manager) I just started to refactor the code to make it easier to read and understand.
The main changes are:

  • replace custom Stack by std::vec::Vec
  • make fields private and use constructors for costruction of types
  • make more use of iterator adapters
  • apply lints

Not sure if you want any of those changes. In case you only want some of them I can also revert the others.

use Vec, make fields private, use constructors, use iterator adapters, apply lints
@Narigo
Copy link
Owner

Narigo commented Jan 30, 2024

Sorry for not getting back to this. I've only had time to skim over the changes as there was lots of private and work related stuff going on. There are lots of nice improvements regarding the code and where I obviously didn't know about some functions or how to do it properly with Rust. I definitely want to understand what has been changed and incorporate things like this into the code. It's a bit much for one PR, so I have postponed properly checking it for forever. I hope in a few weeks from now I have more time for this project here again and will be able to get this code base and everything around it more polished into something that can be downloaded and used.

@mrghosti3
Copy link

I am also working on similar changes using cargo clippy but I for now am trying not to touch main.rs as I believe there should be a bigger refactor on execution flow and maybe replace some variable declarations. Good examples would be:

  • Replace String in favor of Box<str> as the former is a struct that is approximately usize * 3 (pointer + current length + capacity) and the latter is just a fat pointer (pointer + len). Also after initial command line read, the variables are no longer mutated so we don't methods for extending the allocation.
  • Find a way to less data cloning and use more referencing as each clone op is O(n) where n is equal to password length.

@@ -34,26 +68,14 @@ impl Diff for Field {

impl std::fmt::Display for Field {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let password_str = match (self.mask_passwords, self.kind) {
(true, ValueType::Protected) => "***",
_ => self.value.as_str(),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be written as follows:

- _ => self.value.as_str(),
+ _ => &self.value,

}

pub fn value(&self) -> &str {
self.value.as_str()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be written as follows:

- self.value.as_str(),
+ &self.value,

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

Successfully merging this pull request may close these issues.

3 participants