zrx_graph/graph/macros.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//! Macros for graph creation.
27
28// ----------------------------------------------------------------------------
29// Macros
30// ----------------------------------------------------------------------------
31
32/// Creates a graph builder from the given pairs.
33///
34/// This macro creates a [`Builder`][] and adds nodes and edges based on the
35/// provided source-target pairs. It's primarily intended for use in tests and
36/// examples to quickly set up graphs, and is not optimized for performance.
37/// Additionally, the node type must implement [`Copy`].
38///
39/// [`Builder`]: crate::graph::Builder
40///
41/// # Examples
42///
43/// ```
44/// use zrx_graph::graph_builder;
45///
46/// // Create graph builder from pairs
47/// let builder = graph_builder! {
48/// "a" => "b", "a" => "c",
49/// "b" => "c",
50/// };
51/// ```
52#[macro_export]
53macro_rules! graph_builder {
54 ($($source:expr => $target:expr),+ $(,)?) => {{
55 let mut builder = $crate::Graph::builder();
56 let mut nodes = std::collections::BTreeMap::new();
57 $(
58 nodes.entry($source).or_insert_with(|| builder.add_node($source));
59 nodes.entry($target).or_insert_with(|| builder.add_node($target));
60 )*
61 $(
62 let _ = builder.add_edge(nodes[$source], nodes[$target], ());
63 )*
64 builder
65 }};
66}
67
68/// Creates a graph from the given pairs.
69///
70/// This macro creates a [`Graph`][] and adds nodes and edges based on the
71/// provided source-target pairs. It's primarily intended for use in tests and
72/// examples to quickly set up graphs, and is not optimized for performance.
73/// Additionally, the node type must implement [`Copy`].
74///
75/// In case you need a [`Builder`][], e.g. to inspect the graph before building
76/// or to print it to DOT format, use the [`graph_builder!`][] macro.
77///
78/// [`Builder`]: crate::graph::Builder
79/// [`Graph`]: crate::graph::Graph
80///
81/// # Examples
82///
83/// ```
84/// use zrx_graph::graph;
85///
86/// // Create graph from pairs
87/// let graph = graph! {
88/// "a" => "b", "a" => "c",
89/// "b" => "c",
90/// };
91/// ```
92#[macro_export]
93macro_rules! graph {
94 ($($source:expr => $target:expr),+ $(,)?) => {{
95 $crate::graph_builder!($($source => $target),+).build()
96 }};
97}