mirror of
https://github.com/ostreedev/ostree.git
synced 2025-02-27 01:57:49 +03:00
collection_ref: add accessors for collection_id and ref_name
This commit is contained in:
parent
91dc916615
commit
faef3562b8
80
rust-bindings/rust/src/collection_ref.rs
Normal file
80
rust-bindings/rust/src/collection_ref.rs
Normal file
@ -0,0 +1,80 @@
|
||||
use glib::translate::ToGlibPtr;
|
||||
use std::ffi::CStr;
|
||||
use CollectionRef;
|
||||
|
||||
trait AsNonnullPtr
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
fn as_nonnull_ptr(&self) -> Option<Self>;
|
||||
}
|
||||
|
||||
impl<T> AsNonnullPtr for *mut T {
|
||||
fn as_nonnull_ptr(&self) -> Option<Self> {
|
||||
if self.is_null() {
|
||||
None
|
||||
} else {
|
||||
Some(*self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: these methods are unsound if you change the underlying OstreeCollectionRef from C. Is that
|
||||
// a problem?
|
||||
impl CollectionRef {
|
||||
/// Get the collection ID from this `CollectionRef`.
|
||||
///
|
||||
/// # Returns
|
||||
/// Since the value may not be valid UTF-8, `&CStr` is returned. You can safely turn it into a
|
||||
/// `&str` using the `as_str` method.
|
||||
///
|
||||
/// If no collection ID was set in the `CollectionRef`, `None` is returned.
|
||||
pub fn collection_id(&self) -> Option<&CStr> {
|
||||
let inner = self.to_glib_none();
|
||||
unsafe {
|
||||
(*inner.0)
|
||||
.collection_id
|
||||
.as_nonnull_ptr()
|
||||
.map(|ptr| CStr::from_ptr(ptr))
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the ref name from this `CollectionRef`.
|
||||
///
|
||||
/// # Returns
|
||||
/// Since the value may not be valid UTF-8, `&CStr` is returned. You can safely turn it into a
|
||||
/// `&str` using the `as_str` method.
|
||||
pub fn ref_name(&self) -> &CStr {
|
||||
let inner = self.to_glib_none();
|
||||
unsafe { CStr::from_ptr((*inner.0).ref_name) }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn should_get_collection_id() {
|
||||
let collection_ref = CollectionRef::new(Some("collection.id"), "ref").unwrap();
|
||||
let id = collection_ref.collection_id().unwrap().to_str().unwrap();
|
||||
|
||||
assert_eq!(id, "collection.id");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_get_none_collection_id() {
|
||||
let collection_ref = CollectionRef::new(None, "ref").unwrap();
|
||||
let id = collection_ref.collection_id();
|
||||
|
||||
assert_eq!(id, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_get_ref_name() {
|
||||
let collection_ref = CollectionRef::new(Some("collection.id"), "ref-name").unwrap();
|
||||
let ref_name = collection_ref.ref_name().to_str().unwrap();
|
||||
|
||||
assert_eq!(ref_name, "ref-name");
|
||||
}
|
||||
}
|
@ -29,8 +29,12 @@ pub use crate::auto::functions::*;
|
||||
pub use crate::auto::*;
|
||||
|
||||
// handwritten code
|
||||
#[cfg(any(feature = "v2018_6", feature = "dox"))]
|
||||
mod collection_ref;
|
||||
mod object_name;
|
||||
mod repo;
|
||||
#[cfg(any(feature = "v2018_6", feature = "dox"))]
|
||||
pub use crate::collection_ref::*;
|
||||
pub use crate::object_name::*;
|
||||
pub use crate::repo::*;
|
||||
|
||||
|
@ -99,29 +99,3 @@ fn should_checkout_tree() {
|
||||
let testfile_contents = std::fs::read_to_string(testfile_path).expect("test file");
|
||||
assert_eq!("test\n", testfile_contents);
|
||||
}
|
||||
|
||||
// TODO: figure this out and turn it back on
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn should_error_safely_when_passing_empty_file_info_to_checkout_tree() {
|
||||
let test_repo = TestRepo::new();
|
||||
let _ = test_repo.test_commit("test");
|
||||
|
||||
let file = test_repo
|
||||
.repo
|
||||
.read_commit("test", NONE_CANCELLABLE)
|
||||
.expect("read commit")
|
||||
.0
|
||||
.downcast::<ostree::RepoFile>()
|
||||
.expect("RepoFile");
|
||||
let result = test_repo.repo.checkout_tree(
|
||||
ostree::RepoCheckoutMode::User,
|
||||
ostree::RepoCheckoutOverwriteMode::None,
|
||||
&gio::File::new_for_path("/"),
|
||||
&file,
|
||||
&gio::FileInfo::new(),
|
||||
NONE_CANCELLABLE,
|
||||
);
|
||||
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user