return results instead of unwrapping

This commit is contained in:
Jan Christian Grünhage 2020-04-19 16:31:35 +02:00
parent 6757ad28f8
commit 40b38d0bec
Signed by: jcgruenhage
GPG key ID: 6594C449C633D10C
3 changed files with 12 additions and 12 deletions

View file

@ -1,4 +1,4 @@
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
println!("{}", yapilt::reqwest_client::ipv4(&mut reqwest::Client::new()).await); println!("{}", yapilt::reqwest_client::ipv4(&mut reqwest::Client::new()).await.unwrap());
} }

View file

@ -1,4 +1,4 @@
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
println!("{}", yapilt::reqwest_client::ipv6(&mut reqwest::Client::new()).await); println!("{}", yapilt::reqwest_client::ipv6(&mut reqwest::Client::new()).await.unwrap());
} }

View file

@ -11,12 +11,12 @@ pub mod http {
use crate::{Error, IpType, Provider}; use crate::{Error, IpType, Provider};
use std::default::Default; use std::default::Default;
pub fn ipv4() -> http::Request<&'static [u8]> { pub fn ipv4() -> Result<http::Request<&'static [u8]>, Error> {
ip(IpType::V4, Provider::default()).unwrap() ip(IpType::V4, Provider::default())
} }
pub fn ipv6() -> http::Request<&'static [u8]> { pub fn ipv6() -> Result<http::Request<&'static [u8]>, Error> {
ip(IpType::V6, Provider::default()).unwrap() ip(IpType::V6, Provider::default())
} }
pub fn ip<T: Into<Provider>>(ip_type: IpType, provider: T) -> Result<http::Request<&'static [u8]>, Error> { pub fn ip<T: Into<Provider>>(ip_type: IpType, provider: T) -> Result<http::Request<&'static [u8]>, Error> {
@ -34,9 +34,9 @@ pub mod reqwest_client {
use std::default::Default; use std::default::Default;
use std::convert::TryInto; use std::convert::TryInto;
pub async fn ipv4(client: &mut Client) -> Ipv4Addr { pub async fn ipv4(client: &mut Client) -> Result<Ipv4Addr, Error> {
match ip(client, IpType::V4, Provider::default()).await.unwrap() { match ip(client, IpType::V4, Provider::default()).await? {
IpAddr::V4(ip) => ip, IpAddr::V4(ip) => Ok(ip),
_ => { _ => {
log::error!("This should be unreachable, if the provider is working and configured correctly"); log::error!("This should be unreachable, if the provider is working and configured correctly");
panic!(); panic!();
@ -44,9 +44,9 @@ pub mod reqwest_client {
} }
} }
pub async fn ipv6(client: &mut Client) -> Ipv6Addr { pub async fn ipv6(client: &mut Client) -> Result<Ipv6Addr, Error> {
match ip(client, IpType::V6, Provider::default()).await.unwrap() { match ip(client, IpType::V6, Provider::default()).await? {
IpAddr::V6(ip) => ip, IpAddr::V6(ip) => Ok(ip),
_ => { _ => {
log::error!("This should be unreachable, if the provider is working and configured correctly"); log::error!("This should be unreachable, if the provider is working and configured correctly");
panic!(); panic!();