brooks_lib/utils.rs
1// brooks, Copyright 2026, Will Hawkins
2//
3// This file is part of brooks.
4
5// This file is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17
18use std::str::FromStr;
19
20pub fn strip_quotes(str: &str) -> String {
21 let result = str.strip_prefix("\"").unwrap_or(str);
22 result.strip_suffix("\"").unwrap_or(result).into()
23}
24
25/// Strip any /s that surround `str`.
26///
27/// Useful for removing the /s that are required in a MEL regular expression
28/// that are not required in a Rust regular expression.
29pub fn strip_slashes(str: &str) -> String {
30 let result = str.strip_prefix("/").unwrap_or(str);
31 result.strip_suffix("/").unwrap_or(result).into()
32}
33
34pub fn sanitize_error_message(str: &str) -> String {
35 str.replace("\n", "")
36}
37
38pub fn try_to_url<A: ToString>(from: A) -> Result<url::Url, url::ParseError> {
39 url::Url::from_str(&from.to_string())
40}
41
42pub fn try_to_uri<A: ToString>(from: A) -> Result<http::Uri, http::uri::InvalidUri> {
43 http::Uri::from_str(&from.to_string())
44}