rustimal 0.8.0

A minimal, GPU-rendered terminal for Windows that runs Git Bash, with animated split panes and a glass look
//! The work a review panel asks for, done on threads of its own, and what comes back.

use super::*;

/// A number no other job has had, in this panel or any other.
pub(super) fn next_number() -> u64 {
    static LAST: AtomicU64 = AtomicU64::new(0);
    LAST.fetch_add(1, Ordering::Relaxed) + 1
}

/// Work done off the UI thread.
#[derive(Clone, Debug, PartialEq)]
pub enum Job {
    /// What can be reviewed here.
    List,
    /// A change, as the arguments `review` takes.
    Open(Vec<String>),
    Git(GitJob),
    /// A new pull request, for a branch that was being looked at or the one checked out.
    Propose(Option<propose::Of>),
}

/// What a job sends back. The number says which job it was; answers to older ones are dropped.
pub enum Message {
    Local(u64, Result<Local, String>),
    Pulls(u64, Result<(&'static str, Vec<PullRequest>), String>),
    Loaded(u64, Result<Loaded, String>),
    /// An approval went through, on the host named, or why not.
    Approved(u64, Result<&'static str, String>),
    /// The host's name and whether it has finished merging, or why it didn't merge.
    Merged(u64, Result<(&'static str, bool), String>),
    Git(u64, Result<Box<GitReply>, String>),
    /// What a new pull request could look like.
    Proposed(u64, Result<propose::Start, String>),
    /// A pull request was created, or why not.
    Created(u64, Result<(&'static str, PullRequest), String>),
}

/// Run a job on its own thread. Listing answers twice: what git knows comes at once, the pull
/// requests when the host has replied.
pub fn spawn(job: Job, cwd: PathBuf, number: u64, send: impl Fn(Message) + Send + 'static) {
    std::thread::spawn(move || match job {
        Job::Git(job) => send(Message::Git(number, workspace::run(&cwd, job).map(Box::new))),
        Job::Propose(of) => send(Message::Proposed(number, propose::propose(&cwd, of.as_ref()))),
        Job::List => {
            let root = source::repository(&cwd);
            send(Message::Local(number, root.clone().map(|r| source::local(&r))));
            let pulls = root.and_then(|r| forge::list(&r)).map(|(kind, prs)| (kind.label(), prs));
            send(Message::Pulls(number, pulls));
        }
        Job::Open(args) => {
            let loaded = match parse_args(&args) {
                Ok(Parsed::Review(o)) => source::load(&cwd, &o),
                Ok(_) => Err("that option only works in the terminal: add --print".to_string()),
                Err(e) => Err(e),
            };
            send(Message::Loaded(number, loaded));
        }
    });
}

/// Approve a pull request on its host, off the UI thread.
pub fn spawn_approve(root: PathBuf, pull: u64, head: String, number: u64, send: impl Fn(Message) + Send + 'static) {
    std::thread::spawn(move || send(Message::Approved(number, forge::approve(&root, pull, &head))));
}

/// Push the branch and create the pull request, off the UI thread.
pub fn spawn_create(proposal: Proposal, number: u64, send: impl Fn(Message) + Send + 'static) {
    std::thread::spawn(move || send(Message::Created(number, propose::create(&proposal))));
}

/// Merge a pull request on its host, off the UI thread.
pub fn spawn_merge(
    root: PathBuf,
    pull: u64,
    head: String,
    how: Method,
    number: u64,
    send: impl Fn(Message) + Send + 'static,
) {
    std::thread::spawn(move || send(Message::Merged(number, forge::merge(&root, pull, &head, how))));
}