Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Add glib::BoolError for use as return value of possibly failing functions returning booleans #169

Merged
merged 5 commits into from
May 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,28 @@ pub trait ErrorDomain: Copy {
/// i.e. any unrecognized codes map to it.
fn from(code: i32) -> Option<Self> where Self: Sized;
}

/// Generic error used for functions that fail without any further information
#[derive(Debug)]
pub struct BoolError(&'static str);

impl BoolError {
pub fn from_glib(b: glib_ffi::gboolean, s: &'static str) -> Result<(), Self> {
match b {
glib_ffi::GFALSE => Err(BoolError(s)),
_ => Ok(()),
}
}
}

impl fmt::Display for BoolError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}

impl error::Error for BoolError {
fn description(&self) -> &str {
self.0
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ extern crate gobject_sys as gobject_ffi;

use std::ffi::CStr;
pub use bytes::Bytes;
pub use error::Error;
pub use error::{Error, BoolError};
pub use file_error::FileError;
pub use object::{
Cast,
Expand Down
72 changes: 63 additions & 9 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use ffi;
use translate::*;
use std::path::PathBuf;
use error::BoolError;

/// Same as [`get_prgname()`].
///
Expand Down Expand Up @@ -31,38 +33,90 @@ pub fn set_prgname(name: Option<&str>) {
}
}

#[cfg(unix)]
#[cfg(not(windows))]
pub fn getenv(variable_name: &str) -> Option<String> {
unsafe {
from_glib_none(ffi::g_getenv(variable_name.to_glib_none().0))
}
}

#[cfg(unix)]
pub fn setenv(variable_name: &str, value: &str, overwrite: bool) -> bool {
#[cfg(windows)]
pub fn getenv(variable_name: &str) -> Option<String> {
use libc::c_char;
extern "C" {
fn g_getenv_utf8(variable: *const c_char) -> *const c_char;
}

unsafe {
from_glib_none(g_getenv_utf8(variable_name.to_glib_none().0))
}
}

#[cfg(not(windows))]
pub fn setenv(variable_name: &str, value: &str, overwrite: bool) -> Result<(), BoolError> {
unsafe {
BoolError::from_glib(ffi::g_setenv(variable_name.to_glib_none().0,
value.to_glib_none().0,
overwrite.to_glib()),
"Failed to set environment variable")
}
}

#[cfg(windows)]
pub fn setenv(variable_name: &str, value: &str, overwrite: bool) -> Result<(), BoolError> {
use libc::c_char;
extern "C" {
fn g_setenv_utf8(variable: *const c_char, value: *const c_char, overwrite: ffi::gboolean) -> ffi::gboolean;
}

unsafe {
from_glib(ffi::g_setenv(variable_name.to_glib_none().0,
BoolError::from_glib(g_setenv_utf8(variable_name.to_glib_none().0,
value.to_glib_none().0,
overwrite.to_glib()))
overwrite.to_glib()),
"Failed to set environment variable")
}
}

#[cfg(unix)]
#[cfg(not(windows))]
pub fn unsetenv(variable_name: &str) {
unsafe {
ffi::g_unsetenv(variable_name.to_glib_none().0)
}
}

#[cfg(windows)]
pub fn unsetenv(variable_name: &str) {
use libc::c_char;
extern "C" {
fn g_unsetenv_utf8(variable: *const c_char);
}

unsafe {
g_unsetenv_utf8(variable_name.to_glib_none().0)
}
}

pub fn get_user_name() -> Option<String> {
unsafe {
from_glib_none(ffi::g_get_user_name())
}
}

#[cfg(unix)]
pub fn get_current_dir() -> Option<String> {
#[cfg(not(windows))]
pub fn get_current_dir() -> Option<PathBuf> {
unsafe {
from_glib_full(ffi::g_get_current_dir())
}
}

#[cfg(windows)]
pub fn get_current_dir() -> Option<PathBuf> {
use libc::c_char;
extern "C" {
fn g_get_current_dir_utf8() -> *mut c_char;
}

unsafe {
from_glib_none(ffi::g_get_current_dir())
from_glib_full(g_get_current_dir_utf8())
}
}