Skip to content

Commit 9c5fed2

Browse files
committed
first few bits of error handling in parser (#450)
1 parent b9a4bdc commit 9c5fed2

File tree

3 files changed

+47
-17
lines changed

3 files changed

+47
-17
lines changed

git-refspec/src/parse.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,29 @@ pub enum Error {
88

99
pub(crate) mod function {
1010
use crate::parse::Error;
11-
use crate::{Operation, RefSpecRef};
12-
use bstr::BStr;
11+
use crate::{Mode, Operation, RefSpecRef};
12+
use bstr::{BStr, ByteSlice};
1313

1414
/// Parse `spec` for use in `operation` and return it if it is valid.
15-
pub fn parse(mut _spec: &BStr, _operation: Operation) -> Result<RefSpecRef<'_>, Error> {
16-
todo!()
15+
pub fn parse(mut spec: &BStr, _operation: Operation) -> Result<RefSpecRef<'_>, Error> {
16+
let mode = match spec.get(0) {
17+
Some(&b'^') => {
18+
spec = &spec[1..];
19+
Mode::Negative
20+
}
21+
Some(_) => Mode::Normal,
22+
None => return Err(Error::Empty),
23+
};
24+
25+
match spec.find_byte(b':') {
26+
Some(pos) => {
27+
let (_src, _dst) = spec.split_at(pos);
28+
if mode == Mode::Negative {
29+
return Err(Error::NegativeWithDestination);
30+
}
31+
todo!("with colon")
32+
}
33+
None => todo!("no colon"),
34+
}
1735
}
1836
}

git-refspec/tests/fixtures/make_baseline.sh

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ EOF
2626
baseline push ''
2727
baseline push '::'
2828
baseline fetch '::'
29+
baseline fetch '^a:'
30+
baseline fetch '^a:b'
31+
baseline fetch '^:'
32+
baseline fetch '^:b'
2933

3034
baseline push 'refs/heads/*:refs/remotes/frotz'
3135
baseline push 'refs/heads:refs/remotes/frotz/*'

git-refspec/tests/parse/mod.rs

+21-13
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,37 @@ fn baseline() {
77
}
88

99
mod invalid {
10-
use git_refspec::{parse, parse::Error, Operation};
10+
use crate::parse::try_parse;
11+
use git_refspec::{parse::Error, Operation};
1112

1213
#[test]
13-
#[ignore]
1414
fn empty() {
15-
assert!(matches!(parse("".into(), Operation::Fetch).unwrap_err(), Error::Empty));
16-
assert!(matches!(parse("".into(), Operation::Push).unwrap_err(), Error::Empty));
15+
assert!(matches!(try_parse("", Operation::Fetch).unwrap_err(), Error::Empty));
16+
assert!(matches!(try_parse("", Operation::Push).unwrap_err(), Error::Empty));
1717
}
1818

1919
#[test]
20-
#[ignore]
2120
fn negative_with_destination() {
22-
assert!(matches!(
23-
parse("^a:b".into(), Operation::Fetch).unwrap_err(),
24-
Error::NegativeWithDestination
25-
));
26-
assert!(matches!(
27-
parse("a:b".into(), Operation::Fetch).unwrap_err(),
28-
Error::NegativeWithDestination
29-
));
21+
for op in [Operation::Fetch, Operation::Push] {
22+
for spec in ["^a:b", "^a:", "^:", "^:b"] {
23+
assert!(matches!(
24+
try_parse(spec, op).unwrap_err(),
25+
Error::NegativeWithDestination
26+
));
27+
}
28+
}
3029
}
3130

3231
mod fetch {}
3332

3433
mod push {}
3534
}
35+
36+
mod util {
37+
use git_refspec::{Operation, RefSpecRef};
38+
39+
pub fn try_parse(spec: &str, op: Operation) -> Result<RefSpecRef<'_>, git_refspec::parse::Error> {
40+
git_refspec::parse(spec.into(), op)
41+
}
42+
}
43+
pub use util::*;

0 commit comments

Comments
 (0)