• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Qiskit / rustworkx / 15950552464

29 Jun 2025 02:20AM UTC coverage: 94.619% (-0.6%) from 95.201%
15950552464

Pull #1472

github

web-flow
Merge 253df1f85 into 8cf854cc4
Pull Request #1472: Fix Clippy Lints for Rust 1.88

39 of 50 new or added lines in 16 files covered. (78.0%)

72 existing lines in 19 files now uncovered.

17760 of 18770 relevant lines covered (94.62%)

1551273.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.83
/src/json/node_link_data.rs
1
// Licensed under the Apache License, Version 2.0 (the "License"); you may
2
// not use this file except in compliance with the License. You may obtain
3
// a copy of the License at
4
//
5
//     http://www.apache.org/licenses/LICENSE-2.0
6
//
7
// Unless required by applicable law or agreed to in writing, software
8
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10
// License for the specific language governing permissions and limitations
11
// under the License.
12

13
use std::collections::BTreeMap;
14
use std::fs::File;
15

16
use hashbrown::HashMap;
17

18
use serde::{Deserialize, Serialize};
19

20
use pyo3::prelude::*;
21
use pyo3::IntoPyObjectExt;
22
use pyo3::Python;
23

24
use petgraph::visit::EdgeRef;
25
use petgraph::visit::IntoEdgeReferences;
26
use petgraph::EdgeType;
27

28
use crate::JSONSerializationError;
29
use crate::NodeIndex;
30
use crate::StablePyGraph;
31

32
#[derive(Serialize)]
33
pub struct Graph {
34
    pub directed: bool,
35
    pub multigraph: bool,
36
    pub attrs: Option<BTreeMap<String, String>>,
37
    pub nodes: Vec<Node>,
38
    pub links: Vec<Link>,
39
}
40

41
#[derive(Deserialize)]
42
pub struct GraphInput {
43
    pub directed: bool,
44
    pub multigraph: bool,
45
    pub attrs: Option<BTreeMap<String, String>>,
46
    pub nodes: Vec<NodeInput>,
47
    pub links: Vec<LinkInput>,
48
}
49

50
#[derive(Serialize)]
51
pub struct Node {
52
    id: usize,
53
    data: Option<BTreeMap<String, String>>,
54
}
55

56
#[derive(Deserialize)]
57
pub struct NodeInput {
58
    id: Option<usize>,
59
    data: Option<BTreeMap<String, String>>,
60
}
61

62
#[derive(Deserialize)]
63
pub struct LinkInput {
64
    source: usize,
65
    target: usize,
66
    #[allow(dead_code)]
67
    id: Option<usize>,
68
    data: Option<BTreeMap<String, String>>,
69
}
70

71
#[derive(Serialize)]
72
pub struct Link {
73
    source: usize,
74
    target: usize,
75
    id: usize,
76
    data: Option<BTreeMap<String, String>>,
77
}
78

79
#[allow(clippy::too_many_arguments)]
80
pub fn parse_node_link_data<Ty: EdgeType>(
16✔
81
    py: &Python,
16✔
82
    graph: GraphInput,
16✔
83
    out_graph: &mut StablePyGraph<Ty>,
16✔
84
    node_attrs: Option<PyObject>,
16✔
85
    edge_attrs: Option<PyObject>,
16✔
86
) -> PyResult<()> {
16✔
87
    let mut id_mapping: HashMap<usize, NodeIndex> = HashMap::with_capacity(graph.nodes.len());
16✔
88
    for node in graph.nodes {
3,832✔
89
        let payload = match node.data {
3,816✔
90
            Some(data) => match node_attrs {
3,544✔
91
                Some(ref callback) => callback.call1(*py, (data,))?,
×
92
                None => data.into_py_any(*py)?,
3,544✔
93
            },
94
            None => py.None(),
272✔
95
        };
96
        let id = out_graph.add_node(payload);
3,816✔
97
        match node.id {
3,816✔
98
            Some(input_id) => id_mapping.insert(input_id, id),
3,816✔
99
            None => id_mapping.insert(id.index(), id),
×
100
        };
101
    }
102
    for edge in graph.links {
4,464✔
103
        let data = match edge.data {
4,448✔
104
            Some(data) => match edge_attrs {
4,184✔
105
                Some(ref callback) => callback.call1(*py, (data,))?,
×
106
                None => data.into_py_any(*py)?,
4,184✔
107
            },
108
            None => py.None(),
264✔
109
        };
110
        out_graph.add_edge(id_mapping[&edge.source], id_mapping[&edge.target], data);
4,448✔
111
    }
112
    Ok(())
16✔
113
}
16✔
114

115
#[allow(clippy::too_many_arguments)]
116
pub fn node_link_data<Ty: EdgeType>(
48✔
117
    py: Python,
48✔
118
    graph: &StablePyGraph<Ty>,
48✔
119
    multigraph: bool,
48✔
120
    attrs: &PyObject,
48✔
121
    path: Option<String>,
48✔
122
    graph_attrs: Option<PyObject>,
48✔
123
    node_attrs: Option<PyObject>,
48✔
124
    edge_attrs: Option<PyObject>,
48✔
125
) -> PyResult<Option<String>> {
48✔
126
    let attr_callable = |attrs: &PyObject, obj: &PyObject| -> PyResult<BTreeMap<String, String>> {
7,788✔
127
        let res = attrs.call1(py, (obj,))?;
7,788✔
128
        res.extract(py)
7,788✔
129
    };
7,788✔
130
    let mut nodes: Vec<Node> = Vec::with_capacity(graph.node_count());
48✔
131
    for n in graph.node_indices() {
3,844✔
132
        let data = match node_attrs {
3,844✔
133
            Some(ref callback) => Some(attr_callable(callback, &graph[n])?),
3,568✔
134
            None => None,
276✔
135
        };
136
        nodes.push(Node {
3,844✔
137
            id: n.index(),
3,844✔
138
            data,
3,844✔
139
        });
3,844✔
140
    }
141
    let mut links: Vec<Link> = Vec::with_capacity(graph.edge_count());
48✔
142
    for e in graph.edge_references() {
4,464✔
143
        let data = match edge_attrs {
4,464✔
144
            Some(ref callback) => Some(attr_callable(callback, e.weight())?),
4,200✔
145
            None => None,
264✔
146
        };
147
        links.push(Link {
4,464✔
148
            source: e.source().index(),
4,464✔
149
            target: e.target().index(),
4,464✔
150
            id: e.id().index(),
4,464✔
151
            data,
4,464✔
152
        });
4,464✔
153
    }
154

155
    let graph_attrs = match graph_attrs {
48✔
156
        Some(ref callback) => Some(attr_callable(callback, attrs)?),
20✔
157
        None => None,
28✔
158
    };
159

160
    let output_struct = Graph {
44✔
161
        directed: graph.is_directed(),
44✔
162
        multigraph,
44✔
163
        attrs: graph_attrs,
44✔
164
        nodes,
44✔
165
        links,
44✔
166
    };
44✔
167
    match path {
44✔
168
        None => match serde_json::to_string(&output_struct) {
28✔
169
            Ok(v) => Ok(Some(v)),
28✔
NEW
170
            Err(e) => Err(JSONSerializationError::new_err(format!("JSON Error: {e}"))),
×
171
        },
172
        Some(filename) => {
16✔
173
            let file = File::create(filename)?;
16✔
174
            match serde_json::to_writer(file, &output_struct) {
12✔
175
                Ok(_) => Ok(None),
12✔
NEW
176
                Err(e) => Err(JSONSerializationError::new_err(format!("JSON Error: {e}"))),
×
177
            }
178
        }
179
    }
180
}
48✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc