use crate::types::StringView;
use polyplug_utils::BundleId;
#[repr(u32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReloadPhaseType {
Preparing = 0,
Reloaded = 1,
Failed = 2,
Unloading = 3,
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct ReloadPhase {
pub phase_type: ReloadPhaseType,
pub bundle_id: BundleId,
pub bundle_name: StringView,
pub reason: StringView,
}
impl ReloadPhase {
pub fn preparing(bundle_id: BundleId, bundle_name: StringView) -> Self {
Self {
phase_type: ReloadPhaseType::Preparing,
bundle_id,
bundle_name,
reason: StringView::null(),
}
}
pub fn reloaded(bundle_id: BundleId, bundle_name: StringView) -> Self {
Self {
phase_type: ReloadPhaseType::Reloaded,
bundle_id,
bundle_name,
reason: StringView::null(),
}
}
pub fn failed(bundle_id: BundleId, bundle_name: StringView, reason: StringView) -> Self {
Self {
phase_type: ReloadPhaseType::Failed,
bundle_id,
bundle_name,
reason,
}
}
pub fn unloading(bundle_id: BundleId, bundle_name: StringView) -> Self {
Self {
phase_type: ReloadPhaseType::Unloading,
bundle_id,
bundle_name,
reason: StringView::null(),
}
}
}
#[cfg(test)]
mod tests {
use super::{ReloadPhase, ReloadPhaseType};
use crate::types::StringView;
use core::mem::{align_of, size_of};
use polyplug_utils::BundleId;
#[test]
fn layout_reload_phase() {
assert_eq!(size_of::<ReloadPhase>(), 48);
assert_eq!(align_of::<ReloadPhase>(), 8);
}
#[test]
fn reload_phase_type_values() {
assert_eq!(ReloadPhaseType::Preparing as u32, 0);
assert_eq!(ReloadPhaseType::Reloaded as u32, 1);
assert_eq!(ReloadPhaseType::Failed as u32, 2);
assert_eq!(ReloadPhaseType::Unloading as u32, 3);
}
#[test]
fn preparing_constructor() {
let bundle_id = BundleId::new("test-bundle");
let bundle_name = StringView::from_static(b"test_bundle");
let phase = ReloadPhase::preparing(bundle_id, bundle_name);
assert_eq!(phase.phase_type, ReloadPhaseType::Preparing);
assert_eq!(phase.bundle_id, bundle_id);
assert!(phase.reason.ptr.is_null());
assert_eq!(phase.reason.len, 0);
}
#[test]
fn reloaded_constructor() {
let bundle_id = BundleId::new("test-bundle");
let bundle_name = StringView::from_static(b"test_bundle");
let phase = ReloadPhase::reloaded(bundle_id, bundle_name);
assert_eq!(phase.phase_type, ReloadPhaseType::Reloaded);
assert_eq!(phase.bundle_id, bundle_id);
assert!(phase.reason.ptr.is_null());
assert_eq!(phase.reason.len, 0);
}
#[test]
fn unloading_constructor() {
let bundle_id = BundleId::new("test-bundle");
let bundle_name = StringView::from_static(b"test_bundle");
let phase = ReloadPhase::unloading(bundle_id, bundle_name);
assert_eq!(phase.phase_type, ReloadPhaseType::Unloading);
assert_eq!(phase.bundle_id, bundle_id);
assert!(phase.reason.ptr.is_null());
assert_eq!(phase.reason.len, 0);
}
#[test]
fn failed_constructor() {
let bundle_id = BundleId::new("test-bundle");
let bundle_name = StringView::from_static(b"test_bundle");
let reason = StringView::from_static(b"init failed");
let phase = ReloadPhase::failed(bundle_id, bundle_name, reason);
assert_eq!(phase.phase_type, ReloadPhaseType::Failed);
assert_eq!(phase.bundle_id, bundle_id);
assert!(!reason.ptr.is_null());
assert_eq!(phase.reason.len, 11);
}
}