Skip to content

Commit

Permalink
refactor: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cjlee38 committed Sep 2, 2024
1 parent baeb66f commit b2e1c1e
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 21 deletions.
10 changes: 5 additions & 5 deletions gitnote-core/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::anyhow;
use colored::Colorize;
use unicode_width::UnicodeWidthStr;

use crate::io::NoteRepository;
use crate::repository::NoteRepository;
use crate::libgit::Libgit;
use crate::note::NoteLedger;
use crate::path::Paths;
Expand All @@ -23,7 +23,7 @@ where
}

pub fn add_note(&self, paths: &Paths, line: usize, message: String) -> anyhow::Result<()> {
let mut ledger = self.note_repository.read_note(paths)?;
let ledger = self.note_repository.read_note(paths)?;
if ledger.opaque_exists(line) {
return Err(anyhow!("comment already exists for line {} in {}. consider to use `edit` instead.", line + 1, paths));
}
Expand All @@ -38,7 +38,7 @@ where
}

pub fn edit_note(&self, paths: &Paths, line: usize, message: String) -> anyhow::Result<()> {
let mut ledger = self.note_repository.read_note(paths)?;
let ledger = self.note_repository.read_note(paths)?;
return if let Some(uuid) = ledger.opaque_uuid(line) {
println!("editing message found uuid {}", uuid);
ledger.edit(uuid, message);
Expand All @@ -50,7 +50,7 @@ where
}

pub fn delete_note(&self, paths: &Paths, line: usize) -> anyhow::Result<()> {
let mut ledger = self.note_repository.read_note(paths)?;
let ledger = self.note_repository.read_note(paths)?;

return if let Some(uuid) = ledger.opaque_uuid(line) {
ledger.delete(uuid);
Expand All @@ -66,7 +66,7 @@ where
mod tests {
use crate::diff::SimilarGitDiffer;
use crate::handlers::NoteHandler;
use crate::io::NoteRepository;
use crate::repository::NoteRepository;
use crate::libgit::{Libgit, ProcessLibgit};
use crate::note::Note;
use crate::path::{PathResolver, Paths};
Expand Down
13 changes: 11 additions & 2 deletions gitnote-core/src/libgit.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::{Path, PathBuf};
use std::process::Command;

use anyhow::{anyhow, Context};

use crate::diff::{DiffModel, GitDiffer};
use crate::io::read_file_content;
use crate::path::Paths;
use crate::utils::PathBufExt;

Expand Down Expand Up @@ -47,6 +48,14 @@ where
pub fn new(differ: T) -> Self {
Self { differ }
}

fn read_file_content(&self, paths: &Paths) -> anyhow::Result<String> {
let file = File::open(paths.canonical())?;
let mut reader = BufReader::new(file);
let mut content = String::new();
reader.read_to_string(&mut content)?;
return Ok(content);
}
}

impl<T> Libgit for ProcessLibgit<T>
Expand All @@ -58,7 +67,7 @@ where
paths.root(),
vec!["hash-object", "-w", paths.relative().try_to_str()?],
)?;
let content = read_file_content(paths)?;
let content = self.read_file_content(paths)?;
Ok(GitBlob {
id,
file_path: paths.relative().clone(),
Expand Down
4 changes: 2 additions & 2 deletions gitnote-core/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use std::process::ExitCode;
use crate::cli::Cli;
use crate::diff::SimilarGitDiffer;
use crate::handlers::NoteHandler;
use crate::io::NoteRepository;
use crate::repository::NoteRepository;
use crate::libgit::ProcessLibgit;
use crate::path::PathResolver;

mod argument;
mod handlers;
mod libgit;
mod io;
mod repository;
mod stdio;
mod note;
mod utils;
Expand Down
4 changes: 2 additions & 2 deletions gitnote-core/src/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ where
.map(|m| m.uuid.clone());
}

pub fn append(&mut self, line: usize, message: String) -> anyhow::Result<()> {
pub fn append(&self, line: usize, message: String) -> anyhow::Result<()> {
let blob = self.git_blob()?;
let message = Message::new(&blob, line, message)?;
self.note.borrow_mut().append(message)?;
return Ok(());
}

pub fn delete(&mut self, uuid: String) {
pub fn delete(&self, uuid: String) {
let mut note_ref = self.note.borrow_mut();
note_ref.messages.retain(|m| m.uuid != uuid);
}
Expand Down
10 changes: 1 addition & 9 deletions gitnote-core/src/io.rs → gitnote-core/src/repository.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fs::File;
use std::io::{BufReader, Read};
use std::io::BufReader;

use crate::diff::GitDiffer;
use crate::libgit::Libgit;
Expand Down Expand Up @@ -42,11 +42,3 @@ impl<T> NoteRepository<T> where T: Libgit {
return Ok(NoteLedger::new(paths, &self.libgit, note));
}
}

pub fn read_file_content(paths: &Paths) -> anyhow::Result<String> {
let file = File::open(paths.canonical())?;
let mut reader = BufReader::new(file);
let mut content = String::new();
reader.read_to_string(&mut content)?;
return Ok(content);
}
2 changes: 1 addition & 1 deletion gitnote-core/src/testlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use std::string::ToString;

use anyhow::{anyhow, Error};
use tempfile::tempdir_in;

use crate::note::Note;
use crate::path::Paths;

pub struct TestRepo {
_dir: tempfile::TempDir, // holds tempdir ref to delay cleanup
Expand Down

0 comments on commit b2e1c1e

Please sign in to comment.