refactored unwrap calls to make it more stable

This commit is contained in:
euzu
2025-07-22 11:32:46 +02:00
parent 7ef30e1aae
commit 6c238c607f
44 changed files with 560 additions and 479 deletions
+16 -13
View File
@@ -531,21 +531,24 @@ impl MapperScript {
let first = pairs.next().unwrap();
let key = match first.as_rule() {
Rule::map_key => {
let map_key = first.into_inner().next().unwrap();
match map_key.as_rule() {
Rule::field => {
MapKey::FieldAccess(map_key.as_str().trim().to_string())
}
Rule::var_access => {
let text = map_key.as_str();
if text.contains('.') {
let splitted: Vec<&str> = text.splitn(2, '.').collect();
MapKey::VarAccess(splitted[0].trim().to_string(), splitted[1].trim().to_string())
} else {
MapKey::Identifier(text.trim().to_string())
if let Some(map_key) = first.into_inner().next() {
match map_key.as_rule() {
Rule::field => {
MapKey::FieldAccess(map_key.as_str().trim().to_string())
}
Rule::var_access => {
let text = map_key.as_str();
if text.contains('.') {
let splitted: Vec<&str> = text.splitn(2, '.').collect();
MapKey::VarAccess(splitted[0].trim().to_string(), splitted[1].trim().to_string())
} else {
MapKey::Identifier(text.trim().to_string())
}
}
_ => return create_tuliprox_error_result!(TuliproxErrorKind::Info, "Unexpected map case key: {:?}", map_key.as_rule()),
}
_ => return create_tuliprox_error_result!(TuliproxErrorKind::Info, "Unexpected map case key: {:?}", map_key.as_rule()),
} else {
return create_tuliprox_error_result!(TuliproxErrorKind::Info, "Missing map case key");
}
}
_ => return create_tuliprox_error_result!(TuliproxErrorKind::Info, "Unexpected map case key: {:?}", first.as_rule()),
+1 -2
View File
@@ -77,8 +77,7 @@ impl FromStr for ProxyType {
return Ok(Self::Reverse(None));
}
if s.starts_with(Self::REVERSE) {
let suffix = s.strip_prefix(Self::REVERSE).unwrap();
if let Some(suffix) = s.strip_prefix(Self::REVERSE) {
if let Ok(force_redirect) = ClusterFlags::try_from(suffix) {
if force_redirect.has_full_flags() {
return Ok(ProxyType::Reverse(None));
+2 -4
View File
@@ -1,7 +1,5 @@
use crate::error::{TuliproxError, TuliproxErrorKind};
use crate::model::{ConfigApiDto, HdHomeRunConfigDto, IpCheckConfigDto, LogConfigDto,
MessagingConfigDto, ProxyConfigDto, ReverseProxyConfigDto, ScheduleConfigDto,
VideoConfigDto, WebUiConfigDto};
use crate::model::{ConfigApiDto, HdHomeRunConfigDto, IpCheckConfigDto, LogConfigDto, MessagingConfigDto, ProxyConfigDto, ReverseProxyConfigDto, ScheduleConfigDto, VideoConfigDto, WebUiConfigDto, DEFAULT_VIDEO_EXTENSIONS};
use crate::utils::default_connect_timeout_secs;
pub const DEFAULT_USER_AGENT: &str = "VLC/3.0.16 LibVLC/3.0.16";
@@ -103,7 +101,7 @@ impl ConfigDto {
match &mut self.video {
None => {
self.video = Some(VideoConfigDto {
extensions: vec!["mkv".to_string(), "avi".to_string(), "mp4".to_string()],
extensions: DEFAULT_VIDEO_EXTENSIONS.iter().map(ToString::to_string).collect(),
download: None,
web_search: None,
});
+8 -8
View File
@@ -20,14 +20,14 @@ impl CacheConfigDto {
pub(crate) fn prepare(&mut self, working_dir: &str) -> Result<(), TuliproxError> {
if self.enabled {
let work_path = PathBuf::from(working_dir);
if self.dir.is_none() {
self.dir = Some(work_path.join("cache").to_string_lossy().to_string());
} else {
let mut cache_dir = self.dir.as_ref().unwrap().to_string();
if PathBuf::from(&cache_dir).is_relative() {
cache_dir = work_path.join(&cache_dir).clean().to_string_lossy().to_string();
}
self.dir = Some(cache_dir.to_string());
match self.dir.as_ref() {
None => self.dir = Some(work_path.join("cache").to_string_lossy().to_string()),
Some(work_dir) => {
let mut cache_dir = work_dir.to_string();
if PathBuf::from(&cache_dir).is_relative() {
cache_dir = work_path.join(&cache_dir).clean().to_string_lossy().to_string();
}
self.dir = Some(cache_dir.to_string()); }
}
if let Some(val) = self.size.as_ref() {
+3 -1
View File
@@ -4,6 +4,8 @@ use crate::create_tuliprox_error_result;
use crate::error::{TuliproxError, TuliproxErrorKind};
use crate::model::DEFAULT_USER_AGENT;
pub const DEFAULT_VIDEO_EXTENSIONS: [&str; 6] = ["mkv", "avi", "mp4", "mpeg", "divx", "mov"];
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Default, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct VideoDownloadConfigDto {
@@ -35,7 +37,7 @@ impl VideoConfigDto {
/// Will panic if default `RegEx` gets invalid
pub fn prepare(&mut self) -> Result<(), TuliproxError> {
if self.extensions.is_empty() {
self.extensions = ["mkv", "avi", "mp4", "mpeg", "divx", "mov"]
self.extensions = DEFAULT_VIDEO_EXTENSIONS
.iter()
.map(|&arg| arg.to_string())
.collect();
+4 -3
View File
@@ -71,9 +71,10 @@ where
self.dfs_find_cycles(neighbor, visited, recursion_stack, cycles);
} else if recursion_stack.contains(neighbor) {
// Cycle detected; collect the cycle path
let cycle_start_index = recursion_stack.iter().position(|n| n == neighbor).unwrap();
let cycle = recursion_stack[cycle_start_index..].to_vec();
cycles.push(cycle);
if let Some(cycle_start_index) = recursion_stack.iter().position(|n| n == neighbor) {
let cycle = recursion_stack[cycle_start_index..].to_vec();
cycles.push(cycle);
}
}
}
}