Skip to content

Commit

Permalink
Use SnmpResult type whenever possibile
Browse files Browse the repository at this point in the history
  • Loading branch information
dvolodin7 committed Jan 22, 2024
1 parent 5da841e commit 47172b0
Show file tree
Hide file tree
Showing 31 changed files with 134 additions and 134 deletions.
6 changes: 3 additions & 3 deletions src/ber/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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)?;
Expand Down
8 changes: 4 additions & 4 deletions src/ber/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -92,7 +92,7 @@ where
const ALLOW_CONSTRUCTED: bool;
const TAG: Tag;

fn decode(i: &'a [u8], hdr: &BerHeader) -> Result<Self, SnmpError>;
fn decode(i: &'a [u8], hdr: &BerHeader) -> SnmpResult<Self>;

fn from_ber(i: &'a [u8]) -> IResult<&'a [u8], Self, SnmpError> {
if i.len() < 2 {
Expand All @@ -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<Py<PyAny>, SnmpError>;
fn try_to_python(self, py: Python) -> SnmpResult<Py<PyAny>>;
}
6 changes: 3 additions & 3 deletions src/ber/t_bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<Self, SnmpError> {
fn decode(i: &'a [u8], h: &BerHeader) -> SnmpResult<Self> {
if h.length != 1 {
return Err(SnmpError::InvalidData);
}
Expand All @@ -32,7 +32,7 @@ impl From<SnmpBool> for bool {
}

impl ToPython for &SnmpBool {
fn try_to_python(self, py: Python) -> Result<Py<PyAny>, SnmpError> {
fn try_to_python(self, py: Python) -> SnmpResult<Py<PyAny>> {
Ok(self.0.into_py(py))
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/ber/t_counter32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<Self, SnmpError> {
fn decode(i: &'a [u8], h: &BerHeader) -> SnmpResult<Self> {
let v = i
.iter()
.take(h.length)
Expand All @@ -29,7 +29,7 @@ impl<'a> BerDecoder<'a> for SnmpCounter32 {
}

impl ToPython for &SnmpCounter32 {
fn try_to_python(self, py: Python) -> Result<Py<PyAny>, SnmpError> {
fn try_to_python(self, py: Python) -> SnmpResult<Py<PyAny>> {
Ok(self.0.into_py(py))
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/ber/t_counter64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<Self, SnmpError> {
fn decode(i: &'a [u8], h: &BerHeader) -> SnmpResult<Self> {
let v = i
.iter()
.take(h.length)
Expand All @@ -29,7 +29,7 @@ impl<'a> BerDecoder<'a> for SnmpCounter64 {
}

impl ToPython for &SnmpCounter64 {
fn try_to_python(self, py: Python) -> Result<Py<PyAny>, SnmpError> {
fn try_to_python(self, py: Python) -> SnmpResult<Py<PyAny>> {
Ok(self.0.into_py(py))
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/ber/t_gauge32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<Self, SnmpError> {
fn decode(i: &'a [u8], h: &BerHeader) -> SnmpResult<Self> {
let v = i
.iter()
.take(h.length)
Expand All @@ -29,7 +29,7 @@ impl<'a> BerDecoder<'a> for SnmpGauge32 {
}

impl ToPython for &SnmpGauge32 {
fn try_to_python(self, py: Python) -> Result<Py<PyAny>, SnmpError> {
fn try_to_python(self, py: Python) -> SnmpResult<Py<PyAny>> {
Ok(self.0.into_py(py))
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/ber/t_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Self, SnmpError> {
fn decode(i: &'a [u8], h: &BerHeader) -> SnmpResult<Self> {
if h.is_empty() {
return Ok(SnmpInt(0));
}
Expand All @@ -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)?;
Expand Down Expand Up @@ -135,7 +135,7 @@ impl SnmpInt {
}

impl ToPython for &SnmpInt {
fn try_to_python(self, py: Python) -> Result<Py<PyAny>, SnmpError> {
fn try_to_python(self, py: Python) -> SnmpResult<Py<PyAny>> {
Ok(self.0.into_py(py))
}
}
Expand Down Expand Up @@ -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 = [
Expand Down
6 changes: 3 additions & 3 deletions src/ber/t_ipaddress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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<Self, SnmpError> {
fn decode(i: &'a [u8], h: &BerHeader) -> SnmpResult<Self> {
if h.length != 4 {
return Err(SnmpError::InvalidTagFormat);
}
Expand All @@ -33,7 +33,7 @@ impl From<&SnmpIpAddress> for String {
}

impl ToPython for &SnmpIpAddress {
fn try_to_python(self, py: Python) -> Result<Py<PyAny>, SnmpError> {
fn try_to_python(self, py: Python) -> SnmpResult<Py<PyAny>> {
let s: String = self.into();
Ok(PyString::new(py, &s).into())
}
Expand Down
6 changes: 3 additions & 3 deletions src/ber/t_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Self, SnmpError> {
fn decode(_: &'a [u8], h: &BerHeader) -> SnmpResult<Self> {
if h.length != 0 {
return Err(SnmpError::InvalidTagFormat);
}
Expand All @@ -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(())
}
Expand Down
6 changes: 3 additions & 3 deletions src/ber/t_objectdescriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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<Self, SnmpError> {
fn decode(i: &'a [u8], h: &BerHeader) -> SnmpResult<Self> {
Ok(SnmpObjectDescriptor(&i[..h.length]))
}
}

impl<'a> ToPython for &SnmpObjectDescriptor<'a> {
fn try_to_python(self, py: Python) -> Result<Py<PyAny>, SnmpError> {
fn try_to_python(self, py: Python) -> SnmpResult<Py<PyAny>> {
let v = PyBytes::new(py, self.0);
Ok(v.into())
}
Expand Down
10 changes: 5 additions & 5 deletions src/ber/t_objectid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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<Self, SnmpError> {
fn decode(i: &'a [u8], h: &BerHeader) -> SnmpResult<Self> {
// First two elements
let mut v = Vec::<u32>::with_capacity(h.length + 1);
let first = i[0] as u32;
Expand All @@ -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
}
Expand All @@ -65,7 +65,7 @@ impl BerEncoder for SnmpOid {
}

impl ToPython for &SnmpOid {
fn try_to_python(self, py: Python) -> Result<Py<PyAny>, SnmpError> {
fn try_to_python(self, py: Python) -> SnmpResult<Py<PyAny>> {
let v = self
.0
.iter()
Expand All @@ -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)?;
Expand Down
6 changes: 3 additions & 3 deletions src/ber/t_octetstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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<Self, SnmpError> {
fn decode(i: &'a [u8], h: &BerHeader) -> SnmpResult<Self> {
Ok(SnmpOctetString(&i[..h.length]))
}
}

impl<'a> ToPython for &SnmpOctetString<'a> {
fn try_to_python(self, py: Python) -> Result<Py<PyAny>, SnmpError> {
fn try_to_python(self, py: Python) -> SnmpResult<Py<PyAny>> {
let v = PyBytes::new(py, self.0);
Ok(v.into())
}
Expand Down
6 changes: 3 additions & 3 deletions src/ber/t_opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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<Self, SnmpError> {
fn decode(i: &'a [u8], h: &BerHeader) -> SnmpResult<Self> {
Ok(SnmpOpaque(&i[..h.length]))
}
}

impl<'a> ToPython for &SnmpOpaque<'a> {
fn try_to_python(self, py: Python) -> Result<Py<PyAny>, SnmpError> {
fn try_to_python(self, py: Python) -> SnmpResult<Py<PyAny>> {
let v = PyBytes::new(py, self.0);
Ok(v.into())
}
Expand Down
Loading

0 comments on commit 47172b0

Please sign in to comment.