cdns_rs/a_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 (C) 2025 Aleksandr Morozov
7 * 
8 * The syslog-rs crate can be redistributed and/or modified
9 * under the terms of either of the following licenses:
10 *
11 *   1. the Mozilla Public License Version 2.0 (the “MPL”) OR
12 *                     
13 *   2. EUROPEAN UNION PUBLIC LICENCE v. 1.2 EUPL © the European Union 2007, 2016
14 */
15
16/// This file contains the config file parsers.
17
18
19use std::path::Path;
20
21use async_trait::async_trait;
22
23
24use crate::a_sync::interface::UnifiedFs;
25use crate::cfg_resolv_parser::{ResolveConfig, OptionFlags};
26use crate::common::{RESOLV_CFG_PATH, RESOLV_CFG_PATH_P};
27use crate::error::*;
28
29use super::caches::CacheOperations;
30use super::cfg_parsers::{ConfigParser, read_file};
31
32
33
34impl CacheOperations for ResolveConfig
35{
36    fn is_reload_allowed(&self) -> bool 
37    {
38        return !self.option_flags.contains(OptionFlags::OPT_NO_RELOAD);
39    }
40}
41
42
43impl ConfigParser<ResolveConfig> for ResolveConfig
44{
45    async 
46    fn parse_config<UFS: UnifiedFs>() -> CDnsResult<Self>
47    {
48        let file_content = read_file::<UFS>(RESOLV_CFG_PATH).await?;
49
50        let ret = Self::parser_resolv_internal(file_content.as_str());
51
52        return ret;
53    }
54
55    fn get_file_path() -> &'static Path
56    {
57        return &RESOLV_CFG_PATH_P;
58    }
59
60    fn is_default(&self) -> bool
61    {
62        return self.nameservers.is_empty();
63    }
64}
65
66impl ResolveConfig
67{
68    /// This function should be used when the program which uses this library
69    /// requires to override the systems /etc/resolv.conf
70    pub async 
71    fn async_custom_config(resolv_cfg: &str) -> CDnsResult<Self>
72    {        
73        return Self::parser_resolv_internal(resolv_cfg);
74    }
75}
76
77