Skip to content

Commit

Permalink
cleanup crate structure (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Aug 7, 2022
1 parent 6278966 commit f0163c9
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 89 deletions.
64 changes: 64 additions & 0 deletions git-refspec/src/instruction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use crate::{parse::Operation, Instruction};
use bstr::BStr;

impl Instruction<'_> {
/// Derive the mode of operation from this instruction.
pub fn operation(&self) -> Operation {
match self {
Instruction::Push(_) => Operation::Push,
Instruction::Fetch(_) => Operation::Fetch,
}
}
}

/// Note that all sources can either be a ref-name, partial or full, or a rev-spec, unless specified otherwise, on the local side.
/// Destinations can only be a partial or full ref names on the remote side.
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug)]
pub enum Push<'a> {
/// Push all local branches to the matching destination on the remote, which has to exist to be updated.
AllMatchingBranches {
/// If true, allow non-fast-forward updates of the matched destination branch.
allow_non_fast_forward: bool,
},
/// Delete the destination ref or glob pattern, with only a single `*` allowed.
Delete {
/// The reference or pattern to delete on the remote.
ref_or_pattern: &'a BStr,
},
/// Push a single ref or refspec to a known destination ref.
Matching {
/// The source ref or refspec to push. If pattern, it contains a single `*`.
src: &'a BStr,
/// The ref to update with the object from `src`. If `src` is a pattern, this is a pattern too.
dst: &'a BStr,
/// If true, allow non-fast-forward updates of `dest`.
allow_non_fast_forward: bool,
},
}

/// Any source can either be a ref name (full or partial) or a fully spelled out hex-sha for an object, on the remote side.
///
/// Destinations can only be a partial or full ref-names on the local side.
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug)]
pub enum Fetch<'a> {
/// Fetch a ref or refs and write the result into the `FETCH_HEAD` without updating local branches.
Only {
/// The ref name to fetch on the remote side, without updating the local side. This will write the result into `FETCH_HEAD`.
src: &'a BStr,
},
/// Exclude a single ref.
Exclude {
/// A single partial or full ref name to exclude on the remote, or a pattern with a single `*`. It cannot be a spelled out object hash.
src: &'a BStr,
},
/// Fetch from `src` and update the corresponding destination branches in `dst` accordingly.
AndUpdate {
/// The ref name to fetch on the remote side, or a pattern with a single `*` to match against.
src: &'a BStr,
/// The local destination to update with what was fetched, or a pattern whose single `*` will be replaced with the matching portion
/// of the `*` from `src`.
dst: &'a BStr,
/// If true, allow non-fast-forward updates of `dest`.
allow_non_fast_forward: bool,
},
}
9 changes: 6 additions & 3 deletions git-refspec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
pub mod parse;
pub use parse::function::parse;

///
pub mod instruction;

/// A refspec with references to the memory it was parsed from.
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug)]
pub struct RefSpecRef<'a> {
mode: types::Mode,
op: Operation,
op: parse::Operation,
src: Option<&'a bstr::BStr>,
dst: Option<&'a bstr::BStr>,
}
Expand All @@ -19,12 +22,12 @@ pub struct RefSpecRef<'a> {
#[derive(PartialEq, Eq, Clone, Hash, Debug)]
pub struct RefSpec {
mode: types::Mode,
op: Operation,
op: parse::Operation,
src: Option<bstr::BString>,
dst: Option<bstr::BString>,
}

mod spec;

mod types;
pub use types::{Fetch, Instruction, Operation, Push};
pub use types::Instruction;
11 changes: 10 additions & 1 deletion git-refspec/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,18 @@ pub enum Error {
RevSpec(#[from] git_revision::spec::parse::Error),
}

/// Define how the parsed refspec should be used.
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug)]
pub enum Operation {
/// The `src` side is local and the `dst` side is remote.
Push,
/// The `src` side is remote and the `dst` side is local.
Fetch,
}

pub(crate) mod function {
use crate::parse::Error;
use crate::{types::Mode, Operation, RefSpecRef};
use crate::{parse::Operation, types::Mode, RefSpecRef};
use bstr::{BStr, ByteSlice};

/// Parse `spec` for use in `operation` and return it if it is valid.
Expand Down
14 changes: 9 additions & 5 deletions git-refspec/src/spec.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::types::Push;
use crate::{types::Mode, Fetch, Instruction, Operation, RefSpec, RefSpecRef};
use crate::instruction::{Fetch, Push};
use crate::{parse::Operation, types::Mode, Instruction, RefSpec, RefSpecRef};

/// Conversion. Use the [RefSpecRef][RefSpec::to_ref()] type for more usage options.
impl RefSpec {
Expand All @@ -14,9 +14,13 @@ impl RefSpec {
}
}

impl Into<RefSpec> for RefSpecRef<'_> {
fn into(self) -> RefSpec {
self.to_owned()
mod impls {
use crate::{RefSpec, RefSpecRef};

impl Into<RefSpec> for RefSpecRef<'_> {
fn into(self) -> RefSpec {
self.to_owned()
}
}
}

Expand Down
79 changes: 4 additions & 75 deletions git-refspec/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bstr::BStr;
use crate::instruction;

/// The way to interpret a refspec.
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug)]
Expand All @@ -11,82 +11,11 @@ pub(crate) enum Mode {
Negative,
}

/// What operation to perform with the refspec.
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug)]
pub enum Operation {
/// The `src` side is local and the `dst` side is remote.
Push,
/// The `src` side is remote and the `dst` side is local.
Fetch,
}

/// Tells what to do and is derived from a [`RefSpec`][RefSpecRef].
/// Tells what to do and is derived from a [`RefSpec`][crate::RefSpecRef].
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug)]
pub enum Instruction<'a> {
/// An instruction for pushing.
Push(Push<'a>),
Push(instruction::Push<'a>),
/// An instruction for fetching.
Fetch(Fetch<'a>),
}

/// Note that all sources can either be a ref-name, partial or full, or a rev-spec, unless specified otherwise, on the local side.
/// Destinations can only be a partial or full ref names on the remote side.
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug)]
pub enum Push<'a> {
/// Push all local branches to the matching destination on the remote, which has to exist to be updated.
AllMatchingBranches {
/// If true, allow non-fast-forward updates of the matched destination branch.
allow_non_fast_forward: bool,
},
/// Delete the destination ref or glob pattern, with only a single `*` allowed.
Delete {
/// The reference or pattern to delete on the remote.
ref_or_pattern: &'a BStr,
},
/// Push a single ref or refspec to a known destination ref.
Matching {
/// The source ref or refspec to push. If pattern, it contains a single `*`.
src: &'a BStr,
/// The ref to update with the object from `src`. If `src` is a pattern, this is a pattern too.
dst: &'a BStr,
/// If true, allow non-fast-forward updates of `dest`.
allow_non_fast_forward: bool,
},
}

/// Note that any source can either be a ref name (full or partial) or a fully spelled out hex-sha for an object, on the remote side.
///
/// Destinations can only be a partial or full ref-names on the local side.
#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug)]
pub enum Fetch<'a> {
/// Fetch a ref or refs and write the result into the `FETCH_HEAD` without updating local branches.
Only {
/// The ref name to fetch on the remote side, without updating the local side. This will write the result into `FETCH_HEAD`.
src: &'a BStr,
},
/// Exclude a single ref.
Exclude {
/// A single partial or full ref name to exclude on the remote, or a pattern with a single `*`. It cannot be a spelled out object hash.
src: &'a BStr,
},
/// Fetch from `src` and update the corresponding destination branches in `dst` accordingly.
AndUpdate {
/// The ref name to fetch on the remote side, or a pattern with a single `*` to match against.
src: &'a BStr,
/// The local destination to update with what was fetched, or a pattern whose single `*` will be replaced with the matching portion
/// of the `*` from `src`.
dst: &'a BStr,
/// If true, allow non-fast-forward updates of `dest`.
allow_non_fast_forward: bool,
},
}

impl Instruction<'_> {
/// Derive the mode of operation from this instruction.
pub fn operation(&self) -> Operation {
match self {
Instruction::Push(_) => Operation::Push,
Instruction::Fetch(_) => Operation::Fetch,
}
}
Fetch(instruction::Fetch<'a>),
}
2 changes: 1 addition & 1 deletion git-refspec/tests/parse/fetch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::parse::{assert_parse, b, try_parse};
use git_refspec::{parse::Error, Fetch, Instruction, Operation};
use git_refspec::{instruction::Fetch, parse::Error, parse::Operation, Instruction};

#[test]
fn revspecs_are_disallowed() {
Expand Down
2 changes: 1 addition & 1 deletion git-refspec/tests/parse/invalid.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::parse::try_parse;
use git_refspec::{parse::Error, Operation};
use git_refspec::{parse::Error, parse::Operation};

#[test]
fn empty() {
Expand Down
4 changes: 2 additions & 2 deletions git-refspec/tests/parse/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bstr::ByteSlice;
use git_refspec::Operation;
use git_refspec::parse::Operation;
use git_testtools::scripted_fixture_repo_read_only;
use std::panic::catch_unwind;

Expand Down Expand Up @@ -61,7 +61,7 @@ mod invalid;
mod push;

mod util {
use git_refspec::{Instruction, Operation, RefSpecRef};
use git_refspec::{parse::Operation, Instruction, RefSpecRef};

pub fn b(input: &str) -> &bstr::BStr {
input.into()
Expand Down
2 changes: 1 addition & 1 deletion git-refspec/tests/parse/push.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::parse::{assert_parse, b, try_parse};
use git_refspec::{parse::Error, Instruction, Operation, Push};
use git_refspec::{instruction::Push, parse::Error, parse::Operation, Instruction};

#[test]
fn negative_unsupported() {
Expand Down

0 comments on commit f0163c9

Please sign in to comment.