summaryrefslogtreecommitdiff
path: root/rust/src/project.rs
diff options
context:
space:
mode:
authorMason Reed <mason@vector35.com>2025-05-07 19:22:21 -0400
committerMason Reed <35282038+emesare@users.noreply.github.com>2025-05-12 17:45:24 -0400
commit2f214f6c9935e8ce8df4732cde44a540a003258c (patch)
tree6fe319433ef0d2ad75fcc58a50eaa632bb627ec9 /rust/src/project.rs
parentb4cf0be8816182c9efca037e27e9439482f8bf36 (diff)
[Rust] Reduce usage of `IntoCStr` in function signatures
This is being done to reduce complexity in function signatures, specifically many of the strings we are passing ultimately should be new types themselves instead of "just strings", things such as type ids. Another place which was confusing was dealing with filesystem related APIs, this commit turns most of those params into a stricter `Path` type. This is bringing the rust api more inline with both python and C++, where the wrapper eagerly converts the string into the languages standard string type. Special consideration must be made for symbols or other possible non utf-8 objects. This commit will be followed up with one that adds the `IntoCStr` bound on API's we want to keep as invalid utf-8 so we can for example, get section by name on a section with invalid utf-8.
Diffstat (limited to 'rust/src/project.rs')
-rw-r--r--rust/src/project.rs197
1 files changed, 76 insertions, 121 deletions
diff --git a/rust/src/project.rs b/rust/src/project.rs
index 7b6a263e..8b5f594a 100644
--- a/rust/src/project.rs
+++ b/rust/src/project.rs
@@ -35,21 +35,23 @@ impl Project {
unsafe { Array::new(result, count, ()) }
}
+ // TODO: Path here is actually local path?
/// Create a new project
///
/// * `path` - Path to the project directory (.bnpr)
/// * `name` - Name of the new project
- pub fn create<P: IntoCStr, S: IntoCStr>(path: P, name: S) -> Option<Ref<Self>> {
+ pub fn create(path: &str, name: &str) -> Option<Ref<Self>> {
let path_raw = path.to_cstr();
let name_raw = name.to_cstr();
let handle = unsafe { BNCreateProject(path_raw.as_ptr(), name_raw.as_ptr()) };
NonNull::new(handle).map(|h| unsafe { Self::ref_from_raw(h) })
}
+ // TODO: Path here is actually local path?
/// Open an existing project
///
/// * `path` - Path to the project directory (.bnpr) or project metadata file (.bnpm)
- pub fn open_project<P: IntoCStr>(path: P) -> Option<Ref<Self>> {
+ pub fn open_project(path: &str) -> Option<Ref<Self>> {
let path_raw = path.to_cstr();
let handle = unsafe { BNOpenProject(path_raw.as_ptr()) };
NonNull::new(handle).map(|h| unsafe { Self::ref_from_raw(h) })
@@ -94,7 +96,7 @@ impl Project {
}
/// Set the name of the project
- pub fn set_name<S: IntoCStr>(&self, value: S) {
+ pub fn set_name(&self, value: &str) {
let value = value.to_cstr();
unsafe { BNProjectSetName(self.handle.as_ptr(), value.as_ptr()) }
}
@@ -105,13 +107,13 @@ impl Project {
}
/// Set the description of the project
- pub fn set_description<S: IntoCStr>(&self, value: S) {
+ pub fn set_description(&self, value: &str) {
let value = value.to_cstr();
unsafe { BNProjectSetDescription(self.handle.as_ptr(), value.as_ptr()) }
}
/// Retrieves metadata stored under a key from the project
- pub fn query_metadata<S: IntoCStr>(&self, key: S) -> Ref<Metadata> {
+ pub fn query_metadata(&self, key: &str) -> Ref<Metadata> {
let key = key.to_cstr();
let result = unsafe { BNProjectQueryMetadata(self.handle.as_ptr(), key.as_ptr()) };
unsafe { Metadata::ref_from_raw(result) }
@@ -121,13 +123,13 @@ impl Project {
///
/// * `key` - Key under which to store the Metadata object
/// * `value` - Object to store
- pub fn store_metadata<S: IntoCStr>(&self, key: S, value: &Metadata) -> bool {
+ pub fn store_metadata(&self, key: &str, value: &Metadata) -> bool {
let key_raw = key.to_cstr();
unsafe { BNProjectStoreMetadata(self.handle.as_ptr(), key_raw.as_ptr(), value.handle) }
}
/// Removes the metadata associated with this `key` from the project
- pub fn remove_metadata<S: IntoCStr>(&self, key: S) {
+ pub fn remove_metadata(&self, key: &str) {
let key_raw = key.to_cstr();
unsafe { BNProjectRemoveMetadata(self.handle.as_ptr(), key_raw.as_ptr()) }
}
@@ -141,16 +143,12 @@ impl Project {
/// * `path` - Path to folder on disk
/// * `parent` - Parent folder in the project that will contain the new contents
/// * `description` - Description for created root folder
- pub fn create_folder_from_path<P, D>(
+ pub fn create_folder_from_path(
&self,
- path: P,
+ path: &str,
parent: Option<&ProjectFolder>,
- description: D,
- ) -> Result<Ref<ProjectFolder>, ()>
- where
- P: IntoCStr,
- D: IntoCStr,
- {
+ description: &str,
+ ) -> Result<Ref<ProjectFolder>, ()> {
self.create_folder_from_path_with_progress(path, parent, description, NoProgressCallback)
}
@@ -160,16 +158,14 @@ impl Project {
/// * `parent` - Parent folder in the project that will contain the new contents
/// * `description` - Description for created root folder
/// * `progress` - [`ProgressCallback`] that will be called as the [`ProjectFolder`] is being created
- pub fn create_folder_from_path_with_progress<P, D, PC>(
+ pub fn create_folder_from_path_with_progress<PC>(
&self,
- path: P,
+ path: &str,
parent: Option<&ProjectFolder>,
- description: D,
+ description: &str,
mut progress: PC,
) -> Result<Ref<ProjectFolder>, ()>
where
- P: IntoCStr,
- D: IntoCStr,
PC: ProgressCallback,
{
let path_raw = path.to_cstr();
@@ -194,16 +190,12 @@ impl Project {
/// * `parent` - Parent folder in the project that will contain the new folder
/// * `name` - Name for the created folder
/// * `description` - Description for created folder
- pub fn create_folder<N, D>(
+ pub fn create_folder(
&self,
parent: Option<&ProjectFolder>,
- name: N,
- description: D,
- ) -> Result<Ref<ProjectFolder>, ()>
- where
- N: IntoCStr,
- D: IntoCStr,
- {
+ name: &str,
+ description: &str,
+ ) -> Result<Ref<ProjectFolder>, ()> {
let name_raw = name.to_cstr();
let description_raw = description.to_cstr();
let parent_ptr = parent.map(|p| p.handle.as_ptr()).unwrap_or(null_mut());
@@ -224,18 +216,13 @@ impl Project {
/// * `name` - Name for the created folder
/// * `description` - Description for created folder
/// * `id` - id unique ID
- pub unsafe fn create_folder_unsafe<N, D, I>(
+ pub unsafe fn create_folder_unsafe(
&self,
parent: Option<&ProjectFolder>,
- name: N,
- description: D,
- id: I,
- ) -> Result<Ref<ProjectFolder>, ()>
- where
- N: IntoCStr,
- D: IntoCStr,
- I: IntoCStr,
- {
+ name: &str,
+ description: &str,
+ id: &str,
+ ) -> Result<Ref<ProjectFolder>, ()> {
let name_raw = name.to_cstr();
let description_raw = description.to_cstr();
let parent_ptr = parent.map(|p| p.handle.as_ptr()).unwrap_or(null_mut());
@@ -264,7 +251,7 @@ impl Project {
}
/// Retrieve a folder in the project by unique folder `id`
- pub fn folder_by_id<S: IntoCStr>(&self, id: S) -> Option<Ref<ProjectFolder>> {
+ pub fn folder_by_id(&self, id: &str) -> Option<Ref<ProjectFolder>> {
let raw_id = id.to_cstr();
let result = unsafe { BNProjectGetFolderById(self.handle.as_ptr(), raw_id.as_ptr()) };
let handle = NonNull::new(result)?;
@@ -282,17 +269,17 @@ impl Project {
///
/// * `folder` - [`ProjectFolder`] to delete recursively
/// * `progress` - [`ProgressCallback`] that will be called as objects get deleted
- pub fn delete_folder_with_progress<P: ProgressCallback>(
+ pub fn delete_folder_with_progress<PC: ProgressCallback>(
&self,
folder: &ProjectFolder,
- mut progress: P,
+ mut progress: PC,
) -> Result<(), ()> {
let result = unsafe {
BNProjectDeleteFolder(
self.handle.as_ptr(),
folder.handle.as_ptr(),
- &mut progress as *mut P as *mut c_void,
- Some(P::cb_progress_callback),
+ &mut progress as *mut PC as *mut c_void,
+ Some(PC::cb_progress_callback),
)
};
@@ -313,18 +300,13 @@ impl Project {
/// * `folder` - Folder to place the created file in
/// * `name` - Name to assign to the created file
/// * `description` - Description to assign to the created file
- pub fn create_file_from_path<P, N, D>(
+ pub fn create_file_from_path(
&self,
- path: P,
+ path: &str,
folder: Option<&ProjectFolder>,
- name: N,
- description: D,
- ) -> Result<Ref<ProjectFile>, ()>
- where
- P: IntoCStr,
- N: IntoCStr,
- D: IntoCStr,
- {
+ name: &str,
+ description: &str,
+ ) -> Result<Ref<ProjectFile>, ()> {
self.create_file_from_path_with_progress(
path,
folder,
@@ -341,18 +323,15 @@ impl Project {
/// * `name` - Name to assign to the created file
/// * `description` - Description to assign to the created file
/// * `progress` - [`ProgressCallback`] that will be called as the [`ProjectFile`] is being added
- pub fn create_file_from_path_with_progress<P, N, D, PC>(
+ pub fn create_file_from_path_with_progress<PC>(
&self,
- path: P,
+ path: &str,
folder: Option<&ProjectFolder>,
- name: N,
- description: D,
+ name: &str,
+ description: &str,
mut progress: PC,
) -> Result<Ref<ProjectFile>, ()>
where
- P: IntoCStr,
- N: IntoCStr,
- D: IntoCStr,
PC: ProgressCallback,
{
let path_raw = path.to_cstr();
@@ -382,21 +361,15 @@ impl Project {
/// * `description` - Description to assign to the created file
/// * `id` - id unique ID
/// * `creation_time` - Creation time of the file
- pub unsafe fn create_file_from_path_unsafe<P, N, D, I>(
+ pub unsafe fn create_file_from_path_unsafe(
&self,
- path: P,
+ path: &str,
folder: Option<&ProjectFolder>,
- name: N,
- description: D,
- id: I,
+ name: &str,
+ description: &str,
+ id: &str,
creation_time: SystemTime,
- ) -> Result<Ref<ProjectFile>, ()>
- where
- P: IntoCStr,
- N: IntoCStr,
- D: IntoCStr,
- I: IntoCStr,
- {
+ ) -> Result<Ref<ProjectFile>, ()> {
self.create_file_from_path_unsafe_with_progress(
path,
folder,
@@ -418,21 +391,17 @@ impl Project {
/// * `creation_time` - Creation time of the file
/// * `progress` - [`ProgressCallback`] that will be called as the [`ProjectFile`] is being created
#[allow(clippy::too_many_arguments)]
- pub unsafe fn create_file_from_path_unsafe_with_progress<P, N, D, I, PC>(
+ pub unsafe fn create_file_from_path_unsafe_with_progress<PC>(
&self,
- path: P,
+ path: &str,
folder: Option<&ProjectFolder>,
- name: N,
- description: D,
- id: I,
+ name: &str,
+ description: &str,
+ id: &str,
creation_time: SystemTime,
mut progress: PC,
) -> Result<Ref<ProjectFile>, ()>
where
- P: IntoCStr,
- N: IntoCStr,
- D: IntoCStr,
- I: IntoCStr,
PC: ProgressCallback,
{
let path_raw = path.to_cstr();
@@ -463,17 +432,13 @@ impl Project {
/// * `folder` - Folder to place the created file in
/// * `name` - Name to assign to the created file
/// * `description` - Description to assign to the created file
- pub fn create_file<N, D>(
+ pub fn create_file(
&self,
contents: &[u8],
folder: Option<&ProjectFolder>,
- name: N,
- description: D,
- ) -> Result<Ref<ProjectFile>, ()>
- where
- N: IntoCStr,
- D: IntoCStr,
- {
+ name: &str,
+ description: &str,
+ ) -> Result<Ref<ProjectFile>, ()> {
self.create_file_with_progress(contents, folder, name, description, NoProgressCallback)
}
@@ -484,18 +449,16 @@ impl Project {
/// * `name` - Name to assign to the created file
/// * `description` - Description to assign to the created file
/// * `progress` - [`ProgressCallback`] that will be called as the [`ProjectFile`] is being created
- pub fn create_file_with_progress<N, D, P>(
+ pub fn create_file_with_progress<PC>(
&self,
contents: &[u8],
folder: Option<&ProjectFolder>,
- name: N,
- description: D,
- mut progress: P,
+ name: &str,
+ description: &str,
+ mut progress: PC,
) -> Result<Ref<ProjectFile>, ()>
where
- N: IntoCStr,
- D: IntoCStr,
- P: ProgressCallback,
+ PC: ProgressCallback,
{
let name_raw = name.to_cstr();
let description_raw = description.to_cstr();
@@ -509,8 +472,8 @@ impl Project {
folder_ptr,
name_raw.as_ptr(),
description_raw.as_ptr(),
- &mut progress as *mut P as *mut c_void,
- Some(P::cb_progress_callback),
+ &mut progress as *mut PC as *mut c_void,
+ Some(PC::cb_progress_callback),
);
Ok(ProjectFile::ref_from_raw(NonNull::new(result).ok_or(())?))
}
@@ -524,20 +487,15 @@ impl Project {
/// * `description` - Description to assign to the created file
/// * `id` - id unique ID
/// * `creation_time` - Creation time of the file
- pub unsafe fn create_file_unsafe<N, D, I>(
+ pub unsafe fn create_file_unsafe(
&self,
contents: &[u8],
folder: Option<&ProjectFolder>,
- name: N,
- description: D,
- id: I,
+ name: &str,
+ description: &str,
+ id: &str,
creation_time: SystemTime,
- ) -> Result<Ref<ProjectFile>, ()>
- where
- N: IntoCStr,
- D: IntoCStr,
- I: IntoCStr,
- {
+ ) -> Result<Ref<ProjectFile>, ()> {
self.create_file_unsafe_with_progress(
contents,
folder,
@@ -559,21 +517,18 @@ impl Project {
/// * `creation_time` - Creation time of the file
/// * `progress` - [`ProgressCallback`] that will be called as the [`ProjectFile`] is being created
#[allow(clippy::too_many_arguments)]
- pub unsafe fn create_file_unsafe_with_progress<N, D, I, P>(
+ pub unsafe fn create_file_unsafe_with_progress<PC>(
&self,
contents: &[u8],
folder: Option<&ProjectFolder>,
- name: N,
- description: D,
- id: I,
+ name: &str,
+ description: &str,
+ id: &str,
creation_time: SystemTime,
- mut progress: P,
+ mut progress: PC,
) -> Result<Ref<ProjectFile>, ()>
where
- N: IntoCStr,
- D: IntoCStr,
- I: IntoCStr,
- P: ProgressCallback,
+ PC: ProgressCallback,
{
let name_raw = name.to_cstr();
let description_raw = description.to_cstr();
@@ -590,8 +545,8 @@ impl Project {
description_raw.as_ptr(),
id_raw.as_ptr(),
systime_to_bntime(creation_time).unwrap(),
- &mut progress as *mut P as *mut c_void,
- Some(P::cb_progress_callback),
+ &mut progress as *mut PC as *mut c_void,
+ Some(PC::cb_progress_callback),
);
Ok(ProjectFile::ref_from_raw(NonNull::new(result).ok_or(())?))
}
@@ -606,7 +561,7 @@ impl Project {
}
/// Retrieve a file in the project by unique `id`
- pub fn file_by_id<S: IntoCStr>(&self, id: S) -> Option<Ref<ProjectFile>> {
+ pub fn file_by_id(&self, id: &str) -> Option<Ref<ProjectFile>> {
let raw_id = id.to_cstr();
let result = unsafe { BNProjectGetFileById(self.handle.as_ptr(), raw_id.as_ptr()) };
let handle = NonNull::new(result)?;
@@ -614,7 +569,7 @@ impl Project {
}
/// Retrieve a file in the project by the `path` on disk
- pub fn file_by_path<S: IntoCStr>(&self, path: S) -> Option<Ref<ProjectFile>> {
+ pub fn file_by_path(&self, path: &str) -> Option<Ref<ProjectFile>> {
let path_raw = path.to_cstr();
let result =
unsafe { BNProjectGetFileByPathOnDisk(self.handle.as_ptr(), path_raw.as_ptr()) };