| 1 |
use std::ffi::CString; |
| 2 |
use std::fmt; |
| 3 |
use std::marker; |
| 4 |
use std::mem; |
| 5 |
use std::ptr; |
| 6 |
use std::str; |
| 7 |
|
| 8 |
use crate::util::Binding; |
| 9 |
use crate::{raw, Error, Time}; |
| 10 |
|
| 11 |
/// A Signature is used to indicate authorship of various actions throughout the |
| 12 |
/// library. |
| 13 |
/// |
| 14 |
/// Signatures contain a name, email, and timestamp. All fields can be specified |
| 15 |
/// with `new` while the `now` constructor omits the timestamp. The |
| 16 |
/// [`Repository::signature`] method can be used to create a default signature |
| 17 |
/// with name and email values read from the configuration. |
| 18 |
/// |
| 19 |
/// [`Repository::signature`]: struct.Repository.html#method.signature |
| 20 |
pub struct Signature<'a> { |
| 21 |
raw: *mut raw::git_signature, |
| 22 |
_marker: marker::PhantomData<&'a str>, |
| 23 |
owned: bool, |
| 24 |
} |
| 25 |
|
| 26 |
impl<'a> Signature<'a> { |
| 27 |
/// Create a new action signature with a timestamp of 'now'. |
| 28 |
/// |
| 29 |
/// See `new` for more information |
| 30 |
pub fn now(name: &str, email: &str) -> Result<Signature<'static>, Error> { |
| 31 |
crate::init(); |
| 32 |
let mut ret = ptr::null_mut(); |
| 33 |
let name = CString::new(name)?; |
| 34 |
let email = CString::new(email)?; |
| 35 |
unsafe { |
| 36 |
try_call!(raw::git_signature_now(&mut ret, name, email)); |
| 37 |
Ok(Binding::from_raw(ret)) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
/// Create a new action signature. |
| 42 |
/// |
| 43 |
/// The `time` specified is in seconds since the epoch, and the `offset` is |
| 44 |
/// the time zone offset in minutes. |
| 45 |
/// |
| 46 |
/// Returns error if either `name` or `email` contain angle brackets. |
| 47 |
pub fn new(name: &str, email: &str, time: &Time) -> Result<Signature<'static>, Error> { |
| 48 |
crate::init(); |
| 49 |
let mut ret = ptr::null_mut(); |
| 50 |
let name = CString::new(name)?; |
| 51 |
let email = CString::new(email)?; |
| 52 |
unsafe { |
| 53 |
try_call!(raw::git_signature_new( |
| 54 |
&mut ret, |
| 55 |
name, |
| 56 |
email, |
| 57 |
time.seconds() as raw::git_time_t, |
| 58 |
time.offset_minutes() as libc::c_int |
| 59 |
)); |
| 60 |
Ok(Binding::from_raw(ret)) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/// Gets the name on the signature. |
| 65 |
pub fn name(&self) -> Result<&str, Error> { |
| 66 |
str::from_utf8(self.name_bytes()).map_err(|e| e.into()) |
| 67 |
} |
| 68 |
|
| 69 |
/// Gets the name on the signature as a byte slice. |
| 70 |
pub fn name_bytes(&self) -> &[u8] { |
| 71 |
unsafe { crate::opt_bytes(self, (*self.raw).name).unwrap() } |
| 72 |
} |
| 73 |
|
| 74 |
/// Gets the email on the signature. |
| 75 |
pub fn email(&self) -> Result<&str, Error> { |
| 76 |
str::from_utf8(self.email_bytes()).map_err(|e| e.into()) |
| 77 |
} |
| 78 |
|
| 79 |
/// Gets the email on the signature as a byte slice. |
| 80 |
pub fn email_bytes(&self) -> &[u8] { |
| 81 |
unsafe { crate::opt_bytes(self, (*self.raw).email).unwrap() } |
| 82 |
} |
| 83 |
|
| 84 |
/// Get the `when` of this signature. |
| 85 |
pub fn when(&self) -> Time { |
| 86 |
unsafe { Binding::from_raw((*self.raw).when) } |
| 87 |
} |
| 88 |
|
| 89 |
/// Convert a signature of any lifetime into an owned signature with a |
| 90 |
/// static lifetime. |
| 91 |
pub fn to_owned(&self) -> Signature<'static> { |
| 92 |
unsafe { |
| 93 |
let me = mem::transmute::<&Signature<'a>, &Signature<'static>>(self); |
| 94 |
me.clone() |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
impl<'a> Binding for Signature<'a> { |
| 100 |
type Raw = *mut raw::git_signature; |
| 101 |
unsafe fn from_raw(raw: *mut raw::git_signature) -> Signature<'a> { |
| 102 |
Signature { |
| 103 |
raw, |
| 104 |
_marker: marker::PhantomData, |
| 105 |
owned: true, |
| 106 |
} |
| 107 |
} |
| 108 |
fn raw(&self) -> *mut raw::git_signature { |
| 109 |
self.raw |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/// Creates a new signature from the give raw pointer, tied to the lifetime |
| 114 |
/// of the given object. |
| 115 |
/// |
| 116 |
/// This function is unsafe as there is no guarantee that `raw` is valid for |
| 117 |
/// `'a` nor that it's a valid pointer. |
| 118 |
pub unsafe fn from_raw_const<'b, T>(_lt: &'b T, raw: *const raw::git_signature) -> Signature<'b> { |
| 119 |
Signature { |
| 120 |
raw: raw as *mut raw::git_signature, |
| 121 |
_marker: marker::PhantomData, |
| 122 |
owned: false, |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
impl Clone for Signature<'static> { |
| 127 |
fn clone(&self) -> Signature<'static> { |
| 128 |
// TODO: can this be defined for 'a and just do a plain old copy if the |
| 129 |
// lifetime isn't static? |
| 130 |
let mut raw = ptr::null_mut(); |
| 131 |
let rc = unsafe { raw::git_signature_dup(&mut raw, &*self.raw) }; |
| 132 |
assert_eq!(rc, 0); |
| 133 |
unsafe { Binding::from_raw(raw) } |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
impl<'a> Drop for Signature<'a> { |
| 138 |
fn drop(&mut self) { |
| 139 |
if self.owned { |
| 140 |
unsafe { raw::git_signature_free(self.raw) } |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
impl<'a> fmt::Display for Signature<'a> { |
| 146 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 147 |
write!( |
| 148 |
f, |
| 149 |
"{} <{}>", |
| 150 |
String::from_utf8_lossy(self.name_bytes()), |
| 151 |
String::from_utf8_lossy(self.email_bytes()) |
| 152 |
) |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
impl PartialEq for Signature<'_> { |
| 157 |
fn eq(&self, other: &Self) -> bool { |
| 158 |
self.when() == other.when() |
| 159 |
&& self.email_bytes() == other.email_bytes() |
| 160 |
&& self.name_bytes() == other.name_bytes() |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
impl Eq for Signature<'_> {} |
| 165 |
|
| 166 |
#[cfg(test)] |
| 167 |
mod tests { |
| 168 |
use crate::{Signature, Time}; |
| 169 |
|
| 170 |
#[test] |
| 171 |
fn smoke() { |
| 172 |
Signature::new("foo", "bar", &Time::new(89, 0)).unwrap(); |
| 173 |
Signature::now("foo", "bar").unwrap(); |
| 174 |
assert!(Signature::new("<foo>", "bar", &Time::new(89, 0)).is_err()); |
| 175 |
assert!(Signature::now("<foo>", "bar").is_err()); |
| 176 |
|
| 177 |
let s = Signature::now("foo", "bar").unwrap(); |
| 178 |
assert_eq!(s.name(), Ok("foo")); |
| 179 |
assert_eq!(s.email(), Ok("bar")); |
| 180 |
|
| 181 |
drop(s.clone()); |
| 182 |
drop(s.to_owned()); |
| 183 |
} |
| 184 |
} |