use std::kinds::marker;
use std::str;
use {raw, Signature, Oid};
pub struct Note<'repo> {
raw: *mut raw::git_note,
marker: marker::ContravariantLifetime<'repo>,
}
pub struct Notes<'repo> {
raw: *mut raw::git_note_iterator,
marker: marker::ContravariantLifetime<'repo>,
}
impl<'repo> Note<'repo> {
pub unsafe fn from_raw(raw: *mut raw::git_note) -> Note<'repo> {
Note {
raw: raw,
marker: marker::ContravariantLifetime,
}
}
pub fn author(&self) -> Signature {
unsafe {
Signature::from_raw_const(self, raw::git_note_author(&*self.raw))
}
}
pub fn committer(&self) -> Signature {
unsafe {
Signature::from_raw_const(self, raw::git_note_committer(&*self.raw))
}
}
pub fn message_bytes(&self) -> &[u8] {
unsafe { ::opt_bytes(self, raw::git_note_message(&*self.raw)).unwrap() }
}
pub fn message(&self) -> Option<&str> {
str::from_utf8(self.message_bytes()).ok()
}
pub fn id(&self) -> Oid {
unsafe { Oid::from_raw(raw::git_note_id(&*self.raw)) }
}
}
#[unsafe_destructor]
impl<'repo> Drop for Note<'repo> {
fn drop(&mut self) {
unsafe { raw::git_note_free(self.raw); }
}
}
impl<'repo> Notes<'repo> {
pub unsafe fn from_raw(raw: *mut raw::git_note_iterator) -> Notes<'repo> {
Notes {
raw: raw,
marker: marker::ContravariantLifetime,
}
}
}
impl<'repo> Iterator for Notes<'repo> {
type Item = (Oid, Oid);
fn next(&mut self) -> Option<(Oid, Oid)> {
let mut note_id = raw::git_oid { id: [0; raw::GIT_OID_RAWSZ] };
let mut annotated_id = note_id;
unsafe {
match raw::git_note_next(&mut note_id, &mut annotated_id, self.raw) {
0 => Some((Oid::from_raw(¬e_id), Oid::from_raw(&annotated_id))),
_ => None,
}
}
}
}
#[unsafe_destructor]
impl<'repo> Drop for Notes<'repo> {
fn drop(&mut self) {
unsafe { raw::git_note_iterator_free(self.raw); }
}
}
#[cfg(test)]
mod tests {
#[test]
fn smoke() {
let (_td, repo) = ::test::repo_init();
assert!(repo.notes(None).is_err());
let sig = repo.signature().unwrap();
let head = repo.head().unwrap().target().unwrap();
let note = repo.note(&sig, &sig, None, head, "foo", false).unwrap();
assert_eq!(repo.notes(None).unwrap().count(), 1);
let note_obj = repo.find_note(None, head).unwrap();
assert_eq!(note_obj.id(), note);
assert_eq!(note_obj.message(), Some("foo"));
let (a, b) = repo.notes(None).unwrap().next().unwrap();
assert_eq!(a, note);
assert_eq!(b, head);
assert_eq!(repo.note_default_ref().unwrap(), "refs/notes/commits");
}
}