FIXED TODO: Avoided take() function on the run editor method#11
Open
rrsmart8 wants to merge 3 commits intoUPB-CS-OpenSourceUpstream:masterfrom
Open
FIXED TODO: Avoided take() function on the run editor method#11rrsmart8 wants to merge 3 commits intoUPB-CS-OpenSourceUpstream:masterfrom
rrsmart8 wants to merge 3 commits intoUPB-CS-OpenSourceUpstream:masterfrom
Conversation
rrsmart8
commented
Jan 5, 2024
rrsmart8
commented
Jan 5, 2024
Author
rrsmart8
left a comment
There was a problem hiding this comment.
Handled the case where the file already exists when the user tries to save_as
alexandruradovici
requested changes
Jan 7, 2024
alexandruradovici
left a comment
There was a problem hiding this comment.
Please describe your changes in the pull request description.
| /// attribute of the editor will be set and syntax highlighting will be updated. | ||
| fn save_as(&mut self, file_name: String) -> Result<(), Error> { | ||
| // TODO: What if file_name already exists? | ||
|
|
| self.save_and_handle_io_errors(&file_name); | ||
| self.file_name = Some(file_name); | ||
| Key::Char(SAVE) => { | ||
| match mem::replace(&mut self.file_name, None) { |
There was a problem hiding this comment.
This has the same effect as take, I would leave the take function in place has it adds more meaning. Your solution also adds an extra line of code.
There was a problem hiding this comment.
I think you could avoid using take by borrowing the value in the match arm:
Key::Char(SAVE) => match self.file_name {
// TODO: Can we avoid using take() then reassigning the value to file_name?
Some(ref file_name) => {
self.save_and_handle_io_errors(file_name);
self.file_name = Some(file_name);
// ...| /// Will Return `Err` if any error occur. | ||
| pub fn run(&mut self, file_name: &Option<String>) -> Result<(), Error> { | ||
| if let Some(path) = file_name.as_ref().map(|p| sys::path(p.as_str())) { | ||
| if let Some(path) = file_name.as_ref().and_then(|p| Some(sys::path(p.as_str()))) { |
| self.prompt_mode = match self.prompt_mode.take() { | ||
| // process_keypress returns (should_quit, prompt_mode) | ||
|
|
||
| self.prompt_mode = match self.prompt_mode.map_or_else(|| None, |pm| Some(pm.process_keypress(self, &key)?)) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.