ryerraguntla commented on code in PR #4043:
URL: https://github.com/apache/iggy/pull/4043#discussion_r3967803549


##########
gateways/kafka/src/bridge/topic_map.rs:
##########
@@ -0,0 +1,203 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::collections::HashMap;
+use std::path::Path;
+
+use serde::Deserialize;
+
+use crate::bridge::error::BridgeError;
+
+/// Explicit Kafka-topic → Iggy stream/topic override. Absent entries fall 
back to
+/// [`TopicMapping::default_stream`] plus the Kafka topic name unchanged - see
+/// [`TopicMapping::resolve`].
+#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
+pub struct TopicOverride {
+    pub stream: String,
+    pub topic: String,
+}
+
+/// Kafka topic name → Iggy stream/topic mapping, loaded from TOML.
+///
+/// Default rule (no override): the Iggy stream is 
[`default_stream`](Self::default_stream) and
+/// the Iggy topic name is the Kafka topic name unchanged. A gateway that 
fronts a single Kafka
+/// "cluster" for one Iggy stream never needs an override entry at all.
+#[derive(Debug, Clone, Deserialize, Default, PartialEq, Eq)]
+pub struct TopicMapping {
+    pub default_stream: String,
+    #[serde(default)]
+    pub topics: HashMap<String, TopicOverride>,
+}
+
+impl TopicMapping {
+    /// Resolves a Kafka topic name to `(iggy_stream, iggy_topic)`.
+    #[must_use]
+    pub fn resolve(&self, kafka_topic: &str) -> (String, String) {
+        self.topics.get(kafka_topic).map_or_else(
+            || (self.default_stream.clone(), kafka_topic.to_string()),
+            |over| (over.stream.clone(), over.topic.clone()),
+        )
+    }
+
+    /// Parses a `TopicMapping` from a TOML document.
+    ///
+    /// # Errors
+    ///
+    /// Returns [`BridgeError::InvalidConfig`] if `raw` is not valid TOML for 
this shape, if
+    /// `default_stream` is empty, or if any `[topics.*]` override has an 
empty `stream`/`topic` -
+    /// in every case, an empty Iggy stream/topic name would otherwise pass 
config loading
+    /// cleanly and only fail much later, deep in 
`IggyBridge::ensure_stream`/`ensure_topic`, as
+    /// an opaque `Identifier::named("")` error with no link back to the 
config entry at fault.
+    pub fn from_toml_str(raw: &str) -> Result<Self, BridgeError> {
+        let mapping: Self = toml::from_str(raw)
+            .map_err(|e| BridgeError::InvalidConfig(format!("invalid topic 
mapping TOML: {e}")))?;
+        if mapping.default_stream.trim().is_empty() {

Review Comment:
   Trimming at validate-time and storing the raw string creates a name with 
leading/trailing space that later `.trim()` would silently re-point.
   Replaced trim-and-store with reject-on-whitespace, plus a 255-byte cap 
matching Identifier::named's own limit — shared validate_identifier_name helper 
used by both topic_map.rs and config.rs (the same bug
     existed in IGGY_KAFKA_IGGY_STREAM handling).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to