rskit_discovery/
instance.rs1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct ServiceInstance {
8 pub id: String,
10 pub name: String,
12 pub address: String,
14 pub port: u16,
16 pub healthy: bool,
18 #[serde(default = "default_weight")]
20 pub weight: u32,
21 pub tags: Vec<String>,
23 pub metadata: HashMap<String, String>,
25}
26
27fn default_weight() -> u32 {
28 1
29}
30
31impl ServiceInstance {
32 pub fn endpoint(&self) -> String {
34 format!("{}:{}", self.address, self.port)
35 }
36}
37
38#[cfg(test)]
39mod tests {
40 use super::*;
41
42 #[test]
43 fn missing_weight_deserializes_to_default() {
44 let instance: ServiceInstance = serde_json::from_str(
45 r#"{
46 "id":"a",
47 "name":"svc",
48 "address":"127.0.0.1",
49 "port":8080,
50 "healthy":true,
51 "tags":[],
52 "metadata":{}
53 }"#,
54 )
55 .unwrap();
56
57 assert_eq!(instance.weight, 1);
58 }
59}