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
use crate::host_id::HostId;
use serde::{Deserialize, Serialize};
use std::fmt::Formatter;
use url::Url;
/// HostInfo corresponds to an aggregation of nodes, running within a same program
/// instance. Think of them as "physical", macro entities, as opposed to nodes
/// which are much more virtual and small. A given host will typically have
/// several nodes to power, all of them going through the same addresses (http etc.).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct HostInfo {
/// ID used by the host. This happens to be the public key as well,
/// when it comes to signing messages.
pub id: HostId,
/// Name of the host, a free-form test, by default the hostname.
pub name: String,
/// Description of the host, a free-form text.
pub description: String,
/// URLs of the host, how to connect to it.
pub urls: Vec<Url>,
/// Signature of the host info.
pub sig: Option<Vec<u8>>,
}
impl HostInfo {
pub fn content_to_verify(&self) -> Vec<u8> {
Vec::from(format!("{}", &self))
}
}
impl std::fmt::Display for HostInfo {
/// Pretty-print a host.
///
/// # Examples
/// ```
/// use confitul::HostInfo;
/// use confitul::HostId;
/// use ed25519_dalek::Keypair;
/// use rand07::rngs::OsRng;
/// use url::Url;
/// use std::convert::TryFrom;
///
/// let mut csprng = OsRng {};
/// let keypair: Keypair = Keypair::generate(&mut csprng);
///
/// let h = HostInfo {
/// id: HostId::new(&keypair),
/// name: String::from("computer"),
/// description: String::from("test"),
/// urls: vec![Url::parse("http://localhost").unwrap()],
/// sig: None,
/// };
/// assert_eq!("{\"id\":\"0x", &format!("{}", h)[0..9]);
/// assert_eq!("\",\"name\":\"computer\",\"description\":\"test\",\"urls\":[\"http://localhost/\"]}", &format!("{}", h)[18..88]);
/// ```
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{{\"id\":\"{}\",\"name\":\"{}\",\"description\":\"{}\",\"urls\":[",
self.id, self.name, self.description,
)?;
let mut require_comma = false;
for url in self.urls.iter() {
if require_comma {
write!(f, ",")?;
require_comma = true;
}
write!(f, "\"{}\"", url.as_str())?;
}
write!(f, "]}}")
}
}