Wrap FromUtf8Error by AVPError

This commit is contained in:
moznion
2020-11-28 18:12:09 +09:00
parent b46b4ceaf9
commit 09a82d3454
+8 -7
View File
@@ -1,9 +1,7 @@
use std::convert::TryInto; use std::convert::TryInto;
use std::net::{Ipv4Addr, Ipv6Addr}; use std::net::{Ipv4Addr, Ipv6Addr};
use std::string::FromUtf8Error;
use chrono::{DateTime, TimeZone, Utc}; use chrono::{DateTime, TimeZone, Utc};
use thiserror::Error; use thiserror::Error;
#[derive(Error, Debug)] #[derive(Error, Debug)]
@@ -142,8 +140,11 @@ impl AVP {
} }
} }
pub fn decode_string(&self) -> Result<String, FromUtf8Error> { pub fn decode_string(&self) -> Result<String, AVPError> {
String::from_utf8(self.value.to_vec()) match String::from_utf8(self.value.to_vec()) {
Ok(str) => Ok(str),
Err(e) => Err(AVPError::UnexpectedDecodingError(e.to_string())),
}
} }
pub fn decode_bytes(&self) -> Vec<u8> { pub fn decode_bytes(&self) -> Vec<u8> {
@@ -239,10 +240,10 @@ impl AVP {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::net::{Ipv4Addr, Ipv6Addr}; use std::net::{Ipv4Addr, Ipv6Addr};
use std::string::FromUtf8Error;
use chrono::Utc;
use crate::avp::{AVPError, AVP}; use crate::avp::{AVPError, AVP};
use chrono::Utc;
#[test] #[test]
fn it_should_convert_attribute_to_integer32() -> Result<(), AVPError> { fn it_should_convert_attribute_to_integer32() -> Result<(), AVPError> {
@@ -253,7 +254,7 @@ mod tests {
} }
#[test] #[test]
fn it_should_convert_attribute_to_string() -> Result<(), FromUtf8Error> { fn it_should_convert_attribute_to_string() -> Result<(), AVPError> {
let given_str = "Hello, World"; let given_str = "Hello, World";
let avp = AVP::encode_string(1, given_str); let avp = AVP::encode_string(1, given_str);
assert_eq!(avp.decode_string()?, given_str); assert_eq!(avp.decode_string()?, given_str);