1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//! # package
//!
//! The `package` module provides functionality for parsing and fetching information about Rust packages.
//! It includes tools for parsing dependencies from `Cargo.toml` files, fetching package information from
//! Crates.io, and building a dependency graph.
//!
//! # Quick Start
//!
//! Get started with the `package` module by following these simple steps:
//!
//! 1. Import the necessary types and functions into your code:
//!
//! ```rust
//! use depth::package::{Package, parse_dependencies, fetch_package_info};
//! use depth::dependency_graph::DependencyGraph;
//! use crates_io_api::SyncClient;
//! ```
//!
//! 2. Utilize the provided functionality to parse dependencies and fetch package information:
//!
//! ```rust
//! use depth::package::{Package, parse_dependencies, fetch_package_info};
//! use depth::dependency_graph::DependencyGraph;
//! use std::collections::HashMap;
//!
//! let cargo_toml_content = "..."; // Contents of your Cargo.toml file
//! // let dependencies = parse_dependencies(&cargo_toml_content)?;
//! let mut visited_packages: HashMap<String, Package> = HashMap::new();
//! let mut graph = DependencyGraph::new();
//!
//! // for dep in dependencies {
//! // fetch_package_info(&dep, &mut visited_packages, &mut graph, &client, 2)?;
//! // }
//! ```
//!
//! # Key Features
//!
//! The `package` module offers the following key features:
//!
//! - **Parsing Dependencies**: Parse dependencies from `Cargo.toml` files using the `parse_dependencies` function.
//! - **Fetching Package Information**: Fetch detailed package information from Crates.io using the `fetch_package_info` function.
//!
//! # Usage
//!
//! ## Parsing Dependencies
//!
//! Use the `parse_dependencies` function to parse dependencies from `Cargo.toml` content:
//!
//! ```rust
//! use depth::package::parse_dependencies;
//!
//! let cargo_toml_content = "..."; // Contents of your Cargo.toml file
//! // let dependencies = parse_dependencies(&cargo_toml_content)?;
//! ```
//!
//! ## Fetching Package Information
//!
//! Utilize the `fetch_package_info` function to fetch and build the dependency tree for a specific package:
//!
//! ```rust
//! use depth::package::{Package, fetch_package_info};
//! use depth::dependency_graph::DependencyGraph;
//! use std::collections::HashMap;
//! use crates_io_api::SyncClient;
//!
//! let mut visited_packages: HashMap<String, Package> = HashMap::new();
//! let mut graph = DependencyGraph::new();
//! // let client = SyncClient::new(
//! // "my-user-agent (my-contact@domain.com)",
//! // std::time::Duration::from_millis(1000),
//! // )?;
//!
//! // fetch_package_info(
//! // &("package_name".to_string(), "homepage_url".to_string()),
//! // &mut visited_packages,
//! // &mut graph,
//! // &client,
//! // 2,
//! // )?;
//! ```
//!
//! # Examples
//!
//! ```rust
//! use depth::package::{Package, parse_dependencies, fetch_package_info};
//! use depth::dependency_graph::DependencyGraph;
//! use crates_io_api::SyncClient;
//! use std::collections::HashMap;
//!
//! let cargo_toml_content = "..."; // Contents of your Cargo.toml file
//! // let dependencies = parse_dependencies(&cargo_toml_content).unwrap();
//!
//! let mut visited_packages: HashMap<String, Package> = HashMap::new();
//! let mut graph = DependencyGraph::new();
//! // let client = SyncClient::new(
//! // "my-user-agent (my-contact@domain.com)",
//! // std::time::Duration::from_millis(1000),
//! // ).unwrap();
//!
//! // for dep in dependencies {
//! // fetch_package_info(&(dep, "".to_string()), &mut visited_packages, &mut graph, &client, 2).unwrap();
//! // }
//! ```
use crateDependencyGraph;
use ;
use HashMap;
use Error;
use Value;
/// Represents a Rust package with its name, URL, dependencies, and internal status.
/// Parses the dependencies from the content of a Cargo.toml file.
///
/// # Arguments
///
/// * `cargo_toml_content` - The content of the Cargo.toml file as a string.
///
/// # Returns
///
/// A Result containing a Vec of dependency names or an error if parsing fails.
/// Fetches package information, including dependencies, from Crates.io and builds a dependency graph.
///
/// # Arguments
///
/// * `package_name` - A tuple containing the package name and its homepage URL.
/// * `visited_packages` - A mutable HashMap to store visited packages and prevent redundant fetching.
/// * `graph` - A mutable reference to a DependencyGraph where package information will be stored.
/// * `client` - A SyncClient instance for interacting with the Crates.io API.
/// * `depth` - The depth up to which dependencies should be fetched and added to the graph.
/// * `optional` - A boolean to scan optional dependencies only.
///
/// # Returns
///
/// A Result containing an optional Package or an error if the fetching process fails.
/// Lists dependencies for a given crate from the Crates.io API.
///
/// # Arguments
///
/// * `client` - A SyncClient instance for interacting with the Crates.io API.
/// * `crate_info` - A reference to the Crate information obtained from Crates.io.
/// * `optional` - A boolean to scan optional dependencies only.
///
/// # Returns
///
/// A Result containing a Vec of dependency tuples or an error if fetching fails.