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 // We can just swallow the result here, since nodes are guaranteed to
62 // exist and point to valid indices, so errors can't possibly occur
63 $(
64 let _ = builder.add_edge(nodes[$source], nodes[$target]);
65 )*
66 builder
67 }};
68}
69
70/// Creates a graph from the given pairs.
71///
72/// This macro creates a [`Graph`][] and adds nodes and edges based on the
73/// provided source-target pairs. It's primarily intended for use in tests and
74/// examples to quickly set up graphs, and is not optimized for performance.
75/// Additionally, the node type must implement [`Copy`].
76///
77/// In case you need a [`Builder`][], e.g. to inspect the graph before building
78/// or to print it to DOT format, use the [`graph_builder!`][] macro.
79///
80/// [`Builder`]: crate::graph::Builder
81/// [`Graph`]: crate::graph::Graph
82///
83/// # Examples
84///
85/// ```
86/// use zrx_graph::graph;
87///
88/// // Create graph from pairs
89/// let graph = graph! {
90/// "a" => "b", "a" => "c",
91/// "b" => "c",
92/// };
93/// ```
94#[macro_export]
95macro_rules! graph {
96 ($($source:expr => $target:expr),+ $(,)?) => {{
97 $crate::graph_builder!($($source => $target),+).build()
98 }};
99}