use crate::Error;
use crate::helper::{Helper, Mode};
use plughost_core::ipc::{Request, Response};
use plughost_core::{
HostIdentity, InputError, PresetDiscovery, PresetDiscoveryTarget, PresetLocation,
PresetProviderInfo,
};
use std::path::Path;
use std::time::Duration;
pub fn preset_providers(
helper: &Path,
bundle: &Path,
host: &HostIdentity,
timeout: Duration,
) -> Result<Vec<PresetProviderInfo>, Error> {
host.validate()
.map_err(|error| Error::Input { slot: None, error })?;
if bundle.to_str().is_none() {
return Err(path_encoding());
}
let mut helper = Helper::spawn(helper, Mode::Scan)?;
match helper.request(
Request::PresetProviders {
bundle: bundle.to_owned(),
host: host.clone(),
},
timeout,
)? {
Response::PresetProviders(providers) => Ok(providers),
_ => Err(Error::Protocol),
}
}
pub fn discover_presets(
helper: &Path,
bundle: &Path,
host: &HostIdentity,
target: &PresetDiscoveryTarget,
timeout: Duration,
) -> Result<PresetDiscovery, Error> {
host.validate()
.map_err(|error| Error::Input { slot: None, error })?;
if bundle.to_str().is_none() || !encodable(&target.location) {
return Err(path_encoding());
}
target
.validate()
.map_err(|error| Error::Input { slot: None, error })?;
let mut helper = Helper::spawn(helper, Mode::Scan)?;
match helper.request(
Request::DiscoverPresets {
bundle: bundle.to_owned(),
host: host.clone(),
target: target.clone(),
},
timeout,
)? {
Response::DiscoveredPresets(presets) => Ok(presets),
_ => Err(Error::Protocol),
}
}
pub(crate) fn encodable(location: &PresetLocation) -> bool {
match location {
PresetLocation::File { path } => path.to_str().is_some(),
PresetLocation::Plugin => true,
_ => false,
}
}
fn path_encoding() -> Error {
Error::Input {
slot: None,
error: InputError::PathEncoding,
}
}