macro_rules! getters {
() => {};
($(#[$attr:meta])* copy $field:ident: $type:ty; $($rest:tt)*) => {
$(#[$attr])*
pub(crate) fn $field(&self) -> $type {
self.$field
}
getters! { $($rest)* }
};
($(#[$attr:meta])* str $field:ident; $($rest:tt)*) => {
getters! { $(#[$attr])* str $field => $field; $($rest)* }
};
($(#[$attr:meta])* str $field:ident => $name:ident; $($rest:tt)*) => {
$(#[$attr])*
pub(crate) fn $name(&self) -> &str {
&self.$field
}
getters! { $($rest)* }
};
($(#[$attr:meta])* optional_str $field:ident; $($rest:tt)*) => {
$(#[$attr])*
pub(crate) fn $field(&self) -> Option<&str> {
self.$field.as_deref()
}
getters! { $($rest)* }
};
($(#[$attr:meta])* borrow $field:ident: $type:ty; $($rest:tt)*) => {
$(#[$attr])*
pub(crate) fn $field(&self) -> &$type {
&self.$field
}
getters! { $($rest)* }
};
($(#[$attr:meta])* present $field:ident: $type:ty => $has:ident; $($rest:tt)*) => {
pub(crate) fn $has(&self) -> bool {
self.$field.is_some()
}
#[doc = concat!("Swift calls this only when [`Self::", stringify!($has), "`] is true.")]
$(#[$attr])*
pub(crate) fn $field(&self) -> &$type {
$crate::bridge::accessors::present(&self.$field)
}
getters! { $($rest)* }
};
($(#[$attr:meta])* list $field:ident: $type:ty => $len:ident, $at:ident; $($rest:tt)*) => {
pub(crate) fn $len(&self) -> usize {
self.$field.len()
}
$(#[$attr])*
pub(crate) fn $at(&self, index: usize) -> &$type {
&self.$field[index]
}
getters! { $($rest)* }
};
(
$(#[$attr:meta])* optional_list $field:ident: $type:ty => $has:ident, $len:ident, $at:ident;
$($rest:tt)*
) => {
pub(crate) fn $has(&self) -> bool {
self.$field.is_some()
}
pub(crate) fn $len(&self) -> usize {
self.$field.as_ref().map_or(0, Vec::len)
}
$(#[$attr])*
pub(crate) fn $at(&self, index: usize) -> &$type {
&$crate::bridge::accessors::present(&self.$field)[index]
}
getters! { $($rest)* }
};
($(#[$attr:meta])* map $field:ident => $len:ident, $key_at:ident, $value_at:ident; $($rest:tt)*) => {
pub(crate) fn $len(&self) -> usize {
self.$field.len()
}
$(#[$attr])*
pub(crate) fn $key_at(&self, index: usize) -> &str {
$crate::bridge::accessors::entry_at(&self.$field, index).0
}
pub(crate) fn $value_at(&self, index: usize) -> &str {
$crate::bridge::accessors::entry_at(&self.$field, index).1
}
getters! { $($rest)* }
};
(
$(#[$attr:meta])* optional_map $field:ident => $has:ident, $len:ident, $key_at:ident, $value_at:ident;
$($rest:tt)*
) => {
pub(crate) fn $has(&self) -> bool {
self.$field.is_some()
}
pub(crate) fn $len(&self) -> usize {
self.$field.as_ref().map_or(0, ::std::collections::BTreeMap::len)
}
$(#[$attr])*
pub(crate) fn $key_at(&self, index: usize) -> &str {
$crate::bridge::accessors::entry_at($crate::bridge::accessors::present(&self.$field), index).0
}
pub(crate) fn $value_at(&self, index: usize) -> &str {
$crate::bridge::accessors::entry_at($crate::bridge::accessors::present(&self.$field), index).1
}
getters! { $($rest)* }
};
}
mod addresses;
mod archive;
mod cloud_hypervisor;
mod container;
#[macro_use]
mod ext4;
mod images;
mod io;
mod os;
mod spec;
use crate::bridge::ffi;
use std::collections::BTreeMap;
use std::path::Path;
fn path(path: &Path) -> Option<String> {
path.to_str().map(str::to_string)
}
fn lossy_path(path: &Path) -> String {
path.display().to_string()
}
fn present<T>(value: &Option<T>) -> &T {
value
.as_ref()
.unwrap_or_else(|| unreachable!("Swift asks for an optional field only after its `has_` getter"))
}
fn entry_at<T>(map: &BTreeMap<String, T>, index: usize) -> (&str, &T) {
map
.iter()
.nth(index)
.map(|(key, value)| (key.as_str(), value))
.unwrap_or_else(|| unreachable!("Swift asks for an entry only below the length"))
}
fn fixed<T, const N: usize>(values: Vec<T>, what: &str) -> [T; N] {
let count = values.len();
values
.try_into()
.unwrap_or_else(|_| unreachable!("Swift sends {N} values for {what}, not {count}"))
}
#[cfg(test)]
fn raw_values<T: Copy>(all: &[T], raw_value: fn(T) -> &'static str) -> Vec<&'static str> {
all.iter().map(|case| raw_value(*case)).collect()
}
impl ffi::CzOutcome {
fn optional<T>(&self, read: impl FnOnce(&Self) -> T) -> Option<T> {
self.is_some().then(|| read(self))
}
fn list<T>(&self, read: impl Fn(&Self) -> T) -> Vec<T> {
(0..self.len()).map(|index| read(&self.at(index))).collect()
}
fn map(&self) -> BTreeMap<String, String> {
self.map_keys().into_iter().zip(self.map_values()).collect()
}
fn map_of<T>(&self, read: impl Fn(&Self) -> T) -> BTreeMap<String, T> {
self
.entry_keys()
.into_iter()
.zip(self.entry_values().list(read))
.collect()
}
pub(crate) fn optional_text(&self) -> Option<String> {
self.optional(Self::text)
}
pub(crate) fn optional_int32(&self) -> Option<i32> {
self.optional(Self::int32)
}
pub(crate) fn int32_map(&self) -> BTreeMap<String, i32> {
self.map_of(Self::int32)
}
}