use std::path::{Path, PathBuf};
use cargo_metadata::Package;
use serde::{Deserialize, Serialize};
#[derive(Default, Clone)]
pub struct GuestOptions {
pub features: Vec<String>,
pub options: Vec<String>,
pub rustc_flags: Vec<String>,
pub profile: Option<String>,
pub target_dir: Option<PathBuf>,
}
impl GuestOptions {
pub fn with_options<S: AsRef<str>>(mut self, options: impl IntoIterator<Item = S>) -> Self {
self.options
.extend(options.into_iter().map(|s| s.as_ref().to_string()));
self
}
pub fn with_features<S: AsRef<str>>(mut self, features: impl IntoIterator<Item = S>) -> Self {
self.features
.extend(features.into_iter().map(|s| s.as_ref().to_string()));
self
}
pub fn with_rustc_flags<S: AsRef<str>>(mut self, flags: impl IntoIterator<Item = S>) -> Self {
self.rustc_flags
.extend(flags.into_iter().map(|s| s.as_ref().to_string()));
self
}
pub fn with_profile(mut self, profile: String) -> Self {
self.profile = Some(profile);
self
}
pub fn with_target_dir<P: AsRef<Path>>(mut self, target_dir: P) -> Self {
self.target_dir = Some(target_dir.as_ref().to_path_buf());
self
}
#[allow(dead_code)]
pub(crate) fn with_metadata(mut self, metadata: GuestMetadata) -> Self {
self.rustc_flags = metadata.rustc_flags.unwrap_or_default();
self
}
}
#[derive(Serialize, Deserialize, Clone, Default)]
pub(crate) struct GuestMetadata {
#[serde(rename = "rustc-flags")]
pub(crate) rustc_flags: Option<Vec<String>>,
}
impl From<&Package> for GuestMetadata {
fn from(value: &Package) -> Self {
let Some(obj) = value.metadata.get("risc0") else {
return Default::default();
};
serde_json::from_value(obj.clone()).unwrap()
}
}