taler-rust

GNU Taler code in Rust. Largely core banking integrations.
Log | Files | Refs | Submodules | README | LICENSE

headers.rs (2481B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2026 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU Affero General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11   A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
     12 
     13   You should have received a copy of the GNU Affero General Public License along with
     14   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 
     17 use hyper::{HeaderMap, header::HeaderName};
     18 use taler_common::encoding::base64;
     19 
     20 #[derive(Debug, thiserror::Error)]
     21 pub enum HeaderError {
     22     #[error("Missing header {0}")]
     23     Missing(HeaderName),
     24     #[error("Malformed header {0} expected string got binary")]
     25     NotStr(HeaderName),
     26     #[error("Malformed header {0} expected {1} got '{2}'")]
     27     Malformed(HeaderName, &'static str, Box<str>),
     28 }
     29 
     30 pub trait HeaderParser<E> {
     31     fn parse<T>(
     32         &self,
     33         name: impl Into<HeaderName>,
     34         kind: &'static str,
     35         transform: impl FnOnce(&str) -> Result<T, ()>,
     36     ) -> Result<T, E>;
     37     fn str_header(&self, name: impl Into<HeaderName>) -> Result<String, E> {
     38         self.parse(name, "string", |s| Ok(s.to_owned()))
     39     }
     40     fn int_header(&self, name: impl Into<HeaderName>) -> Result<u64, E> {
     41         self.parse(name, "integer", |s| s.parse().map_err(|_| ()))
     42     }
     43     fn bool_header(&self, name: impl Into<HeaderName>) -> Result<bool, E> {
     44         self.parse(name, "boolean", |s| s.parse().map_err(|_| ()))
     45     }
     46     fn b64_header(&self, name: impl Into<HeaderName>) -> Result<Vec<u8>, E> {
     47         self.parse(name, "b64", |s| base64::decode(s).map_err(|_| ()))
     48     }
     49 }
     50 
     51 impl HeaderParser<HeaderError> for HeaderMap {
     52     fn parse<T>(
     53         &self,
     54         name: impl Into<HeaderName>,
     55         kind: &'static str,
     56         transform: impl FnOnce(&str) -> Result<T, ()>,
     57     ) -> Result<T, HeaderError> {
     58         let name = name.into();
     59         let Some(value) = self.get(&name) else {
     60             return Err(HeaderError::Missing(name));
     61         };
     62         let Ok(str) = value.to_str() else {
     63             return Err(HeaderError::NotStr(name));
     64         };
     65         transform(str).map_err(|_| HeaderError::Malformed(name, kind, str.into()))
     66     }
     67 }