Skip to main content

zrx_graph/graph/operator/
map.rs

1// Copyright (c) 2025-2026 Zensical and contributors
2
3// SPDX-License-Identifier: MIT
4// All contributions are certified under the DCO
5
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to
8// deal in the Software without restriction, including without limitation the
9// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10// sell copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22// IN THE SOFTWARE.
23
24// ----------------------------------------------------------------------------
25
26//! Map operator.
27
28use crate::graph::Graph;
29
30// ----------------------------------------------------------------------------
31// Implementations
32// ----------------------------------------------------------------------------
33
34impl<T> Graph<T> {
35    /// Maps the nodes to a different type.
36    ///
37    /// # Examples
38    ///
39    /// ```
40    /// # use std::error::Error;
41    /// # fn main() -> Result<(), Box<dyn Error>> {
42    /// use zrx_graph::Graph;
43    ///
44    /// // Create graph builder and add nodes
45    /// let mut builder = Graph::builder();
46    /// let a = builder.add_node("a");
47    /// let b = builder.add_node("b");
48    /// let c = builder.add_node("c");
49    ///
50    /// // Create edges between nodes
51    /// builder.add_edge(a, b)?;
52    /// builder.add_edge(b, c)?;
53    ///
54    /// // Create graph from builder and map data
55    /// let graph = builder.build();
56    /// graph.map(str::to_uppercase);
57    /// # Ok(())
58    /// # }
59    /// ```
60    #[inline]
61    pub fn map<F, U>(self, f: F) -> Graph<U>
62    where
63        F: FnMut(T) -> U,
64    {
65        Graph {
66            data: self.data.into_iter().map(f).collect(),
67            topology: self.topology,
68        }
69    }
70}