use crate::CryptDisk;
use async_trait::async_trait;
use disk_backend::resolve::ResolveDiskParameters;
use disk_backend::resolve::ResolvedDisk;
use disk_crypt_resources::DiskCryptHandle;
use thiserror::Error;
use vm_resource::AsyncResolveResource;
use vm_resource::ResolveError;
use vm_resource::ResourceResolver;
use vm_resource::declare_static_async_resolver;
use vm_resource::kind::DiskHandleKind;
declare_static_async_resolver! {
DiskCryptResolver,
(DiskHandleKind, DiskCryptHandle),
}
pub struct DiskCryptResolver;
#[derive(Debug, Error)]
pub enum DiskResolveError {
#[error("failed to resolve inner disk")]
ResolveInner(#[source] ResolveError),
#[error("failed to create disk")]
NewDisk(#[source] crate::NewDiskError),
#[error("invalid disk")]
InvalidDisk(#[source] disk_backend::InvalidDisk),
}
#[async_trait]
impl AsyncResolveResource<DiskHandleKind, DiskCryptHandle> for DiskCryptResolver {
type Output = ResolvedDisk;
type Error = DiskResolveError;
async fn resolve(
&self,
resolver: &ResourceResolver,
resource: DiskCryptHandle,
input: ResolveDiskParameters<'_>,
) -> Result<Self::Output, Self::Error> {
let inner = resolver
.resolve(
resource.disk,
ResolveDiskParameters {
read_only: input.read_only,
_async_trait_workaround: &(),
},
)
.await
.map_err(DiskResolveError::ResolveInner)?;
let disk = CryptDisk::new(resource.cipher, &resource.key, inner.0)
.map_err(DiskResolveError::NewDisk)?;
ResolvedDisk::new(disk).map_err(DiskResolveError::InvalidDisk)
}
}