hglib 0.1.1

Mercurial command server client library.
Documentation
//! High-level interface to the Mercurial command server.
//!
//! This module is currently only a skeletal implementation. In the
//! future this will allow you to run Mercurial commands by calling
//! methods of the same name:
//!
//! ```ignore
//! use hglib::cmdserver::CommandServer;
//! let cmdserver = CommandServer::new().expect("failed to start command server");
//! let statcmd = cmdserver.status();
//! ```
//!
//! and get the results back as native Rust data types. The details of
//! this implementation are yet to be determined. For an idea of what's
//! in store, see the [Python hglib documentation][python-hglib].
//!
//! [python-hglib]: https://mercurial.selenic.com/wiki/PythonHglib

use std::io;
use connection::*;

/// Spawns and communicates with a command server process.
pub struct CommandServer {
    /// A handle on the spawned process.
    pub connection: Connection,
    /// The list of capabilities the server reported on startup.
    pub capabilities: Vec<String>,
    /// The character encoding the server reported on startup.
    pub encoding: String,
}

impl CommandServer {
    /// Constructs and starts up a command server instance, or returns an error.
    pub fn new() -> io::Result<CommandServer> {
        let mut conn = try!(Connection::new());
        let (capabilities, encoding) = match conn.read_hello() {
            Ok((caps, enc)) => (caps, enc),
            Err(e)   => panic!("failed to read server hello: {}", e),
        };
        Ok(CommandServer {
            connection: conn,
            capabilities: capabilities,
            encoding: encoding,
        })
    }
}