diff options
| author | Fuwn <[email protected]> | 2022-06-01 01:44:21 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2022-06-01 01:44:21 +0000 |
| commit | 607430c0aa3d99a012fbdb0d2a8bf32eb32122a7 (patch) | |
| tree | 85fa26f8f80ecc7fedf6ab57eec8ceef60014c5c /src/url.rs | |
| parent | feat(main.rs): replace gemini_to_html with germ!!! (diff) | |
| download | september-607430c0aa3d99a012fbdb0d2a8bf32eb32122a7.tar.xz september-607430c0aa3d99a012fbdb0d2a8bf32eb32122a7.zip | |
refactor(src): move parts to seperate files
Diffstat (limited to 'src/url.rs')
| -rw-r--r-- | src/url.rs | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/url.rs b/src/url.rs new file mode 100644 index 0000000..1e29e85 --- /dev/null +++ b/src/url.rs @@ -0,0 +1,68 @@ +// This file is part of September <https://github.com/gemrest/september>. +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 3. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +// +// Copyright (C) 2022-2022 Fuwn <[email protected]> +// SPDX-License-Identifier: GPL-3.0-only + +use gmi::url::Url; + +pub fn make( + path: &str, + fallback: bool, + is_proxy: &mut bool, +) -> Result<Url, gmi::url::UrlParseError> { + Ok( + match Url::try_from(&*if path.starts_with("/proxy") { + *is_proxy = true; + + format!( + "gemini://{}{}", + path.replace("/proxy/", ""), + if fallback { "/" } else { "" } + ) + } else if path.starts_with("/x") { + *is_proxy = true; + + format!( + "gemini://{}{}", + path.replace("/x/", ""), + if fallback { "/" } else { "" } + ) + } else { + // Try to set `ROOT` as `ROOT` environment variable, or use + // `"gemini://fuwn.me"` as default. + format!( + "{}{}{}", + { + if let Ok(root) = std::env::var("ROOT") { + root + } else { + warn!( + "could not use ROOT from environment variables, proceeding with \ + default root: gemini://fuwn.me" + ); + + "gemini://fuwn.me".to_string() + } + }, + path, + if fallback { "/" } else { "" } + ) + }) { + Ok(url) => url, + Err(e) => return Err(e), + }, + ) +} |