1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use crate::nameserver::{NameServerConfig, NameServerConfigGroup};
use crate::resolver::ResolverGroup;
use crate::{Error, Result};
use std::path::Path;
use tokio::fs::File;
use tokio::io::AsyncBufReadExt;
use tokio::io::BufReader;
impl NameServerConfigGroup {
pub async fn from_file<P: AsRef<Path>>(resolvers: &ResolverGroup, path: P) -> Result<NameServerConfigGroup> {
NameServerConfigGroup::do_from_file(resolvers, path, true).await
}
pub async fn from_file_abort_on_error<P: AsRef<Path>>(
resolvers: &ResolverGroup,
path: P,
abort_on_error: bool,
) -> Result<NameServerConfigGroup> {
NameServerConfigGroup::do_from_file(resolvers, path, abort_on_error).await
}
async fn do_from_file<P: AsRef<Path>>(
resolvers: &ResolverGroup,
path: P,
abort_on_error: bool,
) -> Result<NameServerConfigGroup> {
let file = File::open(path).await?;
let mut buf_reader = BufReader::new(file);
let mut configs = Vec::new();
let mut buffer = String::new();
loop {
buffer.clear();
let len = buf_reader.read_line(&mut buffer).await?;
if len == 0 {
break;
}
if buffer.starts_with("//") {
continue;
}
let config = NameServerConfig::from_str_with_resolution(&resolvers, &buffer)
.await
.map_err(|e| Error::ParserError {
what: buffer.clone(),
to: "NameServerConfig",
why: e.to_string(),
});
match config {
Ok(config) => configs.push(config),
Err(e) if abort_on_error => return Err(e),
_ => break,
}
}
Ok(NameServerConfigGroup::new(configs))
}
}