From 47172b0e733403e20a4ddc3caf583432b39d50d6 Mon Sep 17 00:00:00 2001 From: Dmitry Volodin Date: Mon, 22 Jan 2024 15:15:57 +0100 Subject: [PATCH] Use SnmpResult type whenever possibile --- src/ber/header.rs | 6 +++--- src/ber/mod.rs | 8 +++---- src/ber/t_bool.rs | 6 +++--- src/ber/t_counter32.rs | 6 +++--- src/ber/t_counter64.rs | 6 +++--- src/ber/t_gauge32.rs | 6 +++--- src/ber/t_int.rs | 10 ++++----- src/ber/t_ipaddress.rs | 6 +++--- src/ber/t_null.rs | 6 +++--- src/ber/t_objectdescriptor.rs | 6 +++--- src/ber/t_objectid.rs | 10 ++++----- src/ber/t_octetstring.rs | 6 +++--- src/ber/t_opaque.rs | 6 +++--- src/ber/t_option.rs | 4 ++-- src/ber/t_real.rs | 6 +++--- src/ber/t_relative_oid.rs | 4 ++-- src/ber/t_sequence.rs | 4 ++-- src/ber/t_timeticks.rs | 6 +++--- src/ber/t_uinteger32.rs | 6 +++--- src/buf.rs | 20 +++++++++--------- src/snmp/get.rs | 4 ++-- src/snmp/getbulk.rs | 4 ++-- src/snmp/getresponse.rs | 2 +- src/snmp/msg/v1.rs | 18 ++++++++-------- src/snmp/msg/v2c.rs | 18 ++++++++-------- src/snmp/msg/v3/data.rs | 2 +- src/snmp/msg/v3/msg.rs | 20 +++++++++--------- src/snmp/msg/v3/usm.rs | 10 ++++----- src/snmp/pdu.rs | 4 ++-- src/snmp/value.rs | 40 +++++++++++++++++------------------ src/socket/transport.rs | 8 +++---- 31 files changed, 134 insertions(+), 134 deletions(-) diff --git a/src/ber/header.rs b/src/ber/header.rs index 394995d..739592c 100755 --- a/src/ber/header.rs +++ b/src/ber/header.rs @@ -131,7 +131,7 @@ mod tests { // Null, zero-length content #[test] - fn test_header_null() -> Result<(), SnmpError> { + fn test_header_null() -> SnmpResult<()> { let data = [5u8, 0u8]; let (tail, hdr) = BerHeader::from_ber(&data)?; assert_eq!(hdr.class, BerClass::Universal); @@ -144,7 +144,7 @@ mod tests { // Octet-stream #[test] - fn test_header_str() -> Result<(), SnmpError> { + fn test_header_str() -> SnmpResult<()> { let data = [4u8, 5, 0x74, 0x65, 0x73, 0x74, 0x2e]; let (tail, hdr) = BerHeader::from_ber(&data)?; assert_eq!(hdr.class, BerClass::Universal); @@ -158,7 +158,7 @@ mod tests { // Non-primitive sequence #[test] - fn test_header_sequence() -> Result<(), SnmpError> { + fn test_header_sequence() -> SnmpResult<()> { // Sequece of 3 nulls let data = [0x30u8, 6, 5, 0, 5, 0, 5, 0]; let (tail, hdr) = BerHeader::from_ber(&data)?; diff --git a/src/ber/mod.rs b/src/ber/mod.rs index 36ee764..c49b092 100755 --- a/src/ber/mod.rs +++ b/src/ber/mod.rs @@ -82,7 +82,7 @@ pub use t_counter64::SnmpCounter64; pub mod t_uinteger32; pub use t_uinteger32::SnmpUInteger32; -use crate::error::SnmpError; +use crate::error::{SnmpError, SnmpResult}; pub trait BerDecoder<'a> where @@ -92,7 +92,7 @@ where const ALLOW_CONSTRUCTED: bool; const TAG: Tag; - fn decode(i: &'a [u8], hdr: &BerHeader) -> Result; + fn decode(i: &'a [u8], hdr: &BerHeader) -> SnmpResult; fn from_ber(i: &'a [u8]) -> IResult<&'a [u8], Self, SnmpError> { if i.len() < 2 { @@ -114,10 +114,10 @@ where } pub trait BerEncoder { - fn push_ber(&self, buf: &mut Buffer) -> Result<(), SnmpError>; + fn push_ber(&self, buf: &mut Buffer) -> SnmpResult<()>; } // Convert value to python under the GIL held pub trait ToPython { - fn try_to_python(self, py: Python) -> Result, SnmpError>; + fn try_to_python(self, py: Python) -> SnmpResult>; } diff --git a/src/ber/t_bool.rs b/src/ber/t_bool.rs index 67a6b59..6dde233 100755 --- a/src/ber/t_bool.rs +++ b/src/ber/t_bool.rs @@ -6,7 +6,7 @@ // ------------------------------------------------------------------------ use super::{BerDecoder, BerHeader, Tag, ToPython, TAG_BOOL}; -use crate::error::SnmpError; +use crate::error::{SnmpError, SnmpResult}; use pyo3::{IntoPy, Py, PyAny, Python}; pub struct SnmpBool(bool); @@ -17,7 +17,7 @@ impl<'a> BerDecoder<'a> for SnmpBool { const TAG: Tag = TAG_BOOL; // Implement X.690 pp 8.2: Encoding of a boolean value - fn decode(i: &'a [u8], h: &BerHeader) -> Result { + fn decode(i: &'a [u8], h: &BerHeader) -> SnmpResult { if h.length != 1 { return Err(SnmpError::InvalidData); } @@ -32,7 +32,7 @@ impl From for bool { } impl ToPython for &SnmpBool { - fn try_to_python(self, py: Python) -> Result, SnmpError> { + fn try_to_python(self, py: Python) -> SnmpResult> { Ok(self.0.into_py(py)) } } diff --git a/src/ber/t_counter32.rs b/src/ber/t_counter32.rs index 51fe398..170efd4 100755 --- a/src/ber/t_counter32.rs +++ b/src/ber/t_counter32.rs @@ -6,7 +6,7 @@ // ------------------------------------------------------------------------ use super::{BerDecoder, BerHeader, Tag, ToPython, TAG_APP_COUNTER32}; -use crate::error::SnmpError; +use crate::error::{SnmpResult}; use pyo3::{IntoPy, Py, PyAny, Python}; pub struct SnmpCounter32(pub(crate) u32); @@ -17,7 +17,7 @@ impl<'a> BerDecoder<'a> for SnmpCounter32 { const TAG: Tag = TAG_APP_COUNTER32; // Implement RFC - fn decode(i: &'a [u8], h: &BerHeader) -> Result { + fn decode(i: &'a [u8], h: &BerHeader) -> SnmpResult { let v = i .iter() .take(h.length) @@ -29,7 +29,7 @@ impl<'a> BerDecoder<'a> for SnmpCounter32 { } impl ToPython for &SnmpCounter32 { - fn try_to_python(self, py: Python) -> Result, SnmpError> { + fn try_to_python(self, py: Python) -> SnmpResult> { Ok(self.0.into_py(py)) } } diff --git a/src/ber/t_counter64.rs b/src/ber/t_counter64.rs index 278cdb7..166bb2c 100755 --- a/src/ber/t_counter64.rs +++ b/src/ber/t_counter64.rs @@ -6,7 +6,7 @@ // ------------------------------------------------------------------------ use super::{BerDecoder, BerHeader, Tag, ToPython, TAG_APP_COUNTER64}; -use crate::error::SnmpError; +use crate::error::{SnmpResult}; use pyo3::{IntoPy, Py, PyAny, Python}; pub struct SnmpCounter64(pub(crate) u64); @@ -17,7 +17,7 @@ impl<'a> BerDecoder<'a> for SnmpCounter64 { const TAG: Tag = TAG_APP_COUNTER64; // Implement RFC - fn decode(i: &'a [u8], h: &BerHeader) -> Result { + fn decode(i: &'a [u8], h: &BerHeader) -> SnmpResult { let v = i .iter() .take(h.length) @@ -29,7 +29,7 @@ impl<'a> BerDecoder<'a> for SnmpCounter64 { } impl ToPython for &SnmpCounter64 { - fn try_to_python(self, py: Python) -> Result, SnmpError> { + fn try_to_python(self, py: Python) -> SnmpResult> { Ok(self.0.into_py(py)) } } diff --git a/src/ber/t_gauge32.rs b/src/ber/t_gauge32.rs index 66e0692..fe57518 100755 --- a/src/ber/t_gauge32.rs +++ b/src/ber/t_gauge32.rs @@ -6,7 +6,7 @@ // ------------------------------------------------------------------------ use super::{BerDecoder, BerHeader, Tag, ToPython, TAG_APP_GAUGE32}; -use crate::error::SnmpError; +use crate::error::{SnmpResult}; use pyo3::{IntoPy, Py, PyAny, Python}; pub struct SnmpGauge32(pub(crate) u32); @@ -17,7 +17,7 @@ impl<'a> BerDecoder<'a> for SnmpGauge32 { const TAG: Tag = TAG_APP_GAUGE32; // Implement RFC - fn decode(i: &'a [u8], h: &BerHeader) -> Result { + fn decode(i: &'a [u8], h: &BerHeader) -> SnmpResult { let v = i .iter() .take(h.length) @@ -29,7 +29,7 @@ impl<'a> BerDecoder<'a> for SnmpGauge32 { } impl ToPython for &SnmpGauge32 { - fn try_to_python(self, py: Python) -> Result, SnmpError> { + fn try_to_python(self, py: Python) -> SnmpResult> { Ok(self.0.into_py(py)) } } diff --git a/src/ber/t_int.rs b/src/ber/t_int.rs index 0ab4db1..ded9080 100755 --- a/src/ber/t_int.rs +++ b/src/ber/t_int.rs @@ -7,7 +7,7 @@ use super::{BerDecoder, BerEncoder, BerHeader, Tag, ToPython, TAG_INT}; use crate::buf::Buffer; -use crate::error::SnmpError; +use crate::error::{SnmpResult}; use pyo3::{IntoPy, Py, PyAny, Python}; use std::cmp::Ordering; @@ -19,7 +19,7 @@ impl<'a> BerDecoder<'a> for SnmpInt { const TAG: Tag = TAG_INT; // Implement X.690 pp 8.3: Encoding of an integer value - fn decode(i: &'a [u8], h: &BerHeader) -> Result { + fn decode(i: &'a [u8], h: &BerHeader) -> SnmpResult { if h.is_empty() { return Ok(SnmpInt(0)); } @@ -42,7 +42,7 @@ impl<'a> BerDecoder<'a> for SnmpInt { const ZERO_BER: [u8; 3] = [TAG_INT, 1, 0]; impl BerEncoder for SnmpInt { - fn push_ber(&self, buf: &mut Buffer) -> Result<(), SnmpError> { + fn push_ber(&self, buf: &mut Buffer) -> SnmpResult<()> { match self.0.cmp(&0) { Ordering::Equal => { buf.push(&ZERO_BER)?; @@ -135,7 +135,7 @@ impl SnmpInt { } impl ToPython for &SnmpInt { - fn try_to_python(self, py: Python) -> Result, SnmpError> { + fn try_to_python(self, py: Python) -> SnmpResult> { Ok(self.0.into_py(py)) } } @@ -172,7 +172,7 @@ mod tests { } #[test] - fn test_encode() -> Result<(), SnmpError> { + fn test_encode() -> SnmpResult<()> { let mut buf = Buffer::default(); let data = [0i64, 1, 127, 128, 256, -128, -129, -65535]; let expected = [ diff --git a/src/ber/t_ipaddress.rs b/src/ber/t_ipaddress.rs index 51eea1d..bca8120 100755 --- a/src/ber/t_ipaddress.rs +++ b/src/ber/t_ipaddress.rs @@ -6,7 +6,7 @@ // ------------------------------------------------------------------------ use super::{BerDecoder, BerHeader, Tag, ToPython, TAG_APP_IPADDRESS}; -use crate::error::SnmpError; +use crate::error::{SnmpError, SnmpResult}; use pyo3::types::PyString; use pyo3::{Py, PyAny, Python}; @@ -18,7 +18,7 @@ impl<'a> BerDecoder<'a> for SnmpIpAddress { const TAG: Tag = TAG_APP_IPADDRESS; // Implement RFC - fn decode(i: &'a [u8], h: &BerHeader) -> Result { + fn decode(i: &'a [u8], h: &BerHeader) -> SnmpResult { if h.length != 4 { return Err(SnmpError::InvalidTagFormat); } @@ -33,7 +33,7 @@ impl From<&SnmpIpAddress> for String { } impl ToPython for &SnmpIpAddress { - fn try_to_python(self, py: Python) -> Result, SnmpError> { + fn try_to_python(self, py: Python) -> SnmpResult> { let s: String = self.into(); Ok(PyString::new(py, &s).into()) } diff --git a/src/ber/t_null.rs b/src/ber/t_null.rs index b4cc0ec..d8dba73 100755 --- a/src/ber/t_null.rs +++ b/src/ber/t_null.rs @@ -7,7 +7,7 @@ use super::{BerDecoder, BerEncoder, BerHeader, Tag, TAG_NULL}; use crate::buf::Buffer; -use crate::error::SnmpError; +use crate::error::{SnmpError, SnmpResult}; pub struct SnmpNull; @@ -17,7 +17,7 @@ impl<'a> BerDecoder<'a> for SnmpNull { const TAG: Tag = TAG_NULL; // Implement X.690 pp 8.8: Encoding of a null value - fn decode(_: &'a [u8], h: &BerHeader) -> Result { + fn decode(_: &'a [u8], h: &BerHeader) -> SnmpResult { if h.length != 0 { return Err(SnmpError::InvalidTagFormat); } @@ -28,7 +28,7 @@ impl<'a> BerDecoder<'a> for SnmpNull { const NULL_BER: [u8; 2] = [5u8, 0]; impl BerEncoder for SnmpNull { - fn push_ber(&self, buf: &mut Buffer) -> Result<(), SnmpError> { + fn push_ber(&self, buf: &mut Buffer) -> SnmpResult<()> { buf.push(&NULL_BER)?; Ok(()) } diff --git a/src/ber/t_objectdescriptor.rs b/src/ber/t_objectdescriptor.rs index b5e0adb..cdc970c 100755 --- a/src/ber/t_objectdescriptor.rs +++ b/src/ber/t_objectdescriptor.rs @@ -6,7 +6,7 @@ // ------------------------------------------------------------------------ use super::{BerDecoder, BerHeader, Tag, ToPython, TAG_OBJECT_DESCRIPTOR}; -use crate::error::SnmpError; +use crate::error::{SnmpResult}; use pyo3::types::PyBytes; use pyo3::{Py, PyAny, Python}; @@ -18,13 +18,13 @@ impl<'a> BerDecoder<'a> for SnmpObjectDescriptor<'a> { const TAG: Tag = TAG_OBJECT_DESCRIPTOR; // Implement X.690 pp 8.7: Encoding of an octetstring value - fn decode(i: &'a [u8], h: &BerHeader) -> Result { + fn decode(i: &'a [u8], h: &BerHeader) -> SnmpResult { Ok(SnmpObjectDescriptor(&i[..h.length])) } } impl<'a> ToPython for &SnmpObjectDescriptor<'a> { - fn try_to_python(self, py: Python) -> Result, SnmpError> { + fn try_to_python(self, py: Python) -> SnmpResult> { let v = PyBytes::new(py, self.0); Ok(v.into()) } diff --git a/src/ber/t_objectid.rs b/src/ber/t_objectid.rs index 419305f..b4d7231 100755 --- a/src/ber/t_objectid.rs +++ b/src/ber/t_objectid.rs @@ -7,7 +7,7 @@ use super::{BerDecoder, BerEncoder, BerHeader, Tag, ToPython, TAG_OBJECT_ID}; use crate::buf::Buffer; -use crate::error::SnmpError; +use crate::error::{SnmpError, SnmpResult}; use pyo3::types::PyString; use pyo3::{Py, PyAny, Python}; @@ -25,7 +25,7 @@ impl<'a> BerDecoder<'a> for SnmpOid { const TAG: Tag = TAG_OBJECT_ID; // Implement X.690 pp 8.19: Encoding of an object identifier value - fn decode(i: &'a [u8], h: &BerHeader) -> Result { + fn decode(i: &'a [u8], h: &BerHeader) -> SnmpResult { // First two elements let mut v = Vec::::with_capacity(h.length + 1); let first = i[0] as u32; @@ -46,7 +46,7 @@ impl<'a> BerDecoder<'a> for SnmpOid { } impl BerEncoder for SnmpOid { - fn push_ber(&self, buf: &mut Buffer) -> Result<(), SnmpError> { + fn push_ber(&self, buf: &mut Buffer) -> SnmpResult<()> { if self.0.len() < 2 { return Err(SnmpError::InvalidData); // Too short oid } @@ -65,7 +65,7 @@ impl BerEncoder for SnmpOid { } impl ToPython for &SnmpOid { - fn try_to_python(self, py: Python) -> Result, SnmpError> { + fn try_to_python(self, py: Python) -> SnmpResult> { let v = self .0 .iter() @@ -79,7 +79,7 @@ impl ToPython for &SnmpOid { impl SnmpOid { #[inline] - fn push_subelement(buf: &mut Buffer, se: u32) -> Result<(), SnmpError> { + fn push_subelement(buf: &mut Buffer, se: u32) -> SnmpResult<()> { let mut left = se; // Push least significant 7 bits buf.push_u8((left & 0x7f) as u8)?; diff --git a/src/ber/t_octetstring.rs b/src/ber/t_octetstring.rs index bb5e951..7962978 100755 --- a/src/ber/t_octetstring.rs +++ b/src/ber/t_octetstring.rs @@ -6,7 +6,7 @@ // ------------------------------------------------------------------------ use super::{BerDecoder, BerHeader, Tag, ToPython, TAG_OCTET_STRING}; -use crate::error::SnmpError; +use crate::error::{SnmpResult}; use pyo3::types::PyBytes; use pyo3::{Py, PyAny, Python}; @@ -18,13 +18,13 @@ impl<'a> BerDecoder<'a> for SnmpOctetString<'a> { const TAG: Tag = TAG_OCTET_STRING; // Implement X.690 pp 8.7: Encoding of an octetstring value - fn decode(i: &'a [u8], h: &BerHeader) -> Result { + fn decode(i: &'a [u8], h: &BerHeader) -> SnmpResult { Ok(SnmpOctetString(&i[..h.length])) } } impl<'a> ToPython for &SnmpOctetString<'a> { - fn try_to_python(self, py: Python) -> Result, SnmpError> { + fn try_to_python(self, py: Python) -> SnmpResult> { let v = PyBytes::new(py, self.0); Ok(v.into()) } diff --git a/src/ber/t_opaque.rs b/src/ber/t_opaque.rs index 607bb4e..f3f520a 100755 --- a/src/ber/t_opaque.rs +++ b/src/ber/t_opaque.rs @@ -6,7 +6,7 @@ // ------------------------------------------------------------------------ use super::{BerDecoder, BerHeader, Tag, ToPython, TAG_APP_OPAQUE}; -use crate::error::SnmpError; +use crate::error::{SnmpResult}; use pyo3::types::PyBytes; use pyo3::{Py, PyAny, Python}; @@ -18,13 +18,13 @@ impl<'a> BerDecoder<'a> for SnmpOpaque<'a> { const TAG: Tag = TAG_APP_OPAQUE; // Implement X.690 pp 8.7: Encoding of an Opaque value - fn decode(i: &'a [u8], h: &BerHeader) -> Result { + fn decode(i: &'a [u8], h: &BerHeader) -> SnmpResult { Ok(SnmpOpaque(&i[..h.length])) } } impl<'a> ToPython for &SnmpOpaque<'a> { - fn try_to_python(self, py: Python) -> Result, SnmpError> { + fn try_to_python(self, py: Python) -> SnmpResult> { let v = PyBytes::new(py, self.0); Ok(v.into()) } diff --git a/src/ber/t_option.rs b/src/ber/t_option.rs index e4ce7d4..2f672d3 100755 --- a/src/ber/t_option.rs +++ b/src/ber/t_option.rs @@ -6,7 +6,7 @@ // ------------------------------------------------------------------------ use super::{BerClass, BerDecoder, BerHeader, Tag, TAG_SEQUENCE}; -use crate::error::SnmpError; +use crate::error::{SnmpError, SnmpResult}; use nom::{Err, IResult}; pub struct SnmpOption<'a> { @@ -19,7 +19,7 @@ impl<'a> BerDecoder<'a> for SnmpOption<'a> { const ALLOW_CONSTRUCTED: bool = true; const TAG: Tag = TAG_SEQUENCE; - fn decode(i: &'a [u8], h: &BerHeader) -> Result { + fn decode(i: &'a [u8], h: &BerHeader) -> SnmpResult { Ok(SnmpOption { tag: h.tag, value: &i[..h.length], diff --git a/src/ber/t_real.rs b/src/ber/t_real.rs index 90bf31e..464ac27 100755 --- a/src/ber/t_real.rs +++ b/src/ber/t_real.rs @@ -6,7 +6,7 @@ // ------------------------------------------------------------------------ use super::{BerDecoder, BerHeader, Tag, ToPython, TAG_REAL}; -use crate::error::SnmpError; +use crate::error::{SnmpError, SnmpResult}; use core::str::from_utf8; use pyo3::{IntoPy, Py, PyAny, Python}; @@ -18,7 +18,7 @@ impl<'a> BerDecoder<'a> for SnmpReal { const TAG: Tag = TAG_REAL; // Implement X.690 pp 8.5: Encoding of a real value - fn decode(i: &'a [u8], h: &BerHeader) -> Result { + fn decode(i: &'a [u8], h: &BerHeader) -> SnmpResult { // zero-sized is a zero // 8.5.2: If the real value is the value plus zero, // there shall be no contents octets in the encoding. @@ -122,7 +122,7 @@ impl SnmpReal { } impl ToPython for &SnmpReal { - fn try_to_python(self, py: Python) -> Result, SnmpError> { + fn try_to_python(self, py: Python) -> SnmpResult> { Ok(self.0.into_py(py)) } } diff --git a/src/ber/t_relative_oid.rs b/src/ber/t_relative_oid.rs index 26bd9ab..fb35dbf 100755 --- a/src/ber/t_relative_oid.rs +++ b/src/ber/t_relative_oid.rs @@ -6,7 +6,7 @@ // ------------------------------------------------------------------------ use super::{BerDecoder, BerHeader, SnmpOid, Tag, TAG_RELATIVE_OID}; -use crate::error::SnmpError; +use crate::error::{SnmpResult}; #[derive(Debug, PartialEq, Clone)] pub struct SnmpRelativeOid(pub(crate) Vec); @@ -17,7 +17,7 @@ impl<'a> BerDecoder<'a> for SnmpRelativeOid { const TAG: Tag = TAG_RELATIVE_OID; // Implement X.690 pp 8.20: Encoding of a relative object identifier value - fn decode(i: &'a [u8], h: &BerHeader) -> Result { + fn decode(i: &'a [u8], h: &BerHeader) -> SnmpResult { let mut v = Vec::::with_capacity(h.length + 1); let mut b = 0; for &x in i[..h.length].iter() { diff --git a/src/ber/t_sequence.rs b/src/ber/t_sequence.rs index 9bf0924..96b383e 100755 --- a/src/ber/t_sequence.rs +++ b/src/ber/t_sequence.rs @@ -6,7 +6,7 @@ // ------------------------------------------------------------------------ use super::{BerDecoder, BerHeader, Tag, TAG_SEQUENCE}; -use crate::error::SnmpError; +use crate::error::{SnmpResult}; pub struct SnmpSequence<'a>(pub(crate) &'a [u8]); @@ -16,7 +16,7 @@ impl<'a> BerDecoder<'a> for SnmpSequence<'a> { const TAG: Tag = TAG_SEQUENCE; // Implement X.690 pp 8.9: Encoding of a sequence value - fn decode(i: &'a [u8], h: &BerHeader) -> Result { + fn decode(i: &'a [u8], h: &BerHeader) -> SnmpResult { Ok(SnmpSequence(&i[..h.length])) } } diff --git a/src/ber/t_timeticks.rs b/src/ber/t_timeticks.rs index d7c2767..ecb6767 100755 --- a/src/ber/t_timeticks.rs +++ b/src/ber/t_timeticks.rs @@ -6,7 +6,7 @@ // ------------------------------------------------------------------------ use super::{BerDecoder, BerHeader, Tag, ToPython, TAG_APP_TIMETICKS}; -use crate::error::SnmpError; +use crate::error::{SnmpResult}; use pyo3::{IntoPy, Py, PyAny, Python}; pub struct SnmpTimeTicks(pub(crate) u32); @@ -17,7 +17,7 @@ impl<'a> BerDecoder<'a> for SnmpTimeTicks { const TAG: Tag = TAG_APP_TIMETICKS; // Implement RFC - fn decode(i: &'a [u8], h: &BerHeader) -> Result { + fn decode(i: &'a [u8], h: &BerHeader) -> SnmpResult { let v = i .iter() .take(h.length) @@ -29,7 +29,7 @@ impl<'a> BerDecoder<'a> for SnmpTimeTicks { } impl ToPython for &SnmpTimeTicks { - fn try_to_python(self, py: Python) -> Result, SnmpError> { + fn try_to_python(self, py: Python) -> SnmpResult> { Ok(self.0.into_py(py)) } } diff --git a/src/ber/t_uinteger32.rs b/src/ber/t_uinteger32.rs index 7e00b10..77e455a 100755 --- a/src/ber/t_uinteger32.rs +++ b/src/ber/t_uinteger32.rs @@ -6,7 +6,7 @@ // ------------------------------------------------------------------------ use super::{BerDecoder, BerHeader, Tag, ToPython, TAG_APP_UINTEGER32}; -use crate::error::SnmpError; +use crate::error::{SnmpResult}; use pyo3::{IntoPy, Py, PyAny, Python}; pub struct SnmpUInteger32(pub(crate) u32); @@ -17,7 +17,7 @@ impl<'a> BerDecoder<'a> for SnmpUInteger32 { const TAG: Tag = TAG_APP_UINTEGER32; // Implement RFC - fn decode(i: &'a [u8], h: &BerHeader) -> Result { + fn decode(i: &'a [u8], h: &BerHeader) -> SnmpResult { let v = i .iter() .take(h.length) @@ -29,7 +29,7 @@ impl<'a> BerDecoder<'a> for SnmpUInteger32 { } impl ToPython for &SnmpUInteger32 { - fn try_to_python(self, py: Python) -> Result, SnmpError> { + fn try_to_python(self, py: Python) -> SnmpResult> { Ok(self.0.into_py(py)) } } diff --git a/src/buf.rs b/src/buf.rs index 5d9d1c5..a10ef14 100755 --- a/src/buf.rs +++ b/src/buf.rs @@ -62,7 +62,7 @@ impl Buffer { } } #[inline] - pub fn push_u8(&mut self, v: u8) -> Result<(), SnmpError> { + pub fn push_u8(&mut self, v: u8) -> SnmpResult<()> { if self.is_full() { return Err(SnmpError::OutOfBuffer); } @@ -70,7 +70,7 @@ impl Buffer { self.len += 1; Ok(()) } - pub fn push(&mut self, chunk: &[u8]) -> Result<(), SnmpError> { + pub fn push(&mut self, chunk: &[u8]) -> SnmpResult<()> { let cs = chunk.len(); if self.free() < cs { return Err(SnmpError::OutOfBuffer); @@ -80,7 +80,7 @@ impl Buffer { self.len += cs; Ok(()) } - pub fn push_ber_len(&mut self, v: usize) -> Result<(), SnmpError> { + pub fn push_ber_len(&mut self, v: usize) -> SnmpResult<()> { if v < 128 { // Short form, X.690 pp 8.3.1.4 self.push_u8(v as u8)?; @@ -136,7 +136,7 @@ mod tests { } #[test] - fn test_push_u8() -> Result<(), SnmpError> { + fn test_push_u8() -> SnmpResult<()> { let mut b = Buffer::default(); b.push_u8(1)?; assert_eq!(b.len(), 1); @@ -150,7 +150,7 @@ mod tests { } #[test] - fn test_push() -> Result<(), SnmpError> { + fn test_push() -> SnmpResult<()> { let mut b = Buffer::default(); let chunk = [1u8, 2, 3]; b.push(&chunk)?; @@ -160,7 +160,7 @@ mod tests { } #[test] - fn test_push_ber_len_short1() -> Result<(), SnmpError> { + fn test_push_ber_len_short1() -> SnmpResult<()> { let mut b = Buffer::default(); b.push_ber_len(1)?; assert_eq!(b.len(), 1); @@ -168,7 +168,7 @@ mod tests { Ok(()) } #[test] - fn test_push_ber_len_short2() -> Result<(), SnmpError> { + fn test_push_ber_len_short2() -> SnmpResult<()> { let mut b = Buffer::default(); b.push_ber_len(127)?; assert_eq!(b.len(), 1); @@ -176,7 +176,7 @@ mod tests { Ok(()) } #[test] - fn test_push_ber_len_long1() -> Result<(), SnmpError> { + fn test_push_ber_len_long1() -> SnmpResult<()> { let mut b = Buffer::default(); b.push_ber_len(128)?; assert_eq!(b.len(), 2); @@ -184,7 +184,7 @@ mod tests { Ok(()) } #[test] - fn test_push_ber_len_long2() -> Result<(), SnmpError> { + fn test_push_ber_len_long2() -> SnmpResult<()> { let mut b = Buffer::default(); b.push_ber_len(255)?; assert_eq!(b.len(), 2); @@ -192,7 +192,7 @@ mod tests { Ok(()) } #[test] - fn test_push_ber_len_long3() -> Result<(), SnmpError> { + fn test_push_ber_len_long3() -> SnmpResult<()> { let mut b = Buffer::default(); b.push_ber_len(256)?; assert_eq!(b.len(), 3); diff --git a/src/snmp/get.rs b/src/snmp/get.rs index 0e0bc9a..d8d9cc4 100755 --- a/src/snmp/get.rs +++ b/src/snmp/get.rs @@ -7,7 +7,7 @@ use crate::ber::{BerDecoder, BerEncoder, SnmpInt, SnmpNull, SnmpOid, SnmpSequence}; use crate::buf::Buffer; -use crate::error::SnmpError; +use crate::error::{SnmpError, SnmpResult}; use nom::IResult; const DOUBLE_ZEROES: [u8; 6] = [2u8, 1, 0, 2, 1, 0]; @@ -53,7 +53,7 @@ impl<'a> TryFrom<&'a [u8]> for SnmpGet { } impl BerEncoder for SnmpGet { - fn push_ber(&self, buf: &mut Buffer) -> Result<(), SnmpError> { + fn push_ber(&self, buf: &mut Buffer) -> SnmpResult<()> { // Push all vars in the reversed order let rest = buf.len(); let null = SnmpNull {}; diff --git a/src/snmp/getbulk.rs b/src/snmp/getbulk.rs index 486f8c7..5ded2f7 100755 --- a/src/snmp/getbulk.rs +++ b/src/snmp/getbulk.rs @@ -7,7 +7,7 @@ use crate::ber::{BerDecoder, BerEncoder, SnmpInt, SnmpNull, SnmpOid, SnmpSequence}; use crate::buf::Buffer; -use crate::error::SnmpError; +use crate::error::{SnmpError, SnmpResult}; use nom::IResult; pub struct SnmpGetBulk { @@ -49,7 +49,7 @@ impl<'a> TryFrom<&'a [u8]> for SnmpGetBulk { } impl BerEncoder for SnmpGetBulk { - fn push_ber(&self, buf: &mut Buffer) -> Result<(), SnmpError> { + fn push_ber(&self, buf: &mut Buffer) -> SnmpResult<()> { // Push all vars in the reversed order let rest = buf.len(); let null = SnmpNull {}; diff --git a/src/snmp/getresponse.rs b/src/snmp/getresponse.rs index fe44509..5abdbcd 100755 --- a/src/snmp/getresponse.rs +++ b/src/snmp/getresponse.rs @@ -10,7 +10,7 @@ use crate::ber::{ BerDecoder, SnmpInt, SnmpOid, SnmpRelativeOid, SnmpSequence, Tag, TAG_OBJECT_ID, TAG_RELATIVE_OID, }; -use crate::error::SnmpError; +use crate::error::{SnmpError}; #[allow(dead_code)] pub struct SnmpGetResponse<'a> { diff --git a/src/snmp/msg/v1.rs b/src/snmp/msg/v1.rs index 98b84d6..795f787 100755 --- a/src/snmp/msg/v1.rs +++ b/src/snmp/msg/v1.rs @@ -8,7 +8,7 @@ use crate::ber::{ BerDecoder, BerEncoder, SnmpInt, SnmpOctetString, SnmpSequence, TAG_INT, TAG_OCTET_STRING, }; use crate::buf::Buffer; -use crate::error::SnmpError; +use crate::error::{SnmpError, SnmpResult}; use crate::snmp::pdu::SnmpPdu; use crate::snmp::SNMP_V1; @@ -22,7 +22,7 @@ const V1_BER: [u8; 3] = [TAG_INT, 1, SNMP_V1]; impl<'a> TryFrom<&'a [u8]> for SnmpV1Message<'a> { type Error = SnmpError; - fn try_from(i: &'a [u8]) -> Result, SnmpError> { + fn try_from(i: &'a [u8]) -> SnmpResult> { // Top-level sequence let (tail, envelope) = SnmpSequence::from_ber(i)?; if !tail.is_empty() { @@ -47,7 +47,7 @@ impl<'a> TryFrom<&'a [u8]> for SnmpV1Message<'a> { } impl<'a> BerEncoder for SnmpV1Message<'a> { - fn push_ber(&self, buf: &mut Buffer) -> Result<(), SnmpError> { + fn push_ber(&self, buf: &mut Buffer) -> SnmpResult<()> { // Push PDU self.pdu.push_ber(buf)?; // Push community @@ -71,7 +71,7 @@ mod tests { use crate::snmp::value::SnmpValue; #[test] - fn test_parse_snmp_v1_get() -> Result<(), SnmpError> { + fn test_parse_snmp_v1_get() -> SnmpResult<()> { let data = [ 0x30u8, 0x35, 0x02, 0x01, 0x00, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0xa0, 0x28, 0x02, 0x04, 0x63, 0xcc, 0xac, 0x7d, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, @@ -97,7 +97,7 @@ mod tests { } #[test] - fn test_parse_snmp_getresponse_exception() -> Result<(), SnmpError> { + fn test_parse_snmp_getresponse_exception() -> SnmpResult<()> { let data = [ 48u8, 40, // Seq 40 bytes 2, 1, 0, // INTEGER 1, v1 @@ -130,7 +130,7 @@ mod tests { Ok(()) } #[test] - fn test_parse_snmp_getresponse() -> Result<(), SnmpError> { + fn test_parse_snmp_getresponse() -> SnmpResult<()> { let data = [ 48u8, 55, // Seq 55 bytes 2, 1, 0, // INTEGER, v1 @@ -168,7 +168,7 @@ mod tests { Ok(()) } #[test] - fn test_parse_snmp_getresponse_many() -> Result<(), SnmpError> { + fn test_parse_snmp_getresponse_many() -> SnmpResult<()> { let data = [ 48u8, 129, 134, // Sequence, 134 bytes 2, 1, 0, // ITEGER, v1 @@ -236,7 +236,7 @@ mod tests { Ok(()) } #[test] - fn test_parse_snmp_getresponse_many_rel() -> Result<(), SnmpError> { + fn test_parse_snmp_getresponse_many_rel() -> SnmpResult<()> { let data = [ 48u8, 116, // Sequence, 116 bytes 2, 1, 0, // ITEGER, v1 @@ -304,7 +304,7 @@ mod tests { Ok(()) } #[test] - fn test_encode_snmp_get() -> Result<(), SnmpError> { + fn test_encode_snmp_get() -> SnmpResult<()> { let expected = [ 0x30u8, 0x35, 0x02, 0x01, 0x00, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0xa0, 0x28, 0x02, 0x04, 0x63, 0xcc, 0xac, 0x7d, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, diff --git a/src/snmp/msg/v2c.rs b/src/snmp/msg/v2c.rs index ce11255..d6c2894 100644 --- a/src/snmp/msg/v2c.rs +++ b/src/snmp/msg/v2c.rs @@ -8,7 +8,7 @@ use crate::ber::{ BerDecoder, BerEncoder, SnmpInt, SnmpOctetString, SnmpSequence, TAG_INT, TAG_OCTET_STRING, }; use crate::buf::Buffer; -use crate::error::SnmpError; +use crate::error::{SnmpError, SnmpResult}; use crate::snmp::pdu::SnmpPdu; use crate::snmp::SNMP_V2C; @@ -22,7 +22,7 @@ const V2C_BER: [u8; 3] = [TAG_INT, 1, SNMP_V2C]; impl<'a> TryFrom<&'a [u8]> for SnmpV2cMessage<'a> { type Error = SnmpError; - fn try_from(i: &'a [u8]) -> Result, SnmpError> { + fn try_from(i: &'a [u8]) -> SnmpResult> { // Top-level sequence let (tail, envelope) = SnmpSequence::from_ber(i)?; if !tail.is_empty() { @@ -47,7 +47,7 @@ impl<'a> TryFrom<&'a [u8]> for SnmpV2cMessage<'a> { } impl<'a> BerEncoder for SnmpV2cMessage<'a> { - fn push_ber(&self, buf: &mut Buffer) -> Result<(), SnmpError> { + fn push_ber(&self, buf: &mut Buffer) -> SnmpResult<()> { // Push PDU self.pdu.push_ber(buf)?; // Push community @@ -71,7 +71,7 @@ mod tests { use crate::snmp::value::SnmpValue; #[test] - fn test_parse_snmp_get() -> Result<(), SnmpError> { + fn test_parse_snmp_get() -> SnmpResult<()> { let data = [ 0x30u8, 0x35, 0x02, 0x01, 0x01, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0xa0, 0x28, 0x02, 0x04, 0x63, 0xcc, 0xac, 0x7d, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, @@ -97,7 +97,7 @@ mod tests { } #[test] - fn test_parse_snmp_getresponse_exception() -> Result<(), SnmpError> { + fn test_parse_snmp_getresponse_exception() -> SnmpResult<()> { let data = [ 48u8, 40, // Seq 40 bytes 2, 1, 1, // INTEGER 1, v2C @@ -130,7 +130,7 @@ mod tests { Ok(()) } #[test] - fn test_parse_snmp_getresponse() -> Result<(), SnmpError> { + fn test_parse_snmp_getresponse() -> SnmpResult<()> { let data = [ 48u8, 55, // Seq 55 bytes 2, 1, 1, // INTEGER, v2c @@ -168,7 +168,7 @@ mod tests { Ok(()) } #[test] - fn test_parse_snmp_getresponse_many() -> Result<(), SnmpError> { + fn test_parse_snmp_getresponse_many() -> SnmpResult<()> { let data = [ 48u8, 129, 134, // Sequence, 134 bytes 2, 1, 1, // ITEGER, v2c @@ -236,7 +236,7 @@ mod tests { Ok(()) } #[test] - fn test_parse_snmp_getresponse_many_rel() -> Result<(), SnmpError> { + fn test_parse_snmp_getresponse_many_rel() -> SnmpResult<()> { let data = [ 48u8, 116, // Sequence, 116 bytes 2, 1, 1, // ITEGER, v2c @@ -305,7 +305,7 @@ mod tests { } #[test] - fn test_encode_snmp_get() -> Result<(), SnmpError> { + fn test_encode_snmp_get() -> SnmpResult<()> { let expected = [ 0x30u8, 0x35, 0x02, 0x01, 0x01, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0xa0, 0x28, 0x02, 0x04, 0x63, 0xcc, 0xac, 0x7d, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, diff --git a/src/snmp/msg/v3/data.rs b/src/snmp/msg/v3/data.rs index f7e1979..0c1a7ec 100644 --- a/src/snmp/msg/v3/data.rs +++ b/src/snmp/msg/v3/data.rs @@ -34,7 +34,7 @@ impl<'a> TryFrom<&'a [u8]> for MsgData<'a> { } impl<'a> BerEncoder for MsgData<'a> { - fn push_ber(&self, buf: &mut Buffer) -> Result<(), SnmpError> { + fn push_ber(&self, buf: &mut Buffer) -> SnmpResult<()> { match self { MsgData::Plaintext(x) => x.push_ber(buf), MsgData::Encrypted(x) => buf.push_tagged(TAG_OCTET_STRING, x), diff --git a/src/snmp/msg/v3/msg.rs b/src/snmp/msg/v3/msg.rs index 823ffc2..a8feae1 100644 --- a/src/snmp/msg/v3/msg.rs +++ b/src/snmp/msg/v3/msg.rs @@ -10,7 +10,7 @@ use crate::ber::{ BerDecoder, BerEncoder, SnmpInt, SnmpOctetString, SnmpSequence, TAG_INT, TAG_OCTET_STRING, }; use crate::buf::Buffer; -use crate::error::SnmpError; +use crate::error::{SnmpError, SnmpResult}; use crate::snmp::SNMP_V3; pub struct SnmpV3Message<'a> { @@ -36,7 +36,7 @@ const FLAG_AUTH: u8 = 1; impl<'a> TryFrom<&'a [u8]> for SnmpV3Message<'a> { type Error = SnmpError; - fn try_from(i: &'a [u8]) -> Result, SnmpError> { + fn try_from(i: &'a [u8]) -> SnmpResult> { // Top-level sequence let (tail, envelope) = SnmpSequence::from_ber(i)?; if !tail.is_empty() { @@ -85,7 +85,7 @@ impl<'a> TryFrom<&'a [u8]> for SnmpV3Message<'a> { } impl<'a> BerEncoder for SnmpV3Message<'a> { - fn push_ber(&self, buf: &mut Buffer) -> Result<(), SnmpError> { + fn push_ber(&self, buf: &mut Buffer) -> SnmpResult<()> { // // Scoped PDU // @@ -145,7 +145,7 @@ mod tests { use crate::snmp::pdu::SnmpPdu; #[test] - fn test_parse_snmp_get() -> Result<(), SnmpError> { + fn test_parse_snmp_get() -> SnmpResult<()> { let data = [ 0x30u8, 0x40, 0x02, 0x01, 0x03, 0x30, 0x0f, 0x02, 0x03, 0x00, 0x91, 0xc8, 0x02, 0x02, 0x05, 0xdc, 0x04, 0x01, 0x00, 0x02, 0x01, 0x03, 0x04, 0x15, 0x30, 0x13, 0x04, 0x00, @@ -179,7 +179,7 @@ mod tests { } #[test] - fn test_encode_snmp_get() -> Result<(), SnmpError> { + fn test_encode_snmp_get() -> SnmpResult<()> { let expected = [ 0x30u8, 0x40, 0x02, 0x01, 0x03, 0x30, 0x0f, 0x02, 0x03, 0x00, 0x91, 0xc8, 0x02, 0x02, 0x05, 0xdc, 0x04, 0x01, 0x00, 0x02, 0x01, 0x03, 0x04, 0x15, 0x30, 0x13, 0x04, 0x00, @@ -216,7 +216,7 @@ mod tests { } // #[test] - // fn test_parse_snmp_getresponse_exception() -> Result<(), SnmpError> { + // fn test_parse_snmp_getresponse_exception() -> SnmpResult<()> { // let data = [ // 48u8, 40, // Seq 40 bytes // 2, 1, 0, // INTEGER 1, v1 @@ -249,7 +249,7 @@ mod tests { // Ok(()) // } // #[test] - // fn test_parse_snmp_getresponse() -> Result<(), SnmpError> { + // fn test_parse_snmp_getresponse() -> SnmpResult<()> { // let data = [ // 48u8, 55, // Seq 55 bytes // 2, 1, 0, // INTEGER, v1 @@ -287,7 +287,7 @@ mod tests { // Ok(()) // } // #[test] - // fn test_parse_snmp_getresponse_many() -> Result<(), SnmpError> { + // fn test_parse_snmp_getresponse_many() -> SnmpResult<()> { // let data = [ // 48u8, 129, 134, // Sequence, 134 bytes // 2, 1, 0, // ITEGER, v1 @@ -355,7 +355,7 @@ mod tests { // Ok(()) // } // #[test] - // fn test_parse_snmp_getresponse_many_rel() -> Result<(), SnmpError> { + // fn test_parse_snmp_getresponse_many_rel() -> SnmpResult<()> { // let data = [ // 48u8, 116, // Sequence, 116 bytes // 2, 1, 0, // ITEGER, v1 @@ -423,7 +423,7 @@ mod tests { // Ok(()) // } // #[test] - // fn test_encode_snmp_get() -> Result<(), SnmpError> { + // fn test_encode_snmp_get() -> SnmpResult<()> { // let expected = [ // 0x30u8, 0x35, 0x02, 0x01, 0x00, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0xa0, // 0x28, 0x02, 0x04, 0x63, 0xcc, 0xac, 0x7d, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, diff --git a/src/snmp/msg/v3/usm.rs b/src/snmp/msg/v3/usm.rs index 75f5980..7317ef7 100644 --- a/src/snmp/msg/v3/usm.rs +++ b/src/snmp/msg/v3/usm.rs @@ -9,7 +9,7 @@ use crate::ber::{ BerDecoder, BerEncoder, SnmpInt, SnmpOctetString, SnmpSequence, TAG_OCTET_STRING, }; use crate::buf::Buffer; -use crate::error::SnmpError; +use crate::error::{SnmpError, SnmpResult}; pub struct UsmParameters<'a> { pub engine_id: &'a [u8], @@ -23,7 +23,7 @@ pub struct UsmParameters<'a> { impl<'a> TryFrom<&'a [u8]> for UsmParameters<'a> { type Error = SnmpError; - fn try_from(i: &'a [u8]) -> Result, SnmpError> { + fn try_from(i: &'a [u8]) -> SnmpResult> { // Top-level sequence let (tail, envelope) = SnmpSequence::from_ber(i)?; if !tail.is_empty() { @@ -55,7 +55,7 @@ impl<'a> TryFrom<&'a [u8]> for UsmParameters<'a> { const EMPTY_BER: [u8; 2] = [TAG_OCTET_STRING, 0]; impl<'a> BerEncoder for UsmParameters<'a> { - fn push_ber(&self, buf: &mut Buffer) -> Result<(), SnmpError> { + fn push_ber(&self, buf: &mut Buffer) -> SnmpResult<()> { let l0 = buf.len(); // Push privacy parameters if self.privacy_params.is_empty() { @@ -100,7 +100,7 @@ mod tests { use super::*; #[test] - fn test_parse_usm_plain() -> Result<(), SnmpError> { + fn test_parse_usm_plain() -> SnmpResult<()> { let data = [ 0x30, 0x13, 0x04, 0x00, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x04, 0x05, 0x61, 0x64, 0x6d, 0x69, 0x6e, 0x04, 0x00, 0x04, 0x00, @@ -116,7 +116,7 @@ mod tests { Ok(()) } #[test] - fn test_push_usm_plain() -> Result<(), SnmpError> { + fn test_push_usm_plain() -> SnmpResult<()> { let expected = [ 0x30, 0x13, 0x04, 0x00, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x04, 0x05, 0x61, 0x64, 0x6d, 0x69, 0x6e, 0x04, 0x00, 0x04, 0x00, diff --git a/src/snmp/pdu.rs b/src/snmp/pdu.rs index 576f1d0..c457c74 100755 --- a/src/snmp/pdu.rs +++ b/src/snmp/pdu.rs @@ -14,7 +14,7 @@ use super::{ }; use crate::ber::{BerDecoder, BerEncoder, SnmpOption}; use crate::buf::Buffer; -use crate::error::SnmpError; +use crate::error::{SnmpError, SnmpResult}; use crate::reqid::RequestId; #[allow(clippy::enum_variant_names)] @@ -43,7 +43,7 @@ impl<'a> TryFrom<&'a [u8]> for SnmpPdu<'a> { } impl<'a> BerEncoder for SnmpPdu<'a> { - fn push_ber(&self, buf: &mut Buffer) -> Result<(), SnmpError> { + fn push_ber(&self, buf: &mut Buffer) -> SnmpResult<()> { let rest = buf.len(); match self { SnmpPdu::GetRequest(req) => { diff --git a/src/snmp/value.rs b/src/snmp/value.rs index 3657ee8..9fa3393 100755 --- a/src/snmp/value.rs +++ b/src/snmp/value.rs @@ -13,7 +13,7 @@ use crate::ber::{ TAG_CTX_END_OF_MIB_VIEW, TAG_CTX_NO_SUCH_INSTANCE, TAG_CTX_NO_SUCH_OBJECT, TAG_INT, TAG_NULL, TAG_OBJECT_DESCRIPTOR, TAG_OBJECT_ID, TAG_OCTET_STRING, TAG_REAL, }; -use crate::error::SnmpError; +use crate::error::{SnmpError, SnmpResult}; use nom::{Err, IResult}; use pyo3::{Py, PyAny, Python}; @@ -117,7 +117,7 @@ impl<'a> SnmpValue<'a> { } impl<'a> ToPython for &SnmpValue<'a> { - fn try_to_python(self, py: Python) -> Result, SnmpError> { + fn try_to_python(self, py: Python) -> SnmpResult> { Ok(match self { SnmpValue::Bool(x) => x.try_to_python(py)?, SnmpValue::Int(x) => x.try_to_python(py)?, @@ -142,10 +142,10 @@ impl<'a> ToPython for &SnmpValue<'a> { #[cfg(test)] mod tests { use super::*; - use crate::error::SnmpError; + use crate::error::{SnmpError, SnmpResult}; #[test] - fn test_bool() -> Result<(), SnmpError> { + fn test_bool() -> SnmpResult<()> { let data = [1u8, 1, 1]; let (tail, value) = SnmpValue::from_ber(&data)?; assert_eq!(tail.len(), 0); @@ -158,7 +158,7 @@ mod tests { } #[test] - fn test_int() -> Result<(), SnmpError> { + fn test_int() -> SnmpResult<()> { let data = [2u8, 1, 10]; let (tail, value) = SnmpValue::from_ber(&data)?; assert_eq!(tail.len(), 0); @@ -172,7 +172,7 @@ mod tests { } #[test] - fn test_null() -> Result<(), SnmpError> { + fn test_null() -> SnmpResult<()> { let data = [5u8, 0]; let (tail, value) = SnmpValue::from_ber(&data)?; assert_eq!(tail.len(), 0); @@ -184,7 +184,7 @@ mod tests { } #[test] - fn test_octet_string() -> Result<(), SnmpError> { + fn test_octet_string() -> SnmpResult<()> { let data = [4u8, 5, 0, 1, 2, 3, 4]; let (tail, value) = SnmpValue::from_ber(&data)?; assert_eq!(tail.len(), 0); @@ -196,7 +196,7 @@ mod tests { } } #[test] - fn test_object_id() -> Result<(), SnmpError> { + fn test_object_id() -> SnmpResult<()> { let data = [0x6u8, 0x8, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x05, 0x00]; let expected = [1u32, 3, 6, 1, 2, 1, 1, 5, 0]; let (tail, value) = SnmpValue::from_ber(&data)?; @@ -209,7 +209,7 @@ mod tests { } } #[test] - fn test_object_descriptor() -> Result<(), SnmpError> { + fn test_object_descriptor() -> SnmpResult<()> { let data = [7u8, 5, 0, 1, 2, 3, 4]; let (tail, value) = SnmpValue::from_ber(&data)?; assert_eq!(tail.len(), 0); @@ -221,7 +221,7 @@ mod tests { } } #[test] - fn test_real() -> Result<(), SnmpError> { + fn test_real() -> SnmpResult<()> { let data = [9u8, 0]; let (tail, value) = SnmpValue::from_ber(&data)?; assert_eq!(tail.len(), 0); @@ -234,7 +234,7 @@ mod tests { } } #[test] - fn test_ipaddress() -> Result<(), SnmpError> { + fn test_ipaddress() -> SnmpResult<()> { let data = [0x40, 0x4, 127, 0, 0, 1]; let (tail, value) = SnmpValue::from_ber(&data)?; assert_eq!(tail.len(), 0); @@ -247,7 +247,7 @@ mod tests { } } #[test] - fn test_counter32() -> Result<(), SnmpError> { + fn test_counter32() -> SnmpResult<()> { let data = [0x41, 4, 1, 53, 16, 171]; let (tail, value) = SnmpValue::from_ber(&data)?; assert_eq!(tail.len(), 0); @@ -259,7 +259,7 @@ mod tests { } } #[test] - fn test_gauge32() -> Result<(), SnmpError> { + fn test_gauge32() -> SnmpResult<()> { let data = [0x42, 4, 1, 53, 16, 171]; let (tail, value) = SnmpValue::from_ber(&data)?; assert_eq!(tail.len(), 0); @@ -271,7 +271,7 @@ mod tests { } } #[test] - fn test_timeticks() -> Result<(), SnmpError> { + fn test_timeticks() -> SnmpResult<()> { let data = [67, 4, 1, 53, 16, 171]; let (tail, value) = SnmpValue::from_ber(&data)?; assert_eq!(tail.len(), 0); @@ -283,7 +283,7 @@ mod tests { } } #[test] - fn test_opaque() -> Result<(), SnmpError> { + fn test_opaque() -> SnmpResult<()> { let data = [0x44, 5, 0, 1, 2, 3, 4]; let (tail, value) = SnmpValue::from_ber(&data)?; assert_eq!(tail.len(), 0); @@ -295,7 +295,7 @@ mod tests { } } #[test] - fn test_counter64() -> Result<(), SnmpError> { + fn test_counter64() -> SnmpResult<()> { let data = [0x46, 4, 1, 53, 16, 171]; let (tail, value) = SnmpValue::from_ber(&data)?; assert_eq!(tail.len(), 0); @@ -307,7 +307,7 @@ mod tests { } } #[test] - fn test_uinteger32() -> Result<(), SnmpError> { + fn test_uinteger32() -> SnmpResult<()> { let data = [0x47, 4, 1, 53, 16, 171]; let (tail, value) = SnmpValue::from_ber(&data)?; assert_eq!(tail.len(), 0); @@ -319,7 +319,7 @@ mod tests { } } #[test] - fn test_no_such_object() -> Result<(), SnmpError> { + fn test_no_such_object() -> SnmpResult<()> { let data = [0x80u8, 0]; let (tail, value) = SnmpValue::from_ber(&data)?; assert_eq!(tail.len(), 0); @@ -330,7 +330,7 @@ mod tests { } } #[test] - fn test_no_such_instance() -> Result<(), SnmpError> { + fn test_no_such_instance() -> SnmpResult<()> { let data = [0x81u8, 0]; let (tail, value) = SnmpValue::from_ber(&data)?; assert_eq!(tail.len(), 0); @@ -341,7 +341,7 @@ mod tests { } } #[test] - fn test_end_of_mib_view() -> Result<(), SnmpError> { + fn test_end_of_mib_view() -> SnmpResult<()> { let data = [0x82u8, 0]; let (tail, value) = SnmpValue::from_ber(&data)?; assert_eq!(tail.len(), 0); diff --git a/src/socket/transport.rs b/src/socket/transport.rs index f3bca94..1370282 100644 --- a/src/socket/transport.rs +++ b/src/socket/transport.rs @@ -23,7 +23,7 @@ impl SnmpTransport { tos: u32, send_buffer_size: usize, recv_buffer_size: usize, - ) -> Result { + ) -> SnmpResult { // Parse address let sock_addr = addr .parse() @@ -89,7 +89,7 @@ impl SnmpTransport { self.send_buffer() } /// Receive message from socket - pub fn recv<'a, 'b, T>(&'b mut self) -> Result + pub fn recv<'a, 'b, T>(&'b mut self) -> SnmpResult where T: TryFrom<&'a [u8], Error = SnmpError>, 'b: 'a, @@ -108,7 +108,7 @@ impl SnmpTransport { T::try_from(self.buf.as_slice(size)) } /// Set internal socket's send buffer size - fn set_send_buffer_size(io: &Socket, size: usize) -> Result<(), SnmpError> { + fn set_send_buffer_size(io: &Socket, size: usize) -> SnmpResult<()> { // @todo: get wmem_max limit on Linux let mut effective_size = size; while effective_size > 0 { @@ -120,7 +120,7 @@ impl SnmpTransport { Err(SnmpError::SocketError("unable to set buffer size".into())) } /// Set internal socket's receive buffer size - fn set_recv_buffer_size(io: &Socket, size: usize) -> Result<(), SnmpError> { + fn set_recv_buffer_size(io: &Socket, size: usize) -> SnmpResult<()> { let mut effective_size = size; while effective_size > 0 { if io.set_recv_buffer_size(effective_size).is_ok() {