bgpkit_commons/lib.rs
1//! # Overview
2//!
3//! `bgpkit-commons` is a library for common BGP-related data and functions with a lazy-loading
4//! architecture. Each module can be independently enabled via feature flags, allowing for minimal builds.
5//!
6//! ## Quick Start
7//!
8//! Add `bgpkit-commons` to your `Cargo.toml`:
9//!
10//! ```toml
11//! [dependencies]
12//! bgpkit-commons = "0.10"
13//! ```
14//!
15//! All modules follow the same pattern: create a [`BgpkitCommons`] instance, call a `load_xxx()`
16//! method to fetch data, then use `xxx_yyy()` methods to access it.
17//!
18//! ```rust
19//! # #[cfg(feature = "bogons")]
20//! # fn main() {
21//! use bgpkit_commons::BgpkitCommons;
22//!
23//! let mut commons = BgpkitCommons::new();
24//! commons.load_bogons().unwrap();
25//!
26//! if let Ok(is_bogon) = commons.bogons_match("23456") {
27//! println!("ASN 23456 is a bogon: {}", is_bogon);
28//! }
29//! # }
30//! # #[cfg(not(feature = "bogons"))]
31//! # fn main() {}
32//! ```
33//!
34//! ## Modules
35//!
36//! ### [`asinfo`] — Autonomous System Information
37//!
38//! Feature: `asinfo` | Sources: RIPE NCC, CAIDA as2org, APNIC population, IIJ IHR hegemony, PeeringDB
39//!
40//! - Load: `load_asinfo(as2org, population, hegemony, peeringdb)`, `load_asinfo_cached()`, `load_asinfo_with(builder)`
41//! - Access: `asinfo_get(asn)`, `asinfo_all()`, `asinfo_are_siblings(asn1, asn2)`
42//! - AS name resolution, country mapping, organization data, population statistics, hegemony scores
43//!
44//! ### [`as2rel`] — AS Relationship Data
45//!
46//! Feature: `as2rel` | Source: BGPKIT AS relationship inference
47//!
48//! - Load: `load_as2rel()`
49//! - Access: `as2rel_lookup(asn1, asn2)`
50//! - Provider-customer, peer-to-peer, and sibling relationships between ASes
51//!
52//! ### [`bogons`] — Bogon Detection
53//!
54//! Feature: `bogons` | Source: IANA special registries (IPv4, IPv6, ASN)
55//!
56//! - Load: `load_bogons()`
57//! - Access: `bogons_match(input)`, `bogons_match_prefix(prefix)`, `bogons_match_asn(asn)`, `get_bogon_prefixes()`, `get_bogon_asns()`
58//! - Detect invalid/reserved IP prefixes and ASNs that shouldn't appear in routing
59//!
60//! ### [`countries`] — Country Information
61//!
62//! Feature: `countries` | Source: GeoNames geographical database
63//!
64//! - Load: `load_countries()`
65//! - Access: `country_by_code(code)`, `country_by_code3(code)`, `country_by_name(name)`, `country_all()`
66//! - ISO country code to name mapping and geographical information
67//!
68//! ### [`mrt_collectors`] — MRT Collector Metadata
69//!
70//! Feature: `mrt_collectors` | Sources: RouteViews and RIPE RIS official APIs
71//!
72//! - Load: `load_mrt_collectors()`, `load_mrt_collector_peers()`
73//! - Access: `mrt_collectors_all()`, `mrt_collectors_by_name(name)`, `mrt_collectors_by_country(country)`, `mrt_collector_peers_all()`, `mrt_collector_peers_full_feed()`
74//! - BGP collector information, peer details, full-feed vs partial-feed classification
75//!
76//! ### [`peeringdb`] — PeeringDB Data
77//!
78//! Feature: `peeringdb` | Source: [PeeringDB API](https://www.peeringdb.com/api/)
79//!
80//! - Load: `Peeringdb::new()` (all tables), `Peeringdb::new_networks_only()` (lightweight)
81//! - Access: `get_network(asn)`, `get_ixp(ix_id)`, `get_ixp_memberships(asn)`, `lookup_ixp_prefix(prefix)`, `get_facility(fac_id)`
82//! - Typed structs and an ASN-to-AS-set map mirroring the 13 supported PeeringDB API endpoints:
83//! networks, internet exchanges, IXP prefixes, IXP membership, facilities, organizations,
84//! carriers, AS-set names, and more
85//! - `PeeringdbData` is a type alias for the full `Network` struct (backward compatible)
86//!
87//! ### [`rpki`] — RPKI Validation
88//!
89//! Feature: `rpki` | Sources: Cloudflare (real-time), RIPE NCC historical, RPKIviews historical, RPKISPOOL historical
90//!
91//! - Load: `load_rpki(optional_date)`, `load_rpki_historical(date, source)`, `load_rpki_from_files(urls, source, date)`
92//! - Poll: `RpkiTrie::from_cloudflare_conditional(etag, last_modified)` returns `Ok(None)` on `304 Not Modified`
93//! - Access: `rpki_validate(asn, prefix)`, `rpki_validate_check_expiry(asn, prefix, timestamp)`, `rpki_lookup_by_prefix(prefix)`, `rpki_lookup_aspa(customer_asn)`
94//! - Route Origin Authorization (ROA) and ASPA validation, supports real-time and historical sources
95//! - Poll current Cloudflare data with `RpkiTrie::from_cloudflare_conditional`, retaining the returned
96//! [`rpki::RpkiLoad`] validators and keeping the existing trie when the result is `Ok(None)`.
97//! - `BgpkitCommons::reload()` performs a full reload; it does not use validators or provide an atomic
98//! poll-and-swap operation.
99//!
100//! ## Examples
101//!
102//! ### Loading multiple modules
103//!
104//! ```rust
105//! # #[cfg(all(feature = "asinfo", feature = "countries"))]
106//! # fn main() {
107//! use bgpkit_commons::BgpkitCommons;
108//!
109//! let mut commons = BgpkitCommons::new();
110//! commons.load_asinfo_with_profile(Default::default()).unwrap();
111//! commons.load_countries().unwrap();
112//!
113//! if let Ok(Some(asinfo)) = commons.asinfo_get(13335) {
114//! println!("AS13335: {} ({})", asinfo.name, asinfo.country);
115//! }
116//! # }
117//! # #[cfg(not(all(feature = "asinfo", feature = "countries")))]
118//! # fn main() {}
119//! ```
120//!
121//! ### Using AsInfoBuilder
122//!
123//! ```rust
124//! # #[cfg(feature = "asinfo")]
125//! # fn main() {
126//! use bgpkit_commons::BgpkitCommons;
127//!
128//! let mut commons = BgpkitCommons::new();
129//! let builder = commons.asinfo_builder()
130//! .with_as2org()
131//! .with_peeringdb();
132//! commons.load_asinfo_with(builder).unwrap();
133//!
134//! if let Ok(are_siblings) = commons.asinfo_are_siblings(13335, 132892) {
135//! println!("AS13335 and AS132892 are siblings: {}", are_siblings);
136//! }
137//! # }
138//! # #[cfg(not(feature = "asinfo"))]
139//! # fn main() {}
140//! ```
141//!
142//! ### Loading historical RPKI data
143//!
144//! ```rust,no_run
145//! # #[cfg(feature = "rpki")]
146//! # fn main() {
147//! use bgpkit_commons::BgpkitCommons;
148//! use bgpkit_commons::rpki::{HistoricalRpkiSource, RpkiViewsCollector};
149//! use chrono::NaiveDate;
150//!
151//! let mut commons = BgpkitCommons::new();
152//! let date = NaiveDate::from_ymd_opt(2024, 1, 4).unwrap();
153//!
154//! // Load from RIPE NCC historical archives
155//! commons.load_rpki_historical(date, HistoricalRpkiSource::Ripe).unwrap();
156//!
157//! // Or load from RPKIviews collectors
158//! let source = HistoricalRpkiSource::RpkiViews(RpkiViewsCollector::KerfuffleNet);
159//! commons.load_rpki_historical(date, source).unwrap();
160//!
161//! // List available files for a date
162//! let files = commons.list_rpki_files(date, HistoricalRpkiSource::Ripe).unwrap();
163//! # }
164//! # #[cfg(not(feature = "rpki"))]
165//! # fn main() {}
166//! ```
167//!
168//! ### Direct module access
169//!
170//! Modules can also be used directly without `BgpkitCommons`:
171//!
172//! ```rust
173//! # #[cfg(feature = "bogons")]
174//! # fn main() {
175//! use bgpkit_commons::bogons::Bogons;
176//! let bogons = Bogons::new().unwrap();
177//! # }
178//! # #[cfg(not(feature = "bogons"))]
179//! # fn main() {}
180//! ```
181//!
182//! ## Feature Flags
183//!
184//! | Feature | Description |
185//! |---------|-------------|
186//! | `asinfo` | AS information: names, countries, organizations, population, hegemony |
187//! | `as2rel` | AS relationship data |
188//! | `bogons` | Bogon prefix and ASN detection |
189//! | `countries` | Country information lookup |
190//! | `mrt_collectors` | MRT collector metadata |
191//! | `peeringdb` | PeeringDB API data (networks, IXPs, facilities, organizations) |
192//! | `rpki` | RPKI validation (ROA and ASPA) |
193//! | `all` *(default)* | Enables all modules |
194//!
195//! For a minimal build:
196//!
197//! ```toml
198//! [dependencies]
199//! bgpkit-commons = { version = "0.14", default-features = false, features = ["bogons", "countries"] }
200//! ```
201
202#![doc(
203 html_logo_url = "https://raw.githubusercontent.com/bgpkit/assets/main/logos/icon-transparent.png",
204 html_favicon_url = "https://raw.githubusercontent.com/bgpkit/assets/main/logos/favicon.ico"
205)]
206
207#[cfg(feature = "as2rel")]
208pub mod as2rel;
209#[cfg(feature = "asinfo")]
210pub mod asinfo;
211#[cfg(feature = "bogons")]
212pub mod bogons;
213#[cfg(feature = "countries")]
214pub mod countries;
215#[cfg(feature = "irr")]
216pub mod irr;
217
218#[cfg(feature = "delegated")]
219pub mod delegated;
220#[cfg(feature = "mrt_collectors")]
221pub mod mrt_collectors;
222#[cfg(feature = "peeringdb")]
223pub mod peeringdb;
224#[cfg(feature = "rpki")]
225pub mod rpki;
226
227#[cfg(feature = "export")]
228pub mod export;
229
230pub mod errors;
231
232// Re-export error types for convenience
233pub use errors::{BgpkitCommonsError, Result};
234
235/// Trait for modules that support lazy loading and reloading of data
236pub trait LazyLoadable {
237 /// Reload the module's data from its external sources
238 fn reload(&mut self) -> Result<()>;
239
240 /// Check if the module's data is currently loaded
241 fn is_loaded(&self) -> bool;
242
243 /// Get a description of the module's current loading status
244 fn loading_status(&self) -> &'static str;
245}
246
247#[derive(Default)]
248pub struct BgpkitCommons {
249 #[cfg(feature = "countries")]
250 countries: Option<crate::countries::Countries>,
251 #[cfg(feature = "rpki")]
252 rpki_trie: Option<crate::rpki::RpkiTrie>,
253 #[cfg(feature = "mrt_collectors")]
254 mrt_collectors: Option<Vec<crate::mrt_collectors::MrtCollector>>,
255 #[cfg(feature = "mrt_collectors")]
256 mrt_collector_peers: Option<Vec<crate::mrt_collectors::MrtCollectorPeer>>,
257 #[cfg(feature = "bogons")]
258 bogons: Option<crate::bogons::Bogons>,
259 #[cfg(feature = "asinfo")]
260 asinfo: Option<crate::asinfo::AsInfoUtils>,
261 #[cfg(feature = "as2rel")]
262 as2rel: Option<crate::as2rel::As2relBgpkit>,
263}
264
265impl BgpkitCommons {
266 pub fn new() -> Self {
267 Self::default()
268 }
269
270 /// Reload all data sources that are already loaded
271 pub fn reload(&mut self) -> Result<()> {
272 #[cfg(feature = "countries")]
273 if self.countries.is_some() {
274 self.load_countries()?;
275 }
276 #[cfg(feature = "rpki")]
277 if let Some(rpki) = self.rpki_trie.as_mut() {
278 rpki.reload()?;
279 }
280 #[cfg(feature = "mrt_collectors")]
281 if self.mrt_collectors.is_some() {
282 self.load_mrt_collectors()?;
283 }
284 #[cfg(feature = "mrt_collectors")]
285 if self.mrt_collector_peers.is_some() {
286 self.load_mrt_collector_peers()?;
287 }
288 #[cfg(feature = "bogons")]
289 if self.bogons.is_some() {
290 self.load_bogons()?;
291 }
292 #[cfg(feature = "asinfo")]
293 if let Some(asinfo) = self.asinfo.as_mut() {
294 asinfo.reload()?;
295 }
296 #[cfg(feature = "as2rel")]
297 if self.as2rel.is_some() {
298 self.load_as2rel()?;
299 }
300
301 Ok(())
302 }
303
304 /// Get loading status for all available modules
305 pub fn loading_status(&self) -> Vec<(&'static str, &'static str)> {
306 #[allow(unused_mut)] // mut needed when any features are enabled
307 let mut status = Vec::new();
308
309 #[cfg(feature = "countries")]
310 if let Some(ref countries) = self.countries {
311 status.push(("countries", countries.loading_status()));
312 } else {
313 status.push(("countries", "Countries data not loaded"));
314 }
315
316 #[cfg(feature = "bogons")]
317 if let Some(ref bogons) = self.bogons {
318 status.push(("bogons", bogons.loading_status()));
319 } else {
320 status.push(("bogons", "Bogons data not loaded"));
321 }
322
323 #[cfg(feature = "rpki")]
324 if let Some(ref rpki) = self.rpki_trie {
325 status.push(("rpki", rpki.loading_status()));
326 } else {
327 status.push(("rpki", "RPKI data not loaded"));
328 }
329
330 #[cfg(feature = "asinfo")]
331 if let Some(ref asinfo) = self.asinfo {
332 status.push(("asinfo", asinfo.loading_status()));
333 } else {
334 status.push(("asinfo", "ASInfo data not loaded"));
335 }
336
337 #[cfg(feature = "as2rel")]
338 if let Some(ref as2rel) = self.as2rel {
339 status.push(("as2rel", as2rel.loading_status()));
340 } else {
341 status.push(("as2rel", "AS2Rel data not loaded"));
342 }
343
344 #[cfg(feature = "mrt_collectors")]
345 {
346 if self.mrt_collectors.is_some() {
347 status.push(("mrt_collectors", "MRT collectors data loaded"));
348 } else {
349 status.push(("mrt_collectors", "MRT collectors data not loaded"));
350 }
351
352 if self.mrt_collector_peers.is_some() {
353 status.push(("mrt_collector_peers", "MRT collector peers data loaded"));
354 } else {
355 status.push(("mrt_collector_peers", "MRT collector peers data not loaded"));
356 }
357 }
358
359 status
360 }
361
362 /// Load countries data
363 #[cfg(feature = "countries")]
364 pub fn load_countries(&mut self) -> Result<()> {
365 self.countries = Some(crate::countries::Countries::new()?);
366 Ok(())
367 }
368
369 /// Load RPKI data from Cloudflare (real-time) or historical archives
370 ///
371 /// - If `date_opt` is `None`, loads real-time data from Cloudflare
372 /// - If `date_opt` is `Some(date)`, loads historical data from RIPE NCC by default
373 ///
374 /// For more control over the data source, use `load_rpki_historical()` instead.
375 #[cfg(feature = "rpki")]
376 pub fn load_rpki(&mut self, date_opt: Option<chrono::NaiveDate>) -> Result<()> {
377 if let Some(date) = date_opt {
378 self.rpki_trie = Some(rpki::RpkiTrie::from_ripe_historical(date)?);
379 } else {
380 self.rpki_trie = Some(rpki::RpkiTrie::from_cloudflare()?);
381 }
382 Ok(())
383 }
384
385 /// Load RPKI data from a specific historical data source
386 ///
387 /// This allows you to choose between RIPE NCC and RPKIviews for historical data.
388 ///
389 /// # Example
390 ///
391 /// ```rust,no_run
392 /// use bgpkit_commons::BgpkitCommons;
393 /// use bgpkit_commons::rpki::{HistoricalRpkiSource, RpkiViewsCollector};
394 /// use chrono::NaiveDate;
395 ///
396 /// let mut commons = BgpkitCommons::new();
397 /// let date = NaiveDate::from_ymd_opt(2024, 1, 4).unwrap();
398 ///
399 /// // Load from RIPE NCC
400 /// commons.load_rpki_historical(date, HistoricalRpkiSource::Ripe).unwrap();
401 ///
402 /// // Or load from RPKIviews
403 /// let source = HistoricalRpkiSource::RpkiViews(RpkiViewsCollector::KerfuffleNet);
404 /// commons.load_rpki_historical(date, source).unwrap();
405 /// ```
406 #[cfg(feature = "rpki")]
407 pub fn load_rpki_historical(
408 &mut self,
409 date: chrono::NaiveDate,
410 source: rpki::HistoricalRpkiSource,
411 ) -> Result<()> {
412 match source {
413 rpki::HistoricalRpkiSource::Ripe => {
414 self.rpki_trie = Some(rpki::RpkiTrie::from_ripe_historical(date)?);
415 }
416 rpki::HistoricalRpkiSource::RpkiViews(collector) => {
417 self.rpki_trie = Some(rpki::RpkiTrie::from_rpkiviews(collector, date)?);
418 }
419 rpki::HistoricalRpkiSource::RpkiSpools(collector) => {
420 self.rpki_trie = Some(rpki::RpkiTrie::from_rpkispools(collector, date)?);
421 }
422 }
423 Ok(())
424 }
425
426 /// Load RPKI data from specific file URLs
427 ///
428 /// This allows loading from specific archive files, which is useful when you want
429 /// to process multiple files or use specific timestamps.
430 ///
431 /// # Arguments
432 ///
433 /// * `urls` - A slice of URLs pointing to RPKI data files
434 /// * `source` - The type of data source (RIPE, RPKIviews, or RPKISPOOL) - determines how files are parsed
435 /// * `date` - Optional date to associate with the loaded data
436 ///
437 /// # Example
438 ///
439 /// ```rust,no_run
440 /// use bgpkit_commons::BgpkitCommons;
441 /// use bgpkit_commons::rpki::HistoricalRpkiSource;
442 ///
443 /// let mut commons = BgpkitCommons::new();
444 /// let urls = vec![
445 /// "https://example.com/rpki-20240104T144128Z.tgz".to_string(),
446 /// ];
447 /// commons.load_rpki_from_files(&urls, HistoricalRpkiSource::RpkiViews(
448 /// bgpkit_commons::rpki::RpkiViewsCollector::KerfuffleNet
449 /// ), None).unwrap();
450 /// ```
451 #[cfg(feature = "rpki")]
452 pub fn load_rpki_from_files(
453 &mut self,
454 urls: &[String],
455 source: rpki::HistoricalRpkiSource,
456 date: Option<chrono::NaiveDate>,
457 ) -> Result<()> {
458 match source {
459 rpki::HistoricalRpkiSource::Ripe => {
460 self.rpki_trie = Some(rpki::RpkiTrie::from_ripe_files(urls, date)?);
461 }
462 rpki::HistoricalRpkiSource::RpkiViews(_) => {
463 self.rpki_trie = Some(rpki::RpkiTrie::from_rpkiviews_files(urls, date)?);
464 }
465 rpki::HistoricalRpkiSource::RpkiSpools(_) => {
466 // For RPKISPOOL, each URL is a tar.zst archive; load the first one
467 if let Some(url) = urls.first() {
468 self.rpki_trie = Some(rpki::RpkiTrie::from_rpkispools_url(url, date)?);
469 }
470 }
471 }
472 Ok(())
473 }
474
475 /// List available RPKI files for a given date from a specific source
476 ///
477 /// # Example
478 ///
479 /// ```rust,no_run
480 /// use bgpkit_commons::BgpkitCommons;
481 /// use bgpkit_commons::rpki::{HistoricalRpkiSource, RpkiViewsCollector};
482 /// use chrono::NaiveDate;
483 ///
484 /// let commons = BgpkitCommons::new();
485 /// let date = NaiveDate::from_ymd_opt(2024, 1, 4).unwrap();
486 ///
487 /// // List files from RIPE NCC
488 /// let ripe_files = commons.list_rpki_files(date, HistoricalRpkiSource::Ripe).unwrap();
489 ///
490 /// // List files from RPKIviews
491 /// let source = HistoricalRpkiSource::RpkiViews(RpkiViewsCollector::KerfuffleNet);
492 /// let rpkiviews_files = commons.list_rpki_files(date, source).unwrap();
493 /// ```
494 #[cfg(feature = "rpki")]
495 pub fn list_rpki_files(
496 &self,
497 date: chrono::NaiveDate,
498 source: rpki::HistoricalRpkiSource,
499 ) -> Result<Vec<rpki::RpkiFile>> {
500 match source {
501 rpki::HistoricalRpkiSource::Ripe => rpki::list_ripe_files(date),
502 rpki::HistoricalRpkiSource::RpkiViews(collector) => {
503 rpki::list_rpkiviews_files(collector, date)
504 }
505 rpki::HistoricalRpkiSource::RpkiSpools(collector) => {
506 rpki::list_rpkispools_files(collector, date)
507 }
508 }
509 }
510
511 /// Load MRT mrt_collectors data
512 #[cfg(feature = "mrt_collectors")]
513 pub fn load_mrt_collectors(&mut self) -> Result<()> {
514 self.mrt_collectors = Some(crate::mrt_collectors::get_all_collectors()?);
515 Ok(())
516 }
517
518 /// Load MRT mrt_collectors data
519 #[cfg(feature = "mrt_collectors")]
520 pub fn load_mrt_collector_peers(&mut self) -> Result<()> {
521 self.mrt_collector_peers = Some(crate::mrt_collectors::get_mrt_collector_peers()?);
522 Ok(())
523 }
524
525 /// Load bogons data
526 #[cfg(feature = "bogons")]
527 pub fn load_bogons(&mut self) -> Result<()> {
528 self.bogons = Some(crate::bogons::Bogons::new()?);
529 Ok(())
530 }
531
532 /// Load AS information using a loading profile.
533 ///
534 /// Profiles provide curated presets for common use cases:
535 /// - [`Minimum`](crate::asinfo::AsInfoProfile::Minimum): asn.txt only (~1s)
536 /// - [`Default`](crate::asinfo::AsInfoProfile::Default): + as2org, population, hegemony, peeringdb (~30s)
537 /// - [`Full`](crate::asinfo::AsInfoProfile::Full): + delegated stats, IRR data with route prefixes (~75s)
538 ///
539 /// For fine-grained control beyond these presets, use the builder via
540 /// [`BgpkitCommons::asinfo_builder`].
541 ///
542 /// # Example
543 ///
544 /// ```rust,no_run
545 /// use bgpkit_commons::asinfo::AsInfoProfile;
546 /// use bgpkit_commons::BgpkitCommons;
547 ///
548 /// let mut commons = BgpkitCommons::new();
549 /// commons.load_asinfo_with_profile(AsInfoProfile::Default).unwrap();
550 /// ```
551 #[cfg(feature = "asinfo")]
552 pub fn load_asinfo_with_profile(
553 &mut self,
554 profile: crate::asinfo::AsInfoProfile,
555 ) -> Result<()> {
556 self.asinfo = Some(profile.builder().build()?);
557 Ok(())
558 }
559
560 /// Load AS name and country data with optional enrichment sources.
561 ///
562 /// Compatibility shim retained from the pre-v0.12 boolean-argument API.
563 /// Maps to the equivalent [`AsInfoBuilder`](crate::asinfo::AsInfoBuilder)
564 /// configuration with identical behavior. Prefer
565 /// [`BgpkitCommons::load_asinfo_with_profile`] or
566 /// [`BgpkitCommons::load_asinfo_with`].
567 #[cfg(feature = "asinfo")]
568 #[deprecated(note = "use load_asinfo_with_profile() or load_asinfo_with() instead")]
569 pub fn load_asinfo(
570 &mut self,
571 load_as2org: bool,
572 load_population: bool,
573 load_hegemony: bool,
574 load_peeringdb: bool,
575 ) -> Result<()> {
576 let mut builder = crate::asinfo::AsInfoBuilder::new();
577 if load_as2org {
578 builder = builder.with_as2org();
579 }
580 if load_population {
581 builder = builder.with_population();
582 }
583 if load_hegemony {
584 builder = builder.with_hegemony();
585 }
586 if load_peeringdb {
587 builder = builder.with_peeringdb();
588 }
589 self.asinfo = Some(builder.build()?);
590 Ok(())
591 }
592
593 #[cfg(feature = "asinfo")]
594 pub fn load_asinfo_cached(&mut self) -> Result<()> {
595 self.asinfo = Some(crate::asinfo::AsInfoUtils::new_from_cached()?);
596 Ok(())
597 }
598
599 /// Returns a builder for loading AS information with specific data sources.
600 ///
601 /// This provides a more ergonomic way to configure which data sources to load
602 /// compared to the `load_asinfo()` method with boolean parameters.
603 ///
604 /// # Example
605 ///
606 /// ```rust,no_run
607 /// use bgpkit_commons::BgpkitCommons;
608 ///
609 /// let mut commons = BgpkitCommons::new();
610 /// let builder = commons.asinfo_builder()
611 /// .with_as2org()
612 /// .with_peeringdb();
613 /// commons.load_asinfo_with(builder).unwrap();
614 /// ```
615 #[cfg(feature = "asinfo")]
616 pub fn asinfo_builder(&self) -> crate::asinfo::AsInfoBuilder {
617 crate::asinfo::AsInfoBuilder::new()
618 }
619
620 /// Load AS information using a pre-configured builder.
621 ///
622 /// # Example
623 ///
624 /// ```rust,no_run
625 /// use bgpkit_commons::BgpkitCommons;
626 ///
627 /// let mut commons = BgpkitCommons::new();
628 /// let builder = commons.asinfo_builder()
629 /// .with_as2org()
630 /// .with_hegemony();
631 /// commons.load_asinfo_with(builder).unwrap();
632 /// ```
633 #[cfg(feature = "asinfo")]
634 pub fn load_asinfo_with(&mut self, builder: crate::asinfo::AsInfoBuilder) -> Result<()> {
635 self.asinfo = Some(builder.build()?);
636 Ok(())
637 }
638
639 /// Load AS-level relationship data
640 #[cfg(feature = "as2rel")]
641 pub fn load_as2rel(&mut self) -> Result<()> {
642 self.as2rel = Some(crate::as2rel::As2relBgpkit::new()?);
643 Ok(())
644 }
645}