aboutsummaryrefslogtreecommitdiffstats
path: root/cms-backd/src/query.rs
blob: 86518a5d332500248b7b1acced58113c2318ac06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// -*- coding: utf-8 -*-
//
// Simple CMS
//
// Copyright (C) 2011-2024 Michael Büsch <m@bues.ch>
//
// Licensed under the Apache License version 2.0
// or the MIT license, at your option.
// SPDX-License-Identifier: Apache-2.0 OR MIT

use crate::numparse::parse_i64;
use std::collections::HashMap;

#[derive(Clone, Debug)]
pub struct Query {
    items: HashMap<String, Vec<u8>>,
}

impl Query {
    pub fn new(items: HashMap<String, Vec<u8>>) -> Self {
        Self { items }
    }

    pub fn into_items(self) -> HashMap<String, Vec<u8>> {
        self.items
    }

    pub fn get(&self, name: &str) -> Option<Vec<u8>> {
        self.items.get(name).cloned()
    }

    pub fn get_str(&self, name: &str) -> Option<String> {
        if let Some(v) = self.get(name) {
            String::from_utf8(v).ok()
        } else {
            None
        }
    }

    pub fn get_int(&self, name: &str) -> Option<i64> {
        if let Some(v) = self.get_str(name) {
            parse_i64(&v).ok()
        } else {
            None
        }
    }
}

// vim: ts=4 sw=4 expandtab
bues.ch cgit interface