Skip to content

Commit cf40257

Browse files
authored
Merge pull request #22 from paq/fix-prefix
Avoid redundant prefixes
2 parents 9eb61fc + 5cd792b commit cf40257

File tree

4 files changed

+30
-28
lines changed

4 files changed

+30
-28
lines changed

src/booster.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde_json::Value;
66

77
use lightgbm_sys;
88

9-
use super::{Dataset, LGBMError, LGBMResult};
9+
use super::{Dataset, Error, Result};
1010

1111
/// Core model in LightGBM, containing functions for training, evaluating and predicting.
1212
pub struct Booster {
@@ -19,7 +19,7 @@ impl Booster {
1919
}
2020

2121
/// Init from model file.
22-
pub fn from_file(filename: String) -> LGBMResult<Self> {
22+
pub fn from_file(filename: String) -> Result<Self> {
2323
let filename_str = CString::new(filename).unwrap();
2424
let mut out_num_iterations = 0;
2525
let mut handle = std::ptr::null_mut();
@@ -56,7 +56,7 @@ impl Booster {
5656
/// };
5757
/// let bst = Booster::train(dataset, &params).unwrap();
5858
/// ```
59-
pub fn train(dataset: Dataset, parameter: &Value) -> LGBMResult<Self> {
59+
pub fn train(dataset: Dataset, parameter: &Value) -> Result<Self> {
6060
// get num_iterations
6161
let num_iterations: i64 = if parameter["num_iterations"].is_null() {
6262
100
@@ -104,7 +104,7 @@ impl Booster {
104104
/// ```
105105
/// let output = vec![vec![1.0, 0.109, 0.433]];
106106
/// ```
107-
pub fn predict(&self, data: Vec<Vec<f64>>) -> LGBMResult<Vec<Vec<f64>>> {
107+
pub fn predict(&self, data: Vec<Vec<f64>>) -> Result<Vec<Vec<f64>>> {
108108
let data_length = data.len();
109109
let feature_length = data[0].len();
110110
let params = CString::new("").unwrap();
@@ -148,16 +148,16 @@ impl Booster {
148148
}
149149

150150
/// Save model to file.
151-
pub fn save_file(&self, filename: String) {
151+
pub fn save_file(&self, filename: String) -> Result<()> {
152152
let filename_str = CString::new(filename).unwrap();
153153
lgbm_call!(lightgbm_sys::LGBM_BoosterSaveModel(
154154
self.handle,
155155
0_i32,
156156
-1_i32,
157157
0_i32,
158158
filename_str.as_ptr() as *const c_char
159-
))
160-
.unwrap();
159+
))?;
160+
Ok(())
161161
}
162162
}
163163

@@ -174,7 +174,7 @@ mod tests {
174174
use std::fs;
175175
use std::path::Path;
176176

177-
fn read_train_file() -> LGBMResult<Dataset> {
177+
fn read_train_file() -> Result<Dataset> {
178178
Dataset::from_file(
179179
"lightgbm-sys/lightgbm/examples/binary_classification/binary.train".to_string(),
180180
)
@@ -213,7 +213,10 @@ mod tests {
213213
}
214214
};
215215
let bst = Booster::train(dataset, &params).unwrap();
216-
bst.save_file("./test/test_save_file.output".to_string());
216+
assert_eq!(
217+
bst.save_file("./test/test_save_file.output".to_string()),
218+
Ok(())
219+
);
217220
assert!(Path::new("./test/test_save_file.output").exists());
218221
let _ = fs::remove_file("./test/test_save_file.output");
219222
}

src/dataset.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use lightgbm_sys;
33
use std;
44
use std::ffi::CString;
55

6-
use super::{LGBMError, LGBMResult};
6+
use super::{Error, Result};
77

88
/// Dataset used throughout LightGBM for training.
99
///
@@ -56,7 +56,7 @@ impl Dataset {
5656
/// let label = vec![0.0, 0.0, 0.0, 1.0, 1.0];
5757
/// let dataset = Dataset::from_mat(data, label).unwrap();
5858
/// ```
59-
pub fn from_mat(data: Vec<Vec<f64>>, label: Vec<f32>) -> LGBMResult<Self> {
59+
pub fn from_mat(data: Vec<Vec<f64>>, label: Vec<f32>) -> Result<Self> {
6060
let data_length = data.len();
6161
let feature_length = data[0].len();
6262
let params = CString::new("").unwrap();
@@ -106,7 +106,7 @@ impl Dataset {
106106
///
107107
/// let dataset = Dataset::from_file("lightgbm-sys/lightgbm/examples/binary_classification/binary.train".to_string());
108108
/// ```
109-
pub fn from_file(file_path: String) -> LGBMResult<Self> {
109+
pub fn from_file(file_path: String) -> Result<Self> {
110110
let file_path_str = CString::new(file_path).unwrap();
111111
let params = CString::new("").unwrap();
112112
let mut handle = std::ptr::null_mut();
@@ -131,7 +131,7 @@ impl Drop for Dataset {
131131
#[cfg(test)]
132132
mod tests {
133133
use super::*;
134-
fn read_train_file() -> LGBMResult<Dataset> {
134+
fn read_train_file() -> Result<Dataset> {
135135
Dataset::from_file(
136136
"lightgbm-sys/lightgbm/examples/binary_classification/binary.train".to_string(),
137137
)

src/error.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
11
//! Functionality related to errors and error handling.
22
3-
use std;
4-
use std::error::Error;
3+
use std::error;
54
use std::ffi::CStr;
65
use std::fmt::{self, Display};
76

87
use lightgbm_sys;
98

109
/// Convenience return type for most operations which can return an `LightGBM`.
11-
pub type LGBMResult<T> = std::result::Result<T, LGBMError>;
10+
pub type Result<T> = std::result::Result<T, Error>;
1211

1312
/// Wrap errors returned by the LightGBM library.
1413
#[derive(Debug, Eq, PartialEq)]
15-
pub struct LGBMError {
14+
pub struct Error {
1615
desc: String,
1716
}
1817

19-
impl LGBMError {
18+
impl Error {
2019
pub(crate) fn new<S: Into<String>>(desc: S) -> Self {
21-
LGBMError { desc: desc.into() }
20+
Error { desc: desc.into() }
2221
}
2322

2423
/// Check the return value from an LightGBM FFI call, and return the last error message on error.
2524
///
2625
/// Return values of 0 are treated as success, returns values of -1 are treated as errors.
2726
///
2827
/// Meaning of any other return values are undefined, and will cause a panic.
29-
pub(crate) fn check_return_value(ret_val: i32) -> LGBMResult<()> {
28+
pub(crate) fn check_return_value(ret_val: i32) -> Result<()> {
3029
match ret_val {
3130
0 => Ok(()),
32-
-1 => Err(LGBMError::from_lightgbm()),
31+
-1 => Err(Error::from_lightgbm()),
3332
_ => panic!(format!(
3433
"unexpected return value '{}', expected 0 or -1",
3534
ret_val
@@ -45,9 +44,9 @@ impl LGBMError {
4544
}
4645
}
4746

48-
impl Error for LGBMError {}
47+
impl error::Error for Error {}
4948

50-
impl Display for LGBMError {
49+
impl Display for Error {
5150
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5251
write!(f, "LightGBM error: {}", &self.desc)
5352
}
@@ -59,10 +58,10 @@ mod tests {
5958

6059
#[test]
6160
fn return_value_handling() {
62-
let result = LGBMError::check_return_value(0);
61+
let result = Error::check_return_value(0);
6362
assert_eq!(result, Ok(()));
6463

65-
let result = LGBMError::check_return_value(-1);
66-
assert_eq!(result, Err(LGBMError::new("Everything is fine")));
64+
let result = Error::check_return_value(-1);
65+
assert_eq!(result, Err(Error::new("Everything is fine")));
6766
}
6867
}

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ extern crate serde_json;
55
#[macro_use]
66
macro_rules! lgbm_call {
77
($x:expr) => {
8-
LGBMError::check_return_value(unsafe { $x })
8+
Error::check_return_value(unsafe { $x })
99
};
1010
}
1111

1212
mod error;
13-
pub use error::{LGBMError, LGBMResult};
13+
pub use error::{Error, Result};
1414

1515
mod dataset;
1616
pub use dataset::Dataset;

0 commit comments

Comments
 (0)