link-cli 3.1.0

A CLI tool and reusable library for links manipulation backed by a LiNo-notation doublet storage engine.
Documentation
//! Helpers shared by the integration tests that talk to a [`LinksServer`].

#![allow(dead_code)] // Every test crate compiles this module but uses only part of it.

use link_cli::protocol::{LinksClient, LinksServer, LinoProtocol, ServerOptions};
use link_cli::{LinkStorage, NamedTypesDecorator};
use std::net::SocketAddr;
use std::path::Path;
use std::thread::{self, JoinHandle};
use tempfile::NamedTempFile;

/// A [`LinksServer`] serving a temporary store on an ephemeral port; dropping
/// it stops the server.
pub struct RunningServer {
    pub address: SocketAddr,
    pub shutdown: link_cli::protocol::ShutdownHandle,
    pub thread: Option<JoinHandle<()>>,
    _database: NamedTempFile,
}

impl RunningServer {
    pub fn start(options: ServerOptions, named: bool) -> Self {
        Self::start_with(options, named, |server| server)
    }

    /// Like [`start`](Self::start), letting `configure` set the server up
    /// before it serves.
    pub fn start_with(
        options: ServerOptions,
        named: bool,
        configure: impl FnOnce(LinksServer) -> LinksServer + Send + 'static,
    ) -> Self {
        Self::start_serving(options, move |server, path| {
            let server = configure(server);
            if named {
                let mut storage = NamedTypesDecorator::new(path, false).unwrap();
                server.serve(&mut storage).unwrap();
            } else {
                let mut storage = LinkStorage::new(path, false).unwrap();
                server.serve(&mut storage).unwrap();
            }
        })
    }

    /// Lets `serve` serve the temporary store at the path it is given.
    pub fn start_serving(
        options: ServerOptions,
        serve: impl FnOnce(LinksServer, &Path) + Send + 'static,
    ) -> Self {
        let database = NamedTempFile::new().unwrap();
        let path = database.path().to_path_buf();
        let server = LinksServer::bind("127.0.0.1:0", options).unwrap();
        let address = server.local_addr().unwrap();
        let shutdown = server.shutdown_handle().unwrap();
        let thread = thread::spawn(move || serve(server, &path));
        Self {
            address,
            shutdown,
            thread: Some(thread),
            _database: database,
        }
    }

    pub fn client(&self, protocol: impl LinoProtocol + 'static) -> LinksClient {
        LinksClient::connect(self.address, protocol).unwrap()
    }

    /// Stops the server and waits for it to finish.
    pub fn stop(&mut self) {
        self.shutdown.shutdown();
        if let Some(thread) = self.thread.take() {
            thread.join().unwrap();
        }
    }
}

impl Drop for RunningServer {
    fn drop(&mut self) {
        self.stop();
    }
}

mod options;
#[allow(unused_imports)] // Individual test crates use only part of these helpers.
pub use options::{binary_options, protocols};