From 040976497749aaa02d54e125ce2b34300213bfb0 Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Tue, 9 Dec 2025 16:51:14 -0500 Subject: [Rust] New type for `TypeArchiveId` Prevents type confusions considering there are at times, three different id types being referred to in the type archive API --- rust/src/type_archive.rs | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) (limited to 'rust/src') diff --git a/rust/src/type_archive.rs b/rust/src/type_archive.rs index 5f7ffb84..4a047205 100644 --- a/rust/src/type_archive.rs +++ b/rust/src/type_archive.rs @@ -14,6 +14,16 @@ use crate::string::{raw_to_string, BnString, IntoCStr}; use crate::type_container::TypeContainer; use crate::types::{QualifiedName, QualifiedNameAndType, QualifiedNameTypeAndId, Type}; +#[repr(transparent)] +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct TypeArchiveId(pub String); + +impl Display for TypeArchiveId { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.write_fmt(format_args!("{}", self.0)) + } +} + #[repr(transparent)] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct TypeArchiveSnapshotId(pub String); @@ -79,24 +89,24 @@ impl TypeArchive { NonNull::new(handle).map(|handle| unsafe { TypeArchive::ref_from_raw(handle) }) } - /// Create a Type Archive at the given path and id, returning None if it could not be created. + /// Create a Type Archive at the given path and id, returning `None` if it could not be created. /// /// If the file has already been created and is not a valid type archive this will return `None`. pub fn create_with_id( path: impl AsRef, - id: &str, + id: &TypeArchiveId, platform: &Platform, ) -> Option> { let raw_path = path.as_ref().to_cstr(); - let id = id.to_cstr(); + let id = id.0.as_str().to_cstr(); let handle = unsafe { BNCreateTypeArchiveWithId(raw_path.as_ptr(), platform.handle, id.as_ptr()) }; NonNull::new(handle).map(|handle| unsafe { TypeArchive::ref_from_raw(handle) }) } /// Get a reference to the Type Archive with the known id, if one exists. - pub fn lookup_by_id(id: &str) -> Option> { - let id = id.to_cstr(); + pub fn lookup_by_id(id: &TypeArchiveId) -> Option> { + let id = id.0.as_str().to_cstr(); let handle = unsafe { BNLookupTypeArchiveById(id.as_ptr()) }; NonNull::new(handle).map(|handle| unsafe { TypeArchive::ref_from_raw(handle) }) } @@ -110,10 +120,11 @@ impl TypeArchive { } /// Get the guid for a Type Archive - pub fn id(&self) -> BnString { + pub fn id(&self) -> TypeArchiveId { let result = unsafe { BNGetTypeArchiveId(self.handle.as_ptr()) }; assert!(!result.is_null()); - unsafe { BnString::from_raw(result) } + let result_str = unsafe { BnString::from_raw(result) }; + TypeArchiveId(result_str.to_string_lossy().to_string()) } /// Get the associated Platform for a Type Archive @@ -711,9 +722,9 @@ impl TypeArchive { /// conflicting type ids pub fn merge_snapshots( &self, - base_snapshot: &str, - first_snapshot: &str, - second_snapshot: &str, + base_snapshot: &TypeArchiveSnapshotId, + first_snapshot: &TypeArchiveSnapshotId, + second_snapshot: &TypeArchiveSnapshotId, merge_conflicts: M, ) -> Result> where @@ -740,9 +751,9 @@ impl TypeArchive { /// conflicting type ids pub fn merge_snapshots_with_progress( &self, - base_snapshot: &str, - first_snapshot: &str, - second_snapshot: &str, + base_snapshot: &TypeArchiveSnapshotId, + first_snapshot: &TypeArchiveSnapshotId, + second_snapshot: &TypeArchiveSnapshotId, merge_conflicts: M, mut progress: PC, ) -> Result> @@ -750,9 +761,9 @@ impl TypeArchive { M: IntoIterator, PC: ProgressCallback, { - let base_snapshot = base_snapshot.to_cstr(); - let first_snapshot = first_snapshot.to_cstr(); - let second_snapshot = second_snapshot.to_cstr(); + let base_snapshot = base_snapshot.0.as_str().to_cstr(); + let first_snapshot = first_snapshot.0.as_str().to_cstr(); + let second_snapshot = second_snapshot.0.as_str().to_cstr(); let (merge_keys, merge_values): (Vec, Vec) = merge_conflicts .into_iter() .map(|(k, v)| (BnString::new(k), BnString::new(v))) -- cgit v1.3.1