Remove all unwraps
This commit is contained in:
parent
0132d32507
commit
ce592985ce
2 changed files with 23 additions and 48 deletions
|
@ -10,7 +10,6 @@ use std::fmt;
|
||||||
use std::fs::{File, OpenOptions};
|
use std::fs::{File, OpenOptions};
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
const CONFIG_FILENAME: &str = "bunbun.yaml";
|
const CONFIG_FILENAME: &str = "bunbun.yaml";
|
||||||
const DEFAULT_CONFIG: &[u8] = include_bytes!("../bunbun.default.yaml");
|
const DEFAULT_CONFIG: &[u8] = include_bytes!("../bunbun.default.yaml");
|
||||||
|
@ -46,20 +45,6 @@ pub struct Route {
|
||||||
pub max_args: Option<usize>,
|
pub max_args: Option<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for Route {
|
|
||||||
type Err = std::convert::Infallible;
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
||||||
Ok(Self {
|
|
||||||
route_type: get_route_type(s),
|
|
||||||
path: s.to_string(),
|
|
||||||
hidden: false,
|
|
||||||
description: None,
|
|
||||||
min_args: None,
|
|
||||||
max_args: None,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<String> for Route {
|
impl From<String> for Route {
|
||||||
fn from(s: String) -> Self {
|
fn from(s: String) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
@ -119,8 +104,7 @@ impl<'de> Deserialize<'de> for Route {
|
||||||
where
|
where
|
||||||
E: serde::de::Error,
|
E: serde::de::Error,
|
||||||
{
|
{
|
||||||
// This is infallible
|
Ok(Self::Value::from(path.to_owned()))
|
||||||
Ok(Self::Value::from_str(path).unwrap())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
|
fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
|
||||||
|
@ -354,19 +338,20 @@ mod route {
|
||||||
use super::*;
|
use super::*;
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use serde_yaml::{from_str, to_string};
|
use serde_yaml::{from_str, to_string};
|
||||||
|
use std::path::Path;
|
||||||
use tempfile::NamedTempFile;
|
use tempfile::NamedTempFile;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn deserialize_relative_path() -> Result<()> {
|
fn deserialize_relative_path() -> Result<()> {
|
||||||
let tmpfile = NamedTempFile::new_in(".")?;
|
let tmpfile = NamedTempFile::new_in(".")?;
|
||||||
let path = format!("{}", tmpfile.path().display());
|
let path = tmpfile.path().display().to_string();
|
||||||
let path = path
|
let path = path
|
||||||
.get(path.rfind(".").context("While finding .")?..)
|
.get(path.rfind(".").context("While finding .")?..)
|
||||||
.context("While getting the path")?;
|
.context("While getting the path")?;
|
||||||
let path = std::path::Path::new(path);
|
let path = Path::new(path);
|
||||||
assert!(path.is_relative());
|
assert!(path.is_relative());
|
||||||
let path = path.to_str().context("While stringifying path")?;
|
let path = path.to_str().context("While stringifying path")?;
|
||||||
assert_eq!(from_str::<Route>(path)?, Route::from_str(path)?);
|
assert_eq!(from_str::<Route>(path)?, Route::from(path.to_owned()));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -375,7 +360,7 @@ mod route {
|
||||||
let tmpfile = NamedTempFile::new()?;
|
let tmpfile = NamedTempFile::new()?;
|
||||||
let path = format!("{}", tmpfile.path().display());
|
let path = format!("{}", tmpfile.path().display());
|
||||||
assert!(tmpfile.path().is_absolute());
|
assert!(tmpfile.path().is_absolute());
|
||||||
assert_eq!(from_str::<Route>(&path)?, Route::from_str(&path)?);
|
assert_eq!(from_str::<Route>(&path)?, Route::from(path));
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -384,7 +369,7 @@ mod route {
|
||||||
fn deserialize_http_path() -> Result<()> {
|
fn deserialize_http_path() -> Result<()> {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
from_str::<Route>("http://google.com")?,
|
from_str::<Route>("http://google.com")?,
|
||||||
Route::from_str("http://google.com")?
|
Route::from("http://google.com")
|
||||||
);
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -393,7 +378,7 @@ mod route {
|
||||||
fn deserialize_https_path() -> Result<()> {
|
fn deserialize_https_path() -> Result<()> {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
from_str::<Route>("https://google.com")?,
|
from_str::<Route>("https://google.com")?,
|
||||||
Route::from_str("https://google.com")?
|
Route::from("https://google.com")
|
||||||
);
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -401,7 +386,7 @@ mod route {
|
||||||
#[test]
|
#[test]
|
||||||
fn serialize() -> Result<()> {
|
fn serialize() -> Result<()> {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
&to_string(&Route::from_str("hello world")?)?,
|
&to_string(&Route::from("hello world"))?,
|
||||||
"---\nroute_type: External\npath: hello world\nhidden: false\ndescription: ~\nmin_args: ~\nmax_args: ~\n"
|
"---\nroute_type: External\npath: hello world\nhidden: false\ndescription: ~\nmin_args: ~\nmax_args: ~\n"
|
||||||
);
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -242,7 +242,6 @@ fn resolve_path(path: &Path, args: &str) -> Result<HopAction, BunBunError> {
|
||||||
mod resolve_hop {
|
mod resolve_hop {
|
||||||
use super::*;
|
use super::*;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
fn generate_route_result<'a>(
|
fn generate_route_result<'a>(
|
||||||
keyword: &'a Route,
|
keyword: &'a Route,
|
||||||
|
@ -277,13 +276,10 @@ mod resolve_hop {
|
||||||
#[test]
|
#[test]
|
||||||
fn only_default_routes_some_default_yields_default_hop() -> Result<()> {
|
fn only_default_routes_some_default_yields_default_hop() -> Result<()> {
|
||||||
let mut map: HashMap<String, Route> = HashMap::new();
|
let mut map: HashMap<String, Route> = HashMap::new();
|
||||||
map.insert("google".into(), Route::from_str("https://example.com")?);
|
map.insert("google".into(), Route::from("https://example.com"));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resolve_hop("hello world", &map, &Some(String::from("google"))),
|
resolve_hop("hello world", &map, &Some(String::from("google"))),
|
||||||
generate_route_result(
|
generate_route_result(&Route::from("https://example.com"), "hello world"),
|
||||||
&Route::from_str("https://example.com")?,
|
|
||||||
"hello world"
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -291,13 +287,10 @@ mod resolve_hop {
|
||||||
#[test]
|
#[test]
|
||||||
fn non_default_routes_some_default_yields_non_default_hop() -> Result<()> {
|
fn non_default_routes_some_default_yields_non_default_hop() -> Result<()> {
|
||||||
let mut map: HashMap<String, Route> = HashMap::new();
|
let mut map: HashMap<String, Route> = HashMap::new();
|
||||||
map.insert("google".into(), Route::from_str("https://example.com")?);
|
map.insert("google".into(), Route::from("https://example.com"));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resolve_hop("google hello world", &map, &Some(String::from("a"))),
|
resolve_hop("google hello world", &map, &Some(String::from("a"))),
|
||||||
generate_route_result(
|
generate_route_result(&Route::from("https://example.com"), "hello world"),
|
||||||
&Route::from_str("https://example.com")?,
|
|
||||||
"hello world"
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -305,13 +298,10 @@ mod resolve_hop {
|
||||||
#[test]
|
#[test]
|
||||||
fn non_default_routes_no_default_yields_non_default_hop() -> Result<()> {
|
fn non_default_routes_no_default_yields_non_default_hop() -> Result<()> {
|
||||||
let mut map: HashMap<String, Route> = HashMap::new();
|
let mut map: HashMap<String, Route> = HashMap::new();
|
||||||
map.insert("google".into(), Route::from_str("https://example.com")?);
|
map.insert("google".into(), Route::from("https://example.com"));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resolve_hop("google hello world", &map, &None),
|
resolve_hop("google hello world", &map, &None),
|
||||||
generate_route_result(
|
generate_route_result(&Route::from("https://example.com"), "hello world"),
|
||||||
&Route::from_str("https://example.com")?,
|
|
||||||
"hello world"
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -370,9 +360,12 @@ mod check_route {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod resolve_path {
|
mod resolve_path {
|
||||||
|
use crate::error::BunBunError;
|
||||||
|
|
||||||
use super::{resolve_path, HopAction};
|
use super::{resolve_path, HopAction};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use std::env::current_dir;
|
use std::env::current_dir;
|
||||||
|
use std::io::ErrorKind;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -397,14 +390,11 @@ mod resolve_path {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn no_permissions_returns_err() {
|
fn no_permissions_returns_err() {
|
||||||
assert!(
|
let result = match resolve_path(&Path::new("/root/some_exec"), "") {
|
||||||
// Trying to run a command without permission
|
Err(BunBunError::Io(e)) => e.kind() == ErrorKind::PermissionDenied,
|
||||||
format!(
|
_ => false,
|
||||||
"{}",
|
};
|
||||||
resolve_path(&Path::new("/root/some_exec"), "").unwrap_err()
|
assert!(result);
|
||||||
)
|
|
||||||
.contains("Permission denied")
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Reference in a new issue