diff --git a/scie-scanner/src/scanner/LICENSE b/scie-scanner/src/scanner/LICENSE index 7fdf3ba9a61d7f0503ab2da6d2f0416390707297..e59a74b933d149bedcb2094dadc257bb38071460 100644 --- a/scie-scanner/src/scanner/LICENSE +++ b/scie-scanner/src/scanner/LICENSE @@ -6,6 +6,8 @@ Copyright (c) Phodal Huang Copyright (c) Microsoft Corporation. +Copyright (c) 2015 Will Speak , Ivan Ivashchenko , and contributors. + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights diff --git a/scie-scanner/src/scanner/mod.rs b/scie-scanner/src/scanner/mod.rs index a89e29aef9a0858953864ae23896b65698709d14..1813295739c6a29d9dbeeb335e8f6231c5e5734f 100644 --- a/scie-scanner/src/scanner/mod.rs +++ b/scie-scanner/src/scanner/mod.rs @@ -1,2 +1,3 @@ pub mod scie_scanner; pub mod scie_onig; +pub mod scie_error; diff --git a/scie-scanner/src/scanner/scie_error.rs b/scie-scanner/src/scanner/scie_error.rs new file mode 100644 index 0000000000000000000000000000000000000000..c0a01a041ff67856f55b0ef6ac714bad82e0e61a --- /dev/null +++ b/scie-scanner/src/scanner/scie_error.rs @@ -0,0 +1,81 @@ +// > The MIT License (MIT) +// > +// > Copyright (c) 2015 Will Speak , Ivan Ivashchenko +// > , and contributors. +// > +// > Permission is hereby granted, free of charge, to any person obtaining a copy +// > of this software and associated documentation files (the "Software"), to deal +// > in the Software without restriction, including without limitation the rights +// > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// > copies of the Software, and to permit persons to whom the Software is +// > furnished to do so, subject to the following conditions: +// > +// > The above copyright notice and this permission notice shall be included in all +// > copies or substantial portions of the Software. +// > +// > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// > SOFTWARE. + + +use std::os::raw::c_int; +use std::ptr::null; +use std::{error, fmt, str}; + +/// This struture represents an error from the underlying Oniguruma libray. +pub struct ScieOnigError { + code: c_int, + description: String, +} + +impl ScieOnigError { + pub fn from_code_and_info(code: c_int, info: &onig_sys::OnigErrorInfo) -> ScieOnigError { + ScieOnigError::new(code, info) + } + + fn from_code(code: c_int) -> ScieOnigError { + ScieOnigError::new(code, null()) + } + + fn new(code: c_int, info: *const onig_sys::OnigErrorInfo) -> ScieOnigError { + let buff = &mut [0; onig_sys::ONIG_MAX_ERROR_MESSAGE_LEN as usize]; + let len = unsafe { onig_sys::onig_error_code_to_str(buff.as_mut_ptr(), code, info) }; + let description = str::from_utf8(&buff[..len as usize]).unwrap(); + ScieOnigError { + code, + description: description.to_owned(), + } + } + + /// Return Oniguruma engine error code. + pub fn code(&self) -> i32 { + self.code + } + + /// Return error description provided by Oniguruma engine. + pub fn description(&self) -> &str { + &self.description + } +} + +impl error::Error for ScieOnigError { + fn description(&self) -> &str { + &self.description + } +} + +impl fmt::Display for ScieOnigError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Oniguruma error: {}", self.description()) + } +} + +impl fmt::Debug for ScieOnigError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Error({}, {})", self.code, self.description()) + } +} diff --git a/scie-scanner/src/scanner/scie_onig.rs b/scie-scanner/src/scanner/scie_onig.rs index d3fb66ffb6a74570dfac10de8bf84781cb51a16a..d5e8dd93835902980ae4bbdec5c4250a58c34eba 100644 --- a/scie-scanner/src/scanner/scie_onig.rs +++ b/scie-scanner/src/scanner/scie_onig.rs @@ -1,7 +1,9 @@ +// +// use std::ptr::null_mut; use onig::{Syntax, EncodedChars}; use std::sync::Mutex; - +use crate::scanner::scie_error::ScieOnigError; lazy_static! { static ref REGEX_NEW_MUTEX: Mutex<()> = Mutex::new(()); @@ -14,10 +16,12 @@ bitflags! { } } -pub struct ScieOnig {} +pub struct ScieOnig { + raw: onig_sys::OnigRegex, +} impl ScieOnig { - pub fn hello(pattern: &str) { + pub fn hello(pattern: &str) -> Result { let option = ScieOnigOptions::REGEX_OPTION_NONE; let syntax = Syntax::default(); @@ -33,7 +37,7 @@ impl ScieOnig { par_end: null_mut(), }; - let _err = unsafe { + let err = unsafe { // Grab a lock to make sure that `onig_new` isn't called by // more than one thread at a time. let _guard = REGEX_NEW_MUTEX.lock().unwrap(); @@ -45,8 +49,14 @@ impl ScieOnig { pattern.encoding(), syntax as *const Syntax as *mut Syntax as *mut onig_sys::OnigSyntaxType, &mut error, - ); + ) }; + + if err == onig_sys::ONIG_NORMAL as i32 { + Ok(ScieOnig { raw: reg }) + } else { + Err(ScieOnigError::from_code_and_info(err, &error)) + } } pub fn create_onig_scanner(_sources: Vec) {} }