Skip to main content

risingwave_connector/connector_common/
pubsub_common.rs

1// Copyright 2026 RisingWave Labs
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
15const DEFAULT_EMULATOR_PROJECT_ID: &str = "local-project";
16
17pub(crate) fn resolve_pubsub_project_id(
18    configured: Option<&str>,
19    detected: Option<&str>,
20    is_emulator: bool,
21) -> Option<String> {
22    configured
23        .or(detected)
24        .or(is_emulator.then_some(DEFAULT_EMULATOR_PROJECT_ID))
25        .map(ToOwned::to_owned)
26}
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31
32    #[test]
33    fn test_resolve_pubsub_project_id() {
34        assert_eq!(
35            resolve_pubsub_project_id(Some("configured"), Some("detected"), true).as_deref(),
36            Some("configured")
37        );
38        assert_eq!(
39            resolve_pubsub_project_id(None, Some("detected"), true).as_deref(),
40            Some("detected")
41        );
42        assert_eq!(
43            resolve_pubsub_project_id(None, None, true).as_deref(),
44            Some(DEFAULT_EMULATOR_PROJECT_ID)
45        );
46        assert_eq!(resolve_pubsub_project_id(None, None, false), None);
47    }
48}