Skip to content

Commit 2c5bd2a

Browse files
committed
Cleanup
1 parent 75eb6d7 commit 2c5bd2a

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

src/error.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ pub enum Error {
6565
IntegerOverflow,
6666
/// An exception was thrown in a function.
6767
Exception(ZBox<ZendObject>),
68+
/// A failure occurred while registering the stream wrapper
69+
StreamWrapperRegistrationFailure,
70+
/// A failure occurred while unregistering the stream wrapper
71+
StreamWrapperUnregistrationFailure,
6872
}
6973

7074
impl Display for Error {
@@ -99,6 +103,12 @@ impl Display for Error {
99103
write!(f, "Converting integer arguments resulted in an overflow.")
100104
}
101105
Error::Exception(e) => write!(f, "Exception was thrown: {e:?}"),
106+
Error::StreamWrapperRegistrationFailure => {
107+
write!(f, "A failure occurred while registering the stream wrapper")
108+
},
109+
Error::StreamWrapperUnregistrationFailure => {
110+
write!(f, "A failure occurred while unregistering the stream wrapper")
111+
}
102112
}
103113
}
104114
}

src/zend/streams.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
php_stream_wrapper_ops, php_unregister_url_stream_wrapper,
88
php_unregister_url_stream_wrapper_volatile, zend_string,
99
},
10-
types::ZendStr,
10+
types::ZendStr, error::Error,
1111
};
1212

1313
pub type StreamWrapper = php_stream_wrapper;
@@ -41,20 +41,20 @@ impl StreamWrapper {
4141
}
4242
}
4343

44-
pub fn register(self, name: &str) -> Result<Self, ()> {
44+
pub fn register(self, name: &str) -> Result<Self, Error> {
4545
// We have to convert it to a static so owned streamwrapper doesn't get dropped.
4646
let copy = Box::new(self);
4747
let copy = Box::leak(copy);
48-
let name = std::ffi::CString::new(name).unwrap();
48+
let name = std::ffi::CString::new(name).expect("Could not create C string for name!");
4949
let result = unsafe { php_register_url_stream_wrapper(name.as_ptr(), copy) };
5050
if result == 0 {
5151
Ok(*copy)
5252
} else {
53-
Err(())
53+
Err(Error::StreamWrapperRegistrationFailure)
5454
}
5555
}
5656

57-
pub fn register_volatile(self, name: &str) -> Result<Self, ()> {
57+
pub fn register_volatile(self, name: &str) -> Result<Self, Error> {
5858
// We have to convert it to a static so owned streamwrapper doesn't get dropped.
5959
let copy = Box::new(self);
6060
let copy = Box::leak(copy);
@@ -64,23 +64,23 @@ impl StreamWrapper {
6464
if result == 0 {
6565
Ok(*copy)
6666
} else {
67-
Err(())
67+
Err(Error::StreamWrapperRegistrationFailure)
6868
}
6969
}
7070

71-
pub fn unregister(name: &str) -> Result<(), ()> {
72-
let name = std::ffi::CString::new(name).unwrap();
71+
pub fn unregister(name: &str) -> Result<(), Error> {
72+
let name = std::ffi::CString::new(name).expect("Could not create C string for name!");
7373
match unsafe { php_unregister_url_stream_wrapper(name.as_ptr()) } {
7474
0 => Ok(()),
75-
_ => Err(()),
75+
_ => Err(Error::StreamWrapperUnregistrationFailure),
7676
}
7777
}
7878

79-
pub fn unregister_volatile(name: &str) -> Result<(), ()> {
79+
pub fn unregister_volatile(name: &str) -> Result<(), Error> {
8080
let name = ZendStr::new(name, false);
8181
match unsafe { php_unregister_url_stream_wrapper_volatile((*name).as_ptr() as _) } {
8282
0 => Ok(()),
83-
_ => Err(()),
83+
_ => Err(Error::StreamWrapperUnregistrationFailure),
8484
}
8585
}
8686

0 commit comments

Comments
 (0)