handle optional response results

This commit is contained in:
yggverse 2025-08-16 18:11:50 +03:00
parent a4f82e8bbc
commit eb6d4633cd

View file

@ -40,8 +40,13 @@ fn main() -> Result<()> {
todo!() todo!()
} }
// start crawler // start crawler
for peer in p.response.unwrap().peers { match p.response {
crawl(peer.key, &config, &mut ygg, &mut key, &mut tcp, &mut udp)?; Some(response) => {
for peer in response.peers {
crawl(peer.key, &config, &mut ygg, &mut key, &mut tcp, &mut udp)?;
}
}
None => println!("node has no peers to connect."),
} }
Ok(()) Ok(())
} }
@ -54,14 +59,17 @@ fn crawl(
tcp: &mut Option<Vec<SocketAddr>>, tcp: &mut Option<Vec<SocketAddr>>,
udp: &mut Option<(Vec<SocketAddr>, Udp)>, udp: &mut Option<(Vec<SocketAddr>, Udp)>,
) -> Result<()> { ) -> Result<()> {
if !key.contains(&k) { if key.contains(&k) {
if config.debug { return Ok(());
println!("get peers for `{k}`..."); }
} if config.debug {
let p = ygg.remote_peers(&k)?; println!("get peers for `{k}`...");
if p.status == "success" { }
key.push(k); let p = ygg.remote_peers(&k)?;
for (host, peers) in p.response.unwrap() { if p.status == "success" {
key.push(k);
if let Some(response) = p.response {
for (host, peers) in response {
for port in &config.port { for port in &config.port {
let address = SocketAddr::new(IpAddr::V6(host), *port); let address = SocketAddr::new(IpAddr::V6(host), *port);
if let Some(index) = tcp if let Some(index) = tcp
@ -94,14 +102,14 @@ fn crawl(
crawl(k, config, ygg, key, tcp, udp)?; crawl(k, config, ygg, key, tcp, udp)?;
} }
} }
} else if config.debug {
println!(
"\t{}: peer `{k}` return status `{}`, skip.",
WARNING.yellow(),
p.status
);
key.push(k) // ban
} }
} else if config.debug {
println!(
"\t{}: peer `{k}` return status `{}`, skip.",
WARNING.yellow(),
p.status
);
key.push(k) // ban
} }
Ok(()) Ok(())
} }