Skip to content

Commit

Permalink
first few bits of error handling in parser (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Aug 5, 2022
1 parent b9a4bdc commit 9c5fed2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
26 changes: 22 additions & 4 deletions git-refspec/src/parse.rs
Expand Up @@ -8,11 +8,29 @@ pub enum Error {

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

/// Parse `spec` for use in `operation` and return it if it is valid.
pub fn parse(mut _spec: &BStr, _operation: Operation) -> Result<RefSpecRef<'_>, Error> {
todo!()
pub fn parse(mut spec: &BStr, _operation: Operation) -> Result<RefSpecRef<'_>, Error> {
let mode = match spec.get(0) {
Some(&b'^') => {
spec = &spec[1..];
Mode::Negative
}
Some(_) => Mode::Normal,
None => return Err(Error::Empty),
};

match spec.find_byte(b':') {
Some(pos) => {
let (_src, _dst) = spec.split_at(pos);
if mode == Mode::Negative {
return Err(Error::NegativeWithDestination);
}
todo!("with colon")
}
None => todo!("no colon"),
}
}
}
4 changes: 4 additions & 0 deletions git-refspec/tests/fixtures/make_baseline.sh
Expand Up @@ -26,6 +26,10 @@ EOF
baseline push ''
baseline push '::'
baseline fetch '::'
baseline fetch '^a:'
baseline fetch '^a:b'
baseline fetch '^:'
baseline fetch '^:b'

baseline push 'refs/heads/*:refs/remotes/frotz'
baseline push 'refs/heads:refs/remotes/frotz/*'
Expand Down
34 changes: 21 additions & 13 deletions git-refspec/tests/parse/mod.rs
Expand Up @@ -7,29 +7,37 @@ fn baseline() {
}

mod invalid {
use git_refspec::{parse, parse::Error, Operation};
use crate::parse::try_parse;
use git_refspec::{parse::Error, Operation};

#[test]
#[ignore]
fn empty() {
assert!(matches!(parse("".into(), Operation::Fetch).unwrap_err(), Error::Empty));
assert!(matches!(parse("".into(), Operation::Push).unwrap_err(), Error::Empty));
assert!(matches!(try_parse("", Operation::Fetch).unwrap_err(), Error::Empty));
assert!(matches!(try_parse("", Operation::Push).unwrap_err(), Error::Empty));
}

#[test]
#[ignore]
fn negative_with_destination() {
assert!(matches!(
parse("^a:b".into(), Operation::Fetch).unwrap_err(),
Error::NegativeWithDestination
));
assert!(matches!(
parse("a:b".into(), Operation::Fetch).unwrap_err(),
Error::NegativeWithDestination
));
for op in [Operation::Fetch, Operation::Push] {
for spec in ["^a:b", "^a:", "^:", "^:b"] {
assert!(matches!(
try_parse(spec, op).unwrap_err(),
Error::NegativeWithDestination
));
}
}
}

mod fetch {}

mod push {}
}

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

pub fn try_parse(spec: &str, op: Operation) -> Result<RefSpecRef<'_>, git_refspec::parse::Error> {
git_refspec::parse(spec.into(), op)
}
}
pub use util::*;

0 comments on commit 9c5fed2

Please sign in to comment.