cdns_rs/sync/
cfg_resolv_parser.rs

1/*-
2 * cdns-rs - a simple sync/async DNS query library
3 * 
4 * Copyright (C) 2020  Aleksandr Morozov
5 * 
6 * Copyright 2025 Aleksandr Morozov
7 * 
8 * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by
9 * the European Commission - subsequent versions of the EUPL (the "Licence").
10 * 
11 * You may not use this work except in compliance with the Licence.
12 * 
13 * You may obtain a copy of the Licence at:
14 * 
15 *    https://joinup.ec.europa.eu/software/page/eupl
16 * 
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the Licence is distributed on an "AS IS" basis, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
20 * Licence for the specific language governing permissions and limitations
21 * under the Licence.
22 */
23
24/// This file contains the config file parsers.
25
26
27use std::path::Path;
28
29use crate::cfg_resolv_parser::{ResolveConfig, OptionFlags};
30use crate::error::*;
31use crate::common::{RESOLV_CFG_PATH, RESOLV_CFG_PATH_P};
32
33use super::caches::CacheOperations;
34use super::cfg_parsers::{ConfigParser, read_file};
35
36
37
38impl CacheOperations for ResolveConfig
39{
40    fn is_reload_allowed(&self) -> bool 
41    {
42        return !self.option_flags.contains(OptionFlags::OPT_NO_RELOAD);
43    }
44}
45
46
47impl ConfigParser<ResolveConfig> for ResolveConfig
48{
49    fn parse_config() -> CDnsResult<Self>
50    {
51        let file_content = read_file(RESOLV_CFG_PATH)?;
52
53        let ret = Self::parser_resolv_internal(file_content.as_str());
54
55        return ret;
56    }
57
58    fn get_file_path() -> &'static Path
59    {
60        return &RESOLV_CFG_PATH_P;
61    }
62
63    fn is_default(&self) -> bool
64    {
65        return self.nameservers.is_empty();
66    }
67}
68
69impl ResolveConfig
70{
71    /// This function should be used when the program which uses this library
72    /// requires to override the systems /etc/resolv.conf
73    pub 
74    fn custom_config(resolv_cfg: &str) -> CDnsResult<Self>
75    {
76        let ret = Self::parser_resolv_internal(resolv_cfg);
77
78        return ret;
79    }
80}
81