use graph_api_lib::{EdgeReference, EdgeSearch, Graph, VertexReference, VertexSearch};
use graph_api_simplegraph::SimpleGraph;
use graph_api_test::{Edge, Vertex, populate_graph};
use std::ops::ControlFlow;
fn main() {
let mut graph = SimpleGraph::new();
let refs = populate_graph(&mut graph);
vertex_example(&graph);
edge_example(&graph, refs.bryn);
}
fn vertex_example<G>(graph: &G)
where
G: Graph<Vertex = Vertex, Edge = Edge>,
{
let project = graph
.walk()
.vertices(VertexSearch::scan())
.control_flow(|vertex, _| {
if let Vertex::Project(project) = vertex.weight() {
if project.name.contains("Graph") {
return ControlFlow::Break(Some(vertex));
}
return ControlFlow::Continue(Some(vertex));
}
ControlFlow::Continue(None)
})
.first();
match project {
Some(id) => println!("Found project with 'Graph' in the name: {:?}", id),
None => println!("No projects with 'Graph' in the name"),
}
}
fn edge_example<G>(graph: &G, start_id: G::VertexId)
where
G: Graph<Vertex = Vertex, Edge = Edge>,
{
let early_connection = graph
.walk()
.vertices_by_id(vec![start_id])
.edges(EdgeSearch::scan())
.control_flow(|edge, _| {
if let Edge::Knows { since } = edge.weight() {
if *since < 2010 {
println!(
"Found a very old connection ({}), stopping traversal",
since
);
return ControlFlow::Break(Some(edge));
}
return ControlFlow::Continue(Some(edge));
}
ControlFlow::Continue(None)
})
.first();
match early_connection {
Some(id) => println!("Old connection {:?}", id),
None => println!("No connections found"),
}
}