From c032f01f8231938220ec864696fb74a18e461451 Mon Sep 17 00:00:00 2001 From: Nikita Galaiko Date: Wed, 20 Dec 2023 14:50:16 +0100 Subject: [PATCH] chore: refactor branch and target readers to use generic reader type --- gitbutler-app/src/reader.rs | 10 ++++---- .../src/virtual_branches/branch/reader.rs | 10 ++++---- .../src/virtual_branches/iterator.rs | 14 +++++------ gitbutler-app/src/virtual_branches/target.rs | 12 ++++----- .../src/virtual_branches/target/reader.rs | 18 ++++++------- gitbutler-app/src/virtual_branches/virtual.rs | 25 +++++++++++++------ 6 files changed, 47 insertions(+), 42 deletions(-) diff --git a/gitbutler-app/src/reader.rs b/gitbutler-app/src/reader.rs index 41cc41980..ae579d233 100644 --- a/gitbutler-app/src/reader.rs +++ b/gitbutler-app/src/reader.rs @@ -129,13 +129,13 @@ impl Reader for CommitReader<'_> { } } -pub struct SubReader<'reader> { - reader: &'reader dyn Reader, +pub struct SubReader<'r, R: Reader> { + reader: &'r R, prefix: path::PathBuf, } -impl<'reader> SubReader<'reader> { - pub fn new>(reader: &'reader dyn Reader, prefix: P) -> SubReader<'reader> { +impl<'r, R: Reader> SubReader<'r, R> { + pub fn new>(reader: &'r R, prefix: P) -> Self { SubReader { reader, prefix: prefix.as_ref().to_path_buf(), @@ -143,7 +143,7 @@ impl<'reader> SubReader<'reader> { } } -impl Reader for SubReader<'_> { +impl Reader for SubReader<'_, R> { fn read(&self, path: &path::Path) -> Result { self.reader.read(&self.prefix.join(path)) } diff --git a/gitbutler-app/src/virtual_branches/branch/reader.rs b/gitbutler-app/src/virtual_branches/branch/reader.rs index 39c78d195..1ebbdd71c 100644 --- a/gitbutler-app/src/virtual_branches/branch/reader.rs +++ b/gitbutler-app/src/virtual_branches/branch/reader.rs @@ -1,15 +1,15 @@ use std::path; -use crate::reader::{self, Reader, SubReader}; +use crate::reader::{self, SubReader}; use super::{Branch, BranchId}; -pub struct BranchReader<'reader> { - reader: &'reader dyn reader::Reader, +pub struct BranchReader<'r, R: crate::reader::Reader> { + reader: &'r R, } -impl<'reader> BranchReader<'reader> { - pub fn new(reader: &'reader dyn Reader) -> Self { +impl<'r, R: crate::reader::Reader> BranchReader<'r, R> { + pub fn new(reader: &'r R) -> Self { Self { reader } } diff --git a/gitbutler-app/src/virtual_branches/iterator.rs b/gitbutler-app/src/virtual_branches/iterator.rs index b2adabcb5..4e6aad979 100644 --- a/gitbutler-app/src/virtual_branches/iterator.rs +++ b/gitbutler-app/src/virtual_branches/iterator.rs @@ -2,17 +2,15 @@ use std::{collections::HashSet, path}; use anyhow::Result; -use crate::reader; - use super::branch::{self, BranchId}; -pub struct BranchIterator<'iterator> { - branch_reader: branch::Reader<'iterator>, +pub struct BranchIterator<'i, R: crate::reader::Reader> { + branch_reader: branch::Reader<'i, R>, ids: Vec, } -impl<'iterator> BranchIterator<'iterator> { - pub fn new(reader: &'iterator dyn reader::Reader) -> Result { +impl<'i, R: crate::reader::Reader> BranchIterator<'i, R> { + pub fn new(reader: &'i R) -> Result { let ids_itarator = reader .list_files(&path::PathBuf::from("branches"))? .into_iter() @@ -41,7 +39,7 @@ impl<'iterator> BranchIterator<'iterator> { } } -impl<'iterator> Iterator for BranchIterator<'iterator> { +impl Iterator for BranchIterator<'_, R> { type Item = Result; fn next(&mut self) -> Option { @@ -63,7 +61,7 @@ mod tests { use once_cell::sync::Lazy; use crate::{ - sessions, + reader, sessions, test_utils::{Case, Suite}, virtual_branches::target, }; diff --git a/gitbutler-app/src/virtual_branches/target.rs b/gitbutler-app/src/virtual_branches/target.rs index a7cbb51aa..6bd5d25c3 100644 --- a/gitbutler-app/src/virtual_branches/target.rs +++ b/gitbutler-app/src/virtual_branches/target.rs @@ -34,7 +34,7 @@ impl Serialize for Target { } // this is a backwards compatibile with the old format -fn read_remote_url(reader: &dyn crate::reader::Reader) -> Result { +fn read_remote_url(reader: &R) -> Result { match reader.read(&path::PathBuf::from("remote_url")) { Ok(url) => Ok(url.try_into()?), // fallback to the old format @@ -46,8 +46,8 @@ fn read_remote_url(reader: &dyn crate::reader::Reader) -> Result( + reader: &R, ) -> Result<(String, String), crate::reader::Error> { match reader.read(&path::PathBuf::from("name")) { Ok(branch) => { @@ -69,10 +69,8 @@ fn read_remote_name_branch_name( } } -impl TryFrom<&dyn crate::reader::Reader> for Target { - type Error = crate::reader::Error; - - fn try_from(reader: &dyn crate::reader::Reader) -> Result { +impl Target { + fn try_from(reader: &R) -> Result { let (_, branch_name) = read_remote_name_branch_name(reader).map_err(|e| { crate::reader::Error::Io(std::io::Error::new( std::io::ErrorKind::Other, diff --git a/gitbutler-app/src/virtual_branches/target/reader.rs b/gitbutler-app/src/virtual_branches/target/reader.rs index eee63d6fe..31ce6d36c 100644 --- a/gitbutler-app/src/virtual_branches/target/reader.rs +++ b/gitbutler-app/src/virtual_branches/target/reader.rs @@ -7,12 +7,12 @@ use crate::{ use super::Target; -pub struct TargetReader<'reader> { - reader: &'reader dyn reader::Reader, +pub struct TargetReader<'r, R: crate::reader::Reader> { + reader: &'r R, } -impl<'reader> TargetReader<'reader> { - pub fn new(reader: &'reader dyn reader::Reader) -> Self { +impl<'r, R: crate::reader::Reader> TargetReader<'r, R> { + pub fn new(reader: &'r R) -> Self { Self { reader } } @@ -21,8 +21,7 @@ impl<'reader> TargetReader<'reader> { return Err(reader::Error::NotFound); } - let reader: &dyn crate::reader::Reader = &SubReader::new(self.reader, "branches/target"); - Target::try_from(reader) + Target::try_from(&SubReader::new(self.reader, "branches/target")) } pub fn read(&self, id: &BranchId) -> Result { @@ -33,9 +32,10 @@ impl<'reader> TargetReader<'reader> { return self.read_default(); } - let reader: &dyn crate::reader::Reader = - &SubReader::new(self.reader, &format!("branches/{}/target", id)); - Target::try_from(reader) + Target::try_from(&SubReader::new( + self.reader, + &format!("branches/{}/target", id), + )) } } diff --git a/gitbutler-app/src/virtual_branches/virtual.rs b/gitbutler-app/src/virtual_branches/virtual.rs index 33483cf5d..f889ae601 100644 --- a/gitbutler-app/src/virtual_branches/virtual.rs +++ b/gitbutler-app/src/virtual_branches/virtual.rs @@ -1118,9 +1118,13 @@ pub fn create_virtual_branch( }; if let Some(ownership) = &create.ownership { - let branch_reader = branch::Reader::new(¤t_session_reader); - set_ownership(&branch_reader, &branch_writer, &mut branch, ownership) - .context("failed to set ownership")?; + set_ownership( + ¤t_session_reader, + &branch_writer, + &mut branch, + ownership, + ) + .context("failed to set ownership")?; } branch_writer @@ -1321,8 +1325,13 @@ pub fn update_branch( })?; if let Some(ownership) = branch_update.ownership { - set_ownership(&branch_reader, &branch_writer, &mut branch, &ownership) - .context("failed to set ownership")?; + set_ownership( + ¤t_session_reader, + &branch_writer, + &mut branch, + &ownership, + ) + .context("failed to set ownership")?; } if let Some(name) = branch_update.name { @@ -1409,8 +1418,8 @@ pub fn delete_branch( Ok(()) } -fn set_ownership( - branch_reader: &branch::Reader, +fn set_ownership( + branch_reader: &R, branch_writer: &branch::Writer, target_branch: &mut branch::Branch, ownership: &branch::Ownership, @@ -1420,7 +1429,7 @@ fn set_ownership( return Ok(()); } - let mut virtual_branches = Iterator::new(branch_reader.reader()) + let mut virtual_branches = Iterator::new(branch_reader) .context("failed to create branch iterator")? .collect::, reader::Error>>() .context("failed to read virtual branches")?