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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;
use std::result::Result;
use serde;
use serde_yaml;
use opcua_types::service_types::{ApplicationDescription, ApplicationType};
use opcua_types::{LocalizedText, UAString};
pub trait Config: serde::Serialize {
fn save(&self, path: &Path) -> Result<(), ()> {
if self.is_valid() {
let s = serde_yaml::to_string(&self).unwrap();
if let Ok(mut f) = File::create(path) {
let result = f.write_all(s.as_bytes());
if result.is_ok() {
return Ok(());
} else {
error!("Could not save config - error = {:?}", result.unwrap_err())
}
} else {
error!("Cannot create the path to save the config");
}
} else {
error!("Config isn't valid and won't be saved");
}
Err(())
}
fn load<A>(path: &Path) -> Result<A, ()>
where
for<'de> A: Config + serde::Deserialize<'de>,
{
if let Ok(mut f) = File::open(path) {
let mut s = String::new();
if f.read_to_string(&mut s).is_ok() {
serde_yaml::from_str(&s).map_err(|err| {
error!(
"Cannot deserialize configuration from {}, error reason: {}",
path.to_string_lossy(),
err.to_string()
);
})
} else {
error!(
"Cannot read configuration file {} to string",
path.to_string_lossy()
);
Err(())
}
} else {
error!("Cannot open configuration file {}", path.to_string_lossy());
Err(())
}
}
fn is_valid(&self) -> bool;
fn application_name(&self) -> UAString;
fn application_uri(&self) -> UAString;
fn product_uri(&self) -> UAString;
fn application_type(&self) -> ApplicationType;
fn discovery_urls(&self) -> Option<Vec<UAString>> {
None
}
fn application_description(&self) -> ApplicationDescription {
ApplicationDescription {
application_uri: self.application_uri(),
application_name: LocalizedText::new("", self.application_name().as_ref()),
application_type: self.application_type(),
product_uri: self.product_uri(),
gateway_server_uri: UAString::null(),
discovery_profile_uri: UAString::null(),
discovery_urls: self.discovery_urls(),
}
}
}