use futures::stream::{SelectAll, Stream, StreamExt};
use std::pin::{Pin, pin};
use std::sync::Mutex;
use taganak_core::sources::serializers::GraphSerializer;
use taganak_core::sources::serializers::turtle::TurtleSerializer;
use taganak_framework::graphs::local::memory::simple::SimpleGraph;
use tokio::io::stdout;
use taganak_core::prelude::*;
use tinipon::registry::discover;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let mut streams: SelectAll<
Pin<Box<dyn Stream<Item = Result<std::sync::Arc<Triple>, SourceError>> + Send + Sync>>,
> = SelectAll::new();
let mut profile = BasicProfile::new();
profile.set_prefix(
"tp".to_string(),
"http://tinipon.taganak.net/vocab#".to_string(),
);
profile.set_prefix(
"tpp".to_string(),
"http://tinipon.taganak.net/reg/providers/".to_string(),
);
profile.set_prefix(
"tpm.system".to_string(),
"http://tinipon.taganak.net/reg/modules/system#".to_string(),
);
let profile = Arc::new(Mutex::new(profile));
let mut providers = pin!(discover()?);
while let Some(provider) = providers.next().await {
let exec1 = provider.exec();
let exec2 = exec1.clone();
let describe = Box::pin(exec1.describe()?);
let run = Box::pin(exec2.run()?);
streams.push(describe);
streams.push(run);
}
let mut graph = SimpleGraph::default();
graph.merge_stream(streams).await?;
let serializer = TurtleSerializer::new(profile.clone(), None);
serializer.write_all(&graph, &mut stdout()).await?;
Ok(())
}