Skip to content

Commit 32b0a29

Browse files
committed
[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
1 parent 89b3f7e commit 32b0a29

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

rust/src/type_archive.rs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ use crate::string::{raw_to_string, BnString, IntoCStr};
1414
use crate::type_container::TypeContainer;
1515
use crate::types::{QualifiedName, QualifiedNameAndType, QualifiedNameTypeAndId, Type};
1616

17+
#[repr(transparent)]
18+
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
19+
pub struct TypeArchiveId(pub String);
20+
21+
impl Display for TypeArchiveId {
22+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
23+
f.write_fmt(format_args!("{}", self.0))
24+
}
25+
}
26+
1727
#[repr(transparent)]
1828
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
1929
pub struct TypeArchiveSnapshotId(pub String);
@@ -79,24 +89,24 @@ impl TypeArchive {
7989
NonNull::new(handle).map(|handle| unsafe { TypeArchive::ref_from_raw(handle) })
8090
}
8191

82-
/// Create a Type Archive at the given path and id, returning None if it could not be created.
92+
/// Create a Type Archive at the given path and id, returning `None` if it could not be created.
8393
///
8494
/// If the file has already been created and is not a valid type archive this will return `None`.
8595
pub fn create_with_id(
8696
path: impl AsRef<Path>,
87-
id: &str,
97+
id: &TypeArchiveId,
8898
platform: &Platform,
8999
) -> Option<Ref<TypeArchive>> {
90100
let raw_path = path.as_ref().to_cstr();
91-
let id = id.to_cstr();
101+
let id = id.0.as_str().to_cstr();
92102
let handle =
93103
unsafe { BNCreateTypeArchiveWithId(raw_path.as_ptr(), platform.handle, id.as_ptr()) };
94104
NonNull::new(handle).map(|handle| unsafe { TypeArchive::ref_from_raw(handle) })
95105
}
96106

97107
/// Get a reference to the Type Archive with the known id, if one exists.
98-
pub fn lookup_by_id(id: &str) -> Option<Ref<TypeArchive>> {
99-
let id = id.to_cstr();
108+
pub fn lookup_by_id(id: &TypeArchiveId) -> Option<Ref<TypeArchive>> {
109+
let id = id.0.as_str().to_cstr();
100110
let handle = unsafe { BNLookupTypeArchiveById(id.as_ptr()) };
101111
NonNull::new(handle).map(|handle| unsafe { TypeArchive::ref_from_raw(handle) })
102112
}
@@ -110,10 +120,11 @@ impl TypeArchive {
110120
}
111121

112122
/// Get the guid for a Type Archive
113-
pub fn id(&self) -> BnString {
123+
pub fn id(&self) -> TypeArchiveId {
114124
let result = unsafe { BNGetTypeArchiveId(self.handle.as_ptr()) };
115125
assert!(!result.is_null());
116-
unsafe { BnString::from_raw(result) }
126+
let result_str = unsafe { BnString::from_raw(result) };
127+
TypeArchiveId(result_str.to_string_lossy().to_string())
117128
}
118129

119130
/// Get the associated Platform for a Type Archive
@@ -711,9 +722,9 @@ impl TypeArchive {
711722
/// conflicting type ids
712723
pub fn merge_snapshots<M>(
713724
&self,
714-
base_snapshot: &str,
715-
first_snapshot: &str,
716-
second_snapshot: &str,
725+
base_snapshot: &TypeArchiveSnapshotId,
726+
first_snapshot: &TypeArchiveSnapshotId,
727+
second_snapshot: &TypeArchiveSnapshotId,
717728
merge_conflicts: M,
718729
) -> Result<BnString, Array<BnString>>
719730
where
@@ -740,19 +751,19 @@ impl TypeArchive {
740751
/// conflicting type ids
741752
pub fn merge_snapshots_with_progress<M, PC>(
742753
&self,
743-
base_snapshot: &str,
744-
first_snapshot: &str,
745-
second_snapshot: &str,
754+
base_snapshot: &TypeArchiveSnapshotId,
755+
first_snapshot: &TypeArchiveSnapshotId,
756+
second_snapshot: &TypeArchiveSnapshotId,
746757
merge_conflicts: M,
747758
mut progress: PC,
748759
) -> Result<BnString, Array<BnString>>
749760
where
750761
M: IntoIterator<Item = (String, String)>,
751762
PC: ProgressCallback,
752763
{
753-
let base_snapshot = base_snapshot.to_cstr();
754-
let first_snapshot = first_snapshot.to_cstr();
755-
let second_snapshot = second_snapshot.to_cstr();
764+
let base_snapshot = base_snapshot.0.as_str().to_cstr();
765+
let first_snapshot = first_snapshot.0.as_str().to_cstr();
766+
let second_snapshot = second_snapshot.0.as_str().to_cstr();
756767
let (merge_keys, merge_values): (Vec<BnString>, Vec<BnString>) = merge_conflicts
757768
.into_iter()
758769
.map(|(k, v)| (BnString::new(k), BnString::new(v)))

rust/tests/type_archive.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ fn test_create_archive() {
2323
.expect("Found test type");
2424
assert_eq!(test_type.width(), 7);
2525
assert_eq!(test_type.type_class(), TypeClass::IntegerTypeClass);
26+
27+
let lookup_type_archive =
28+
TypeArchive::lookup_by_id(&type_archive.id()).expect("Failed to lookup type archive");
29+
assert_eq!(lookup_type_archive, type_archive);
2630
}

0 commit comments

Comments
 (0)