drasi_source_grpc/config.rs
1// Copyright 2025 The Drasi Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Configuration types for gRPC source.
16
17use serde::{Deserialize, Serialize};
18
19/// Default timeout in milliseconds
20fn default_timeout_ms() -> u64 {
21 5000
22}
23
24/// gRPC source configuration
25#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
26pub struct GrpcSourceConfig {
27 /// gRPC server host
28 #[serde(default = "default_host")]
29 pub host: String,
30
31 /// gRPC server port
32 #[serde(default = "default_port")]
33 pub port: u16,
34
35 /// Optional service endpoint
36 #[serde(default, skip_serializing_if = "Option::is_none")]
37 pub endpoint: Option<String>,
38
39 /// Request timeout in milliseconds
40 #[serde(default = "default_timeout_ms")]
41 pub timeout_ms: u64,
42}
43
44fn default_host() -> String {
45 "0.0.0.0".to_string()
46}
47
48fn default_port() -> u16 {
49 50051
50}
51
52impl Default for GrpcSourceConfig {
53 fn default() -> Self {
54 Self {
55 host: default_host(),
56 port: default_port(),
57 endpoint: None,
58 timeout_ms: default_timeout_ms(),
59 }
60 }
61}
62
63impl GrpcSourceConfig {
64 /// Validate the configuration and return an error if invalid.
65 ///
66 /// # Errors
67 ///
68 /// Returns an error if:
69 /// - Port is 0 (invalid port)
70 /// - Timeout is 0 (would cause immediate timeouts)
71 pub fn validate(&self) -> anyhow::Result<()> {
72 if self.port == 0 {
73 return Err(anyhow::anyhow!(
74 "Validation error: port cannot be 0. \
75 Please specify a valid port number (1-65535)"
76 ));
77 }
78
79 if self.timeout_ms == 0 {
80 return Err(anyhow::anyhow!(
81 "Validation error: timeout_ms cannot be 0. \
82 Please specify a positive timeout value in milliseconds"
83 ));
84 }
85
86 Ok(())
87 }
88}