1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#![allow(missing_docs)]

use alsa;
use png;
use std::convert::From;
use std;
use toml;



error_chain! {
    foreign_links {
        Alsa(alsa::Error);
        IO(std::io::Error);
        Toml(toml::de::Error);
        Png(png::DecodingError);
    }

    errors {
        GtkResponseCancel(t: String) {
            description("User hit cancel")
            display("User hit cancel: {}", t)
        }
    }
}




#[macro_export]
/// Try to unwrap a `Result<T, E>`. If there is a value `T`, yield it,
/// otherwise print a warning and `return ()` from the function.
macro_rules! try_w {
    ($expr:expr) => {
        try_wr!($expr, ())
    };
    ($expr:expr, $fmt:expr, $($arg:tt)+) => {
        try_wr!($expr, (), $fmt, $(arg)+)
    };
    ($expr:expr, $fmt:expr) => {
        try_wr!($expr, (), $fmt)
    }
}


#[macro_export]
/// Try to unwrap a `Result<T, E>`. If there is a value `T`, yield it,
/// otherwise print a warning and return from the function with the given value.
macro_rules! try_wr {
    ($expr:expr, $ret:expr) => (match $expr {
        ::std::result::Result::Ok(val) => val,
        ::std::result::Result::Err(err) => {
            warn!("{:?}", err);
            return $ret;
        },
    });
    ($expr:expr, $ret:expr, $fmt:expr) => (match $expr {
        ::std::result::Result::Ok(val) => val,
        ::std::result::Result::Err(err) => {
            warn!("Original error: {:?}", err);
            warn!($fmt);
            return $ret;
        },
    });
    ($expr:expr, $ret:expr, $fmt:expr, $($arg:tt)+) => (match $expr {
        ::std::result::Result::Ok(val) => val,
        ::std::result::Result::Err(err) => {
            warn!("Original error: {:?}", err);
            warn!(format!($fmt, $(arg)+));
            return $ret;
        },
    })
}


#[macro_export]
/// Try to unwrap a `Result<T, E>`. If there is a value `T`, yield it,
/// otherwise return from the function with the given value.
macro_rules! try_r {
    ($expr:expr, $ret:expr) => (match $expr {
        ::std::result::Result::Ok(val) => val,
        ::std::result::Result::Err(_) => {
            return $ret;
        },
    });
}


#[macro_export]
/// Try to unwrap a `Result<T, E>`. If there is a value `T`, yield it,
/// otherwise print an error and exit the program.
macro_rules! try_e {
    ($expr:expr) => (match $expr {
        ::std::result::Result::Ok(val) => val,
        ::std::result::Result::Err(err) => {
            error!("{:?}", err);
            ::std::process::exit(1);
        },
    });
    ($expr:expr, $fmt:expr) => (match $expr {
        ::std::result::Result::Ok(val) => val,
        ::std::result::Result::Err(err) => {
            error!("Original error: {:?}", err);
            error!($fmt);
            std::process::exit(1);
        },
    });
    ($expr:expr, $fmt:expr, $($arg:tt)+) => (match $expr {
        ::std::result::Result::Ok(val) => val,
        ::std::result::Result::Err(err) => {
            error!("Original error: {:?}", err);
            error!(format!($fmt, $(arg)+));
            std::process::exit(1);
        },
    })
}

#[macro_export]
/// Unwraps a `Result<T, E>` by yielding a value of the samet ype
/// for either case.
macro_rules! unwrap_any {
    ($expr:expr, $fmt_ok:expr, $fmt_err:expr) => (match $expr {
        ::std::result::Result::Ok(val) => $fmt_ok,
        ::std::result::Result::Err(err) => $fmt_err,
    })

}


#[macro_export]
/// Warns on err and yields `()` without returning the function.
macro_rules! just_warn {
    ($expr:expr) => (match $expr {
        ::std::result::Result::Ok(_) => (),
        ::std::result::Result::Err(err) => {
            warn!("{:?}", err);
            ()
        },
    });
}


#[macro_export]
/// Present a gtk error dialog with given message.
/// Provides only a close button.
macro_rules! error_dialog {
    ($msg:expr, $parent:expr) => {
        {
            use gtk::DialogExt;
            use gtk::WidgetExt;
            use gtk::WindowExt;

            let parent: Option<&gtk::Window> = $parent;
            let dialog = gtk::MessageDialog::new(parent,
                                                 gtk::DIALOG_DESTROY_WITH_PARENT,
                                                 gtk::MessageType::Error,
                                                 gtk::ButtonsType::Close,
                                                 $msg);
            dialog.set_title("PNMixer-rs Error");

            dialog.run();
            dialog.destroy();
        }
    };
}


#[macro_export]
/// Present a gtk error dialog with the error from the `Result` type,
/// if any.
/// Provides only a close button.
macro_rules! result_warn {
    ($expr:expr, $parent:expr) => (match $expr {
        ::std::result::Result::Ok(v) => Ok(v),
        ::std::result::Result::Err(err) => {
            use std::error::Error;
            let warn_string = format!("{}{}", err.description(),
                err.cause().map(|e| format!("\n\nCause: {}", e.description())).unwrap_or(String::from("")));
            warn!("{}", warn_string);
            error_dialog!(warn_string.as_str(), $parent);
            Err(err)
        },
    });
}


#[macro_export]
/// Convert `WResult` to `Result`. All warnings are printed via the `log`
/// crate and are shown via Gtk dialogs.
macro_rules! wresult_warn {
    ($expr:expr, $parent:expr) => (match $expr {
        ::w_result::WResult::WOk(t, ws) => {
            use std::error::Error;
            for w in ws {
                let warn_string = format!("{}{}", w.description(),
                    w.cause().map(|e| format!("\n\nCause: {}", e.description())).unwrap_or(String::from("")));
                warn!("{}", warn_string);
                error_dialog!(warn_string.as_str(), $parent);
            }
            Ok(t)
        },
        ::w_result::WResult::WErr(err) => Err(err),
    });
}


#[macro_export]
/// If there is an error in the expression, push it to
/// the given mutable warning vector.
macro_rules! push_warning {
    ($expr:expr, $vec:ident) => (match $expr {
            Err(e) => $vec.push(e),
            _ => ()
    });
}


#[macro_export]
/// If there is a value in the Result type, unwrap it, otherwise error-log
/// the error, show it via gtk dialog and exit the whole program.
macro_rules! unwrap_error {
    ($expr:expr, $parent:expr) => (match $expr {
        ::std::result::Result::Ok(v) => v,
        ::std::result::Result::Err(err) => {
            use std::error::Error;
            let err_string = format!("{}{}", err.description(),
                err.cause().map(|e| format!("\n\nCause: {}", e.description())).unwrap_or(String::from("")));

            error!("{}", err_string);
            error_dialog!(err_string.as_str(), $parent);
            ::std::process::exit(1);
        },
    });
}