| 1 |
#![macro_use] |
| 2 |
|
| 3 |
use crate::Error; |
| 4 |
|
| 5 |
macro_rules! call { |
| 6 |
(raw::$p:ident ($($e:expr),*)) => ( |
| 7 |
raw::$p($(crate::call::convert(&$e)),*) |
| 8 |
) |
| 9 |
} |
| 10 |
|
| 11 |
macro_rules! try_call { |
| 12 |
(raw::$p:ident ($($e:expr),*)) => ({ |
| 13 |
match crate::call::c_try(raw::$p($(crate::call::convert(&$e)),*)) { |
| 14 |
Ok(o) => o, |
| 15 |
Err(e) => { crate::panic::check(); return Err(e) } |
| 16 |
} |
| 17 |
}) |
| 18 |
} |
| 19 |
|
| 20 |
macro_rules! try_call_iter { |
| 21 |
($($f:tt)*) => { |
| 22 |
match call!($($f)*) { |
| 23 |
0 => {} |
| 24 |
raw::GIT_ITEROVER => return None, |
| 25 |
e => return Some(Err(crate::call::last_error(e))) |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
#[doc(hidden)] |
| 31 |
pub trait Convert<T> { |
| 32 |
fn convert(&self) -> T; |
| 33 |
} |
| 34 |
|
| 35 |
pub fn convert<T, U: Convert<T>>(u: &U) -> T { |
| 36 |
u.convert() |
| 37 |
} |
| 38 |
|
| 39 |
pub fn c_try(ret: libc::c_int) -> Result<libc::c_int, Error> { |
| 40 |
match ret { |
| 41 |
n if n < 0 => Err(last_error(n)), |
| 42 |
n => Ok(n), |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
pub fn last_error(code: libc::c_int) -> Error { |
| 47 |
Error::last_error(code) |
| 48 |
} |
| 49 |
|
| 50 |
mod impls { |
| 51 |
use std::ffi::CString; |
| 52 |
use std::ptr; |
| 53 |
|
| 54 |
use crate::call::Convert; |
| 55 |
use crate::{raw, BranchType, ConfigLevel, Direction, ObjectType, ResetType}; |
| 56 |
use crate::{ |
| 57 |
AutotagOption, DiffFormat, FetchPrune, FileFavor, SubmoduleIgnore, SubmoduleUpdate, |
| 58 |
}; |
| 59 |
|
| 60 |
impl<T: Copy> Convert<T> for T { |
| 61 |
fn convert(&self) -> T { |
| 62 |
*self |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
impl Convert<libc::c_int> for bool { |
| 67 |
fn convert(&self) -> libc::c_int { |
| 68 |
*self as libc::c_int |
| 69 |
} |
| 70 |
} |
| 71 |
impl<'a, T> Convert<*const T> for &'a T { |
| 72 |
fn convert(&self) -> *const T { |
| 73 |
*self as *const T |
| 74 |
} |
| 75 |
} |
| 76 |
impl<'a, T> Convert<*mut T> for &'a mut T { |
| 77 |
fn convert(&self) -> *mut T { |
| 78 |
&**self as *const T as *mut T |
| 79 |
} |
| 80 |
} |
| 81 |
impl<T> Convert<*const T> for *mut T { |
| 82 |
fn convert(&self) -> *const T { |
| 83 |
*self as *const T |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
impl Convert<*const libc::c_char> for CString { |
| 88 |
fn convert(&self) -> *const libc::c_char { |
| 89 |
self.as_ptr() |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
impl<T, U: Convert<*const T>> Convert<*const T> for Option<U> { |
| 94 |
fn convert(&self) -> *const T { |
| 95 |
self.as_ref().map(|s| s.convert()).unwrap_or(ptr::null()) |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
impl<T, U: Convert<*mut T>> Convert<*mut T> for Option<U> { |
| 100 |
fn convert(&self) -> *mut T { |
| 101 |
self.as_ref() |
| 102 |
.map(|s| s.convert()) |
| 103 |
.unwrap_or(ptr::null_mut()) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
impl Convert<raw::git_reset_t> for ResetType { |
| 108 |
fn convert(&self) -> raw::git_reset_t { |
| 109 |
match *self { |
| 110 |
ResetType::Soft => raw::GIT_RESET_SOFT, |
| 111 |
ResetType::Hard => raw::GIT_RESET_HARD, |
| 112 |
ResetType::Mixed => raw::GIT_RESET_MIXED, |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
impl Convert<raw::git_direction> for Direction { |
| 118 |
fn convert(&self) -> raw::git_direction { |
| 119 |
match *self { |
| 120 |
Direction::Push => raw::GIT_DIRECTION_PUSH, |
| 121 |
Direction::Fetch => raw::GIT_DIRECTION_FETCH, |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
impl Convert<raw::git_object_t> for ObjectType { |
| 127 |
fn convert(&self) -> raw::git_object_t { |
| 128 |
match *self { |
| 129 |
ObjectType::Any => raw::GIT_OBJECT_ANY, |
| 130 |
ObjectType::Commit => raw::GIT_OBJECT_COMMIT, |
| 131 |
ObjectType::Tree => raw::GIT_OBJECT_TREE, |
| 132 |
ObjectType::Blob => raw::GIT_OBJECT_BLOB, |
| 133 |
ObjectType::Tag => raw::GIT_OBJECT_TAG, |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
impl Convert<raw::git_object_t> for Option<ObjectType> { |
| 139 |
fn convert(&self) -> raw::git_object_t { |
| 140 |
self.unwrap_or(ObjectType::Any).convert() |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
impl Convert<raw::git_branch_t> for BranchType { |
| 145 |
fn convert(&self) -> raw::git_branch_t { |
| 146 |
match *self { |
| 147 |
BranchType::Remote => raw::GIT_BRANCH_REMOTE, |
| 148 |
BranchType::Local => raw::GIT_BRANCH_LOCAL, |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
impl Convert<raw::git_branch_t> for Option<BranchType> { |
| 154 |
fn convert(&self) -> raw::git_branch_t { |
| 155 |
self.map(|s| s.convert()).unwrap_or(raw::GIT_BRANCH_ALL) |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
impl Convert<raw::git_config_level_t> for ConfigLevel { |
| 160 |
fn convert(&self) -> raw::git_config_level_t { |
| 161 |
match *self { |
| 162 |
ConfigLevel::ProgramData => raw::GIT_CONFIG_LEVEL_PROGRAMDATA, |
| 163 |
ConfigLevel::System => raw::GIT_CONFIG_LEVEL_SYSTEM, |
| 164 |
ConfigLevel::XDG => raw::GIT_CONFIG_LEVEL_XDG, |
| 165 |
ConfigLevel::Global => raw::GIT_CONFIG_LEVEL_GLOBAL, |
| 166 |
ConfigLevel::Local => raw::GIT_CONFIG_LEVEL_LOCAL, |
| 167 |
ConfigLevel::Worktree => raw::GIT_CONFIG_LEVEL_WORKTREE, |
| 168 |
ConfigLevel::App => raw::GIT_CONFIG_LEVEL_APP, |
| 169 |
ConfigLevel::Highest => raw::GIT_CONFIG_HIGHEST_LEVEL, |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
impl Convert<raw::git_diff_format_t> for DiffFormat { |
| 175 |
fn convert(&self) -> raw::git_diff_format_t { |
| 176 |
match *self { |
| 177 |
DiffFormat::Patch => raw::GIT_DIFF_FORMAT_PATCH, |
| 178 |
DiffFormat::PatchHeader => raw::GIT_DIFF_FORMAT_PATCH_HEADER, |
| 179 |
DiffFormat::Raw => raw::GIT_DIFF_FORMAT_RAW, |
| 180 |
DiffFormat::NameOnly => raw::GIT_DIFF_FORMAT_NAME_ONLY, |
| 181 |
DiffFormat::NameStatus => raw::GIT_DIFF_FORMAT_NAME_STATUS, |
| 182 |
DiffFormat::PatchId => raw::GIT_DIFF_FORMAT_PATCH_ID, |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
impl Convert<raw::git_merge_file_favor_t> for FileFavor { |
| 188 |
fn convert(&self) -> raw::git_merge_file_favor_t { |
| 189 |
match *self { |
| 190 |
FileFavor::Normal => raw::GIT_MERGE_FILE_FAVOR_NORMAL, |
| 191 |
FileFavor::Ours => raw::GIT_MERGE_FILE_FAVOR_OURS, |
| 192 |
FileFavor::Theirs => raw::GIT_MERGE_FILE_FAVOR_THEIRS, |
| 193 |
FileFavor::Union => raw::GIT_MERGE_FILE_FAVOR_UNION, |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
impl Convert<raw::git_submodule_ignore_t> for SubmoduleIgnore { |
| 199 |
fn convert(&self) -> raw::git_submodule_ignore_t { |
| 200 |
match *self { |
| 201 |
SubmoduleIgnore::Unspecified => raw::GIT_SUBMODULE_IGNORE_UNSPECIFIED, |
| 202 |
SubmoduleIgnore::None => raw::GIT_SUBMODULE_IGNORE_NONE, |
| 203 |
SubmoduleIgnore::Untracked => raw::GIT_SUBMODULE_IGNORE_UNTRACKED, |
| 204 |
SubmoduleIgnore::Dirty => raw::GIT_SUBMODULE_IGNORE_DIRTY, |
| 205 |
SubmoduleIgnore::All => raw::GIT_SUBMODULE_IGNORE_ALL, |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
impl Convert<raw::git_submodule_update_t> for SubmoduleUpdate { |
| 211 |
fn convert(&self) -> raw::git_submodule_update_t { |
| 212 |
match *self { |
| 213 |
SubmoduleUpdate::Checkout => raw::GIT_SUBMODULE_UPDATE_CHECKOUT, |
| 214 |
SubmoduleUpdate::Rebase => raw::GIT_SUBMODULE_UPDATE_REBASE, |
| 215 |
SubmoduleUpdate::Merge => raw::GIT_SUBMODULE_UPDATE_MERGE, |
| 216 |
SubmoduleUpdate::None => raw::GIT_SUBMODULE_UPDATE_NONE, |
| 217 |
SubmoduleUpdate::Default => raw::GIT_SUBMODULE_UPDATE_DEFAULT, |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
impl Convert<raw::git_remote_autotag_option_t> for AutotagOption { |
| 223 |
fn convert(&self) -> raw::git_remote_autotag_option_t { |
| 224 |
match *self { |
| 225 |
AutotagOption::Unspecified => raw::GIT_REMOTE_DOWNLOAD_TAGS_UNSPECIFIED, |
| 226 |
AutotagOption::None => raw::GIT_REMOTE_DOWNLOAD_TAGS_NONE, |
| 227 |
AutotagOption::Auto => raw::GIT_REMOTE_DOWNLOAD_TAGS_AUTO, |
| 228 |
AutotagOption::All => raw::GIT_REMOTE_DOWNLOAD_TAGS_ALL, |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
impl Convert<raw::git_fetch_prune_t> for FetchPrune { |
| 234 |
fn convert(&self) -> raw::git_fetch_prune_t { |
| 235 |
match *self { |
| 236 |
FetchPrune::Unspecified => raw::GIT_FETCH_PRUNE_UNSPECIFIED, |
| 237 |
FetchPrune::On => raw::GIT_FETCH_PRUNE, |
| 238 |
FetchPrune::Off => raw::GIT_FETCH_NO_PRUNE, |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
} |