chore: refactor branch and target readers to use generic reader type

This commit is contained in:
Nikita Galaiko 2023-12-20 14:50:16 +01:00 committed by GitButler
parent ab93d48e73
commit c032f01f82
6 changed files with 47 additions and 42 deletions

View File

@ -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<P: AsRef<path::Path>>(reader: &'reader dyn Reader, prefix: P) -> SubReader<'reader> {
impl<'r, R: Reader> SubReader<'r, R> {
pub fn new<P: AsRef<path::Path>>(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<R: Reader> Reader for SubReader<'_, R> {
fn read(&self, path: &path::Path) -> Result<Content, Error> {
self.reader.read(&self.prefix.join(path))
}

View File

@ -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 }
}

View File

@ -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<BranchId>,
}
impl<'iterator> BranchIterator<'iterator> {
pub fn new(reader: &'iterator dyn reader::Reader) -> Result<Self> {
impl<'i, R: crate::reader::Reader> BranchIterator<'i, R> {
pub fn new(reader: &'i R) -> Result<Self> {
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<R: crate::reader::Reader> Iterator for BranchIterator<'_, R> {
type Item = Result<branch::Branch, crate::reader::Error>;
fn next(&mut self) -> Option<Self::Item> {
@ -63,7 +61,7 @@ mod tests {
use once_cell::sync::Lazy;
use crate::{
sessions,
reader, sessions,
test_utils::{Case, Suite},
virtual_branches::target,
};

View File

@ -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<String, crate::reader::Error> {
fn read_remote_url<R: crate::reader::Reader>(reader: &R) -> Result<String, crate::reader::Error> {
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<String, crate::
}
// returns (remote_name, branch_name)
fn read_remote_name_branch_name(
reader: &dyn crate::reader::Reader,
fn read_remote_name_branch_name<R: crate::reader::Reader>(
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<Self, Self::Error> {
impl Target {
fn try_from<R: crate::reader::Reader>(reader: &R) -> Result<Target, crate::reader::Error> {
let (_, branch_name) = read_remote_name_branch_name(reader).map_err(|e| {
crate::reader::Error::Io(std::io::Error::new(
std::io::ErrorKind::Other,

View File

@ -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<Target, reader::Error> {
@ -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),
))
}
}

View File

@ -1118,9 +1118,13 @@ pub fn create_virtual_branch(
};
if let Some(ownership) = &create.ownership {
let branch_reader = branch::Reader::new(&current_session_reader);
set_ownership(&branch_reader, &branch_writer, &mut branch, ownership)
.context("failed to set ownership")?;
set_ownership(
&current_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(
&current_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<R: reader::Reader>(
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::<Result<Vec<branch::Branch>, reader::Error>>()
.context("failed to read virtual branches")?