use std::kinds::marker;
use std::mem;
use std::raw as stdraw;
use {raw, Oid};
pub struct Blob<'repo> {
raw: *mut raw::git_blob,
marker: marker::ContravariantLifetime<'repo>,
}
impl<'repo> Blob<'repo> {
pub unsafe fn from_raw(raw: *mut raw::git_blob) -> Blob<'repo> {
Blob {
raw: raw,
marker: marker::ContravariantLifetime,
}
}
pub fn id(&self) -> Oid {
unsafe { Oid::from_raw(raw::git_blob_id(&*self.raw)) }
}
pub fn raw(&self) -> *mut raw::git_blob { self.raw }
pub fn is_binary(&self) -> bool {
unsafe { raw::git_blob_is_binary(&*self.raw) == 1 }
}
pub fn content(&self) -> &[u8] {
unsafe {
mem::transmute(stdraw::Slice {
data: raw::git_blob_rawcontent(&*self.raw) as *const u8,
len: raw::git_blob_rawsize(&*self.raw) as uint,
})
}
}
}
#[unsafe_destructor]
impl<'repo> Drop for Blob<'repo> {
fn drop(&mut self) {
unsafe { raw::git_blob_free(self.raw) }
}
}
#[cfg(test)]
mod tests {
use std::io::{TempDir, File};
use Repository;
#[test]
fn buffer() {
let td = TempDir::new("test").unwrap();
let repo = Repository::init(td.path()).unwrap();
let id = repo.blob(&[5, 4, 6]).unwrap();
let blob = repo.find_blob(id).unwrap();
assert_eq!(blob.id(), id);
assert_eq!(blob.content(), [5, 4, 6].as_slice());
}
#[test]
fn path() {
let td = TempDir::new("test").unwrap();
let path = td.path().join("foo");
File::create(&path).write(&[7, 8, 9]).unwrap();
let repo = Repository::init(td.path()).unwrap();
let id = repo.blob_path(&path).unwrap();
let blob = repo.find_blob(id).unwrap();
assert_eq!(blob.content(), [7, 8, 9].as_slice());
}
}