1use std::ffi::CString;
2use std::marker;
3use std::str;
4
5use crate::util::Binding;
6use crate::{raw, Buf, Direction, Error};
7
8pub struct Refspec<'remote> {
14 raw: *const raw::git_refspec,
15 _marker: marker::PhantomData<&'remote raw::git_remote>,
16}
17
18impl<'remote> Refspec<'remote> {
19 pub fn direction(&self) -> Direction {
21 match unsafe { raw::git_refspec_direction(self.raw) } {
22 raw::GIT_DIRECTION_FETCH => Direction::Fetch,
23 raw::GIT_DIRECTION_PUSH => Direction::Push,
24 n => panic!("unknown refspec direction: {}", n),
25 }
26 }
27
28 pub fn dst(&self) -> Result<&str, Error> {
30 str::from_utf8(self.dst_bytes()).map_err(|e| e.into())
31 }
32
33 pub fn dst_bytes(&self) -> &[u8] {
35 unsafe { crate::opt_bytes(self, raw::git_refspec_dst(self.raw)).unwrap() }
36 }
37
38 pub fn dst_matches(&self, refname: &str) -> bool {
40 let refname = CString::new(refname).unwrap();
41 unsafe { raw::git_refspec_dst_matches(self.raw, refname.as_ptr()) == 1 }
42 }
43
44 pub fn src(&self) -> Result<&str, Error> {
46 str::from_utf8(self.src_bytes()).map_err(|e| e.into())
47 }
48
49 pub fn src_bytes(&self) -> &[u8] {
51 unsafe { crate::opt_bytes(self, raw::git_refspec_src(self.raw)).unwrap() }
52 }
53
54 pub fn src_matches(&self, refname: &str) -> bool {
56 let refname = CString::new(refname).unwrap();
57 unsafe { raw::git_refspec_src_matches(self.raw, refname.as_ptr()) == 1 }
58 }
59
60 pub fn is_force(&self) -> bool {
62 unsafe { raw::git_refspec_force(self.raw) == 1 }
63 }
64
65 pub fn str(&self) -> Result<&str, Error> {
67 str::from_utf8(self.bytes()).map_err(|e| e.into())
68 }
69
70 pub fn bytes(&self) -> &[u8] {
72 unsafe { crate::opt_bytes(self, raw::git_refspec_string(self.raw)).unwrap() }
73 }
74
75 pub fn transform(&self, name: &str) -> Result<Buf, Error> {
77 let name = CString::new(name).unwrap();
78 unsafe {
79 let buf = Buf::new();
80 try_call!(raw::git_refspec_transform(
81 buf.raw(),
82 self.raw,
83 name.as_ptr()
84 ));
85 Ok(buf)
86 }
87 }
88
89 pub fn rtransform(&self, name: &str) -> Result<Buf, Error> {
91 let name = CString::new(name).unwrap();
92 unsafe {
93 let buf = Buf::new();
94 try_call!(raw::git_refspec_rtransform(
95 buf.raw(),
96 self.raw,
97 name.as_ptr()
98 ));
99 Ok(buf)
100 }
101 }
102}
103
104impl<'remote> Binding for Refspec<'remote> {
105 type Raw = *const raw::git_refspec;
106
107 unsafe fn from_raw(raw: *const raw::git_refspec) -> Refspec<'remote> {
108 Refspec {
109 raw,
110 _marker: marker::PhantomData,
111 }
112 }
113 fn raw(&self) -> *const raw::git_refspec {
114 self.raw
115 }
116}