forked from Cutieguwu/raven
Most of the refactor. Need to switch machines.
This commit is contained in:
15
crates/io/Cargo.toml
Normal file
15
crates/io/Cargo.toml
Normal file
@@ -0,0 +1,15 @@
|
||||
[package]
|
||||
name = "io"
|
||||
version = "0.1.0"
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
description = "Raven's IO utilities"
|
||||
repository.workspace = true
|
||||
|
||||
publish.workspace = true
|
||||
test.workspace = true
|
||||
|
||||
[dependencies]
|
||||
derive_more.workspace = true
|
||||
subprocess.workspace = true
|
||||
11
crates/io/src/error.rs
Normal file
11
crates/io/src/error.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
use derive_more::{Display, From};
|
||||
|
||||
pub type Result<T> = core::result::Result<T, Error>;
|
||||
|
||||
#[derive(Debug, From, Display)]
|
||||
pub enum Error {
|
||||
#[from]
|
||||
Io(std::io::Error),
|
||||
#[from]
|
||||
Popen(subprocess::PopenError),
|
||||
}
|
||||
30
crates/io/src/lib.rs
Normal file
30
crates/io/src/lib.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
mod error;
|
||||
|
||||
use std::ffi;
|
||||
|
||||
pub use error::{Error, Result};
|
||||
|
||||
pub fn run_process<S>(argv: &[S]) -> Result<(Option<String>, Option<String>)>
|
||||
where
|
||||
S: AsRef<ffi::OsStr>,
|
||||
{
|
||||
let mut process = subprocess::Popen::create(
|
||||
argv,
|
||||
subprocess::PopenConfig {
|
||||
stdout: subprocess::Redirection::Pipe,
|
||||
..Default::default()
|
||||
},
|
||||
)?;
|
||||
|
||||
let result = process.communicate(None)?;
|
||||
|
||||
if process
|
||||
.wait_timeout(std::time::Duration::from_secs(5))
|
||||
.is_err()
|
||||
|| process.exit_status().is_none()
|
||||
{
|
||||
process.terminate()?;
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
Reference in New Issue
Block a user