| |
Ok(())
|
| |
}
|
| |
}
|
| + |
|
| + |
/// Helper functions for writing adapters.
|
| + |
pub mod helper {
|
| + |
use super::{MessageError, Request, Response, RunId, RunResult};
|
| + |
|
| + |
/// Read a request from stdin.
|
| + |
pub fn read_request() -> Result<Request, NativeMessageError> {
|
| + |
let req =
|
| + |
Request::from_reader(std::io::stdin()).map_err(NativeMessageError::ReadRequest)?;
|
| + |
Ok(req)
|
| + |
}
|
| + |
|
| + |
// Write response to stdout.
|
| + |
fn write_response(resp: &Response) -> Result<(), NativeMessageError> {
|
| + |
resp.to_writer(std::io::stdout())
|
| + |
.map_err(|e| NativeMessageError::WriteResponse(resp.clone(), Box::new(e)))?;
|
| + |
Ok(())
|
| + |
}
|
| + |
|
| + |
/// Write a "triggered" response to stdout.
|
| + |
pub fn write_triggered(
|
| + |
run_id: &RunId,
|
| + |
info_url: Option<&str>,
|
| + |
) -> Result<(), NativeMessageError> {
|
| + |
let response = if let Some(url) = info_url {
|
| + |
Response::triggered_with_url(run_id.clone(), url)
|
| + |
} else {
|
| + |
Response::triggered(run_id.clone())
|
| + |
};
|
| + |
write_response(&response)?;
|
| + |
Ok(())
|
| + |
}
|
| + |
|
| + |
/// Write a message indicating failure to stdout.
|
| + |
pub fn write_failed() -> Result<(), NativeMessageError> {
|
| + |
write_response(&Response::Finished {
|
| + |
result: RunResult::Failure,
|
| + |
})?;
|
| + |
Ok(())
|
| + |
}
|
| + |
|
| + |
/// Write a message indicating success to stdout.
|
| + |
pub fn write_succeeded() -> Result<(), NativeMessageError> {
|
| + |
write_response(&Response::Finished {
|
| + |
result: RunResult::Success,
|
| + |
})?;
|
| + |
Ok(())
|
| + |
}
|
| + |
|
| + |
/// Possible errors from this module.
|
| + |
#[derive(Debug, thiserror::Error)]
|
| + |
pub enum NativeMessageError {
|
| + |
/// Error reading request from stdin.
|
| + |
#[error("failed to read request from stdin: {0:?}")]
|
| + |
ReadRequest(#[source] MessageError),
|
| + |
|
| + |
/// Error writing response to stdout.
|
| + |
#[error("failed to write response to stdout: {0:?}")]
|
| + |
WriteResponse(Response, #[source] Box<MessageError>),
|
| + |
}
|
| + |
}
|