query.rs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // -*- coding: utf-8 -*-
  2. //
  3. // Simple CMS
  4. //
  5. // Copyright (C) 2011-2024 Michael Büsch <m@bues.ch>
  6. //
  7. // Licensed under the Apache License version 2.0
  8. // or the MIT license, at your option.
  9. // SPDX-License-Identifier: Apache-2.0 OR MIT
  10. use crate::numparse::parse_i64;
  11. use std::collections::HashMap;
  12. #[derive(Clone, Debug)]
  13. pub struct Query {
  14. items: HashMap<String, Vec<u8>>,
  15. }
  16. impl Query {
  17. pub fn new(items: HashMap<String, Vec<u8>>) -> Self {
  18. Self { items }
  19. }
  20. pub fn into_items(self) -> HashMap<String, Vec<u8>> {
  21. self.items
  22. }
  23. pub fn get(&self, name: &str) -> Option<Vec<u8>> {
  24. self.items.get(name).cloned()
  25. }
  26. pub fn get_str(&self, name: &str) -> Option<String> {
  27. if let Some(v) = self.get(name) {
  28. String::from_utf8(v).ok()
  29. } else {
  30. None
  31. }
  32. }
  33. pub fn get_int(&self, name: &str) -> Option<i64> {
  34. if let Some(v) = self.get_str(name) {
  35. parse_i64(&v).ok()
  36. } else {
  37. None
  38. }
  39. }
  40. }
  41. // vim: ts=4 sw=4 expandtab