windmark/
utilities.rs

1// This file is part of Windmark <https://github.com/gemrest/windmark>.
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, version 3.
6//
7// This program is distributed in the hope that it will be useful, but
8// WITHOUT ANY WARRANTY; without even the implied warranty of
9// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10// General Public License for more details.
11//
12// You should have received a copy of the GNU General Public License
13// along with this program. If not, see <http://www.gnu.org/licenses/>.
14//
15// Copyright (C) 2022-2023 Fuwn <contact@fuwn.me>
16// SPDX-License-Identifier: GPL-3.0-only
17
18//! Utilities to make cumbersome tasks simpler
19
20use std::collections::HashMap;
21
22/// Extract the queries from a URL into a `HashMap`.
23#[must_use]
24pub fn queries_from_url(url: &url::Url) -> HashMap<String, String> {
25  let mut queries = HashMap::new();
26
27  for (key, value) in url.query_pairs() {
28    queries.insert(key.to_string(), value.to_string());
29  }
30
31  queries
32}
33
34#[must_use]
35pub fn params_to_hashmap(
36  params: &matchit::Params<'_, '_>,
37) -> HashMap<String, String> {
38  params
39    .iter()
40    .map(|(k, v)| (k.to_string(), v.to_string()))
41    .collect()
42}