]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/recursordist/settings/rust/src/helpers.rs
Merge pull request #11431 from jroessler-ox/docs-kskzskroll-update
[thirdparty/pdns.git] / pdns / recursordist / settings / rust / src / helpers.rs
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 use std::{error::Error, fmt};
24 use crate::ValidationError;
25
26 /* Helper code for validation */
27 impl Error for ValidationError {}
28 impl fmt::Display for ValidationError {
29 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
30 write!(f, "{}", self.msg)
31 }
32 }
33
34 // Generic helpers
35
36 // A helper to define a function returning a constant value and an equal function as a Rust path */
37 pub struct U64<const U: u64>;
38 impl<const U: u64> U64<U> {
39 pub const fn value() -> u64 {
40 U
41 }
42 pub fn is_equal(v: &u64) -> bool {
43 v == &U
44 }
45 }
46
47 // A helper to define constant value as a Rust path */
48 pub struct Bool<const U: bool>;
49 impl<const U: bool> Bool<U> {
50 pub const fn value() -> bool {
51 U
52 }
53 }
54
55 // A helper used to decide if a bool value should be skipped
56 pub fn if_true(v: &bool) -> bool {
57 *v
58 }
59
60 /* Helper to decide if a value has a default value, as defined by Default trait */
61 pub fn is_default<T: Default + PartialEq>(t: &T) -> bool {
62 t == &T::default()
63 }
64
65 pub const OVERRIDE_TAG: &str = "!override";
66
67 pub fn is_overriding(m: &serde_yaml::Mapping, key: &str) -> bool{
68 if let Some(serde_yaml::Value::Tagged(vvv)) = m.get(key) {
69 return vvv.tag == OVERRIDE_TAG;
70 }
71 false
72 }
73