Struct File

Source
pub struct File<'a> { /* private fields */ }
Expand description

The root of a GVDB file

§Examples

Load a GResource file from disk

use std::path::PathBuf;
use serde::Deserialize;
use gvdb::read::File;

let path = PathBuf::from("test-data/test3.gresource");
let file = File::from_file(&path).unwrap();
let table = file.hash_table().unwrap();

#[derive(serde::Deserialize, zvariant::Type, zvariant::OwnedValue)]
struct SvgData {
    size: u32,
    flags: u32,
    content: Vec<u8>
}

let svg: SvgData = table
    .get("/gvdb/rs/test/online-symbolic.svg")
    .unwrap();
let svg_str = std::str::from_utf8(&svg.content).unwrap();

println!("{}", svg_str);

Query the root hash table

use gvdb::read::File;

fn query_hash_table(file: File) {
    let table = file.hash_table().unwrap();

    let mut keys = table.keys();
    assert_eq!(keys.len(), 2);
    assert_matches!(keys.next().unwrap().as_deref(), Ok("string"));
    assert_matches!(keys.next().unwrap().as_deref(), Ok("table"));

    let str_value: String = table.get("string").unwrap();
    assert_eq!(str_value, "test string");

    let mut items = table.values().collect::<Result<Vec<_>, _>>().unwrap();
    assert_eq!(items.len(), 2);
    assert_eq!(String::try_from(&items[0]).unwrap(), "test string");

    let sub_table = table.get_hash_table("table").unwrap();
    let mut sub_table_keys = sub_table.keys().collect::<Result<Vec<_>, _>>().unwrap();
    assert_eq!(sub_table_keys.len(), 1);
    assert_eq!(sub_table_keys[0], "int");

    let int_value: u32 = sub_table.get("int").unwrap();
    assert_eq!(int_value, 42);
}

Implementations§

Source§

impl<'a> File<'a>

Source

pub fn hash_table(&self) -> Result<HashTable<'_, '_>>

Returns the root hash table of the file

Source

pub fn from_bytes(bytes: Cow<'a, [u8]>) -> Result<Self>

Interpret a slice of bytes as a GVDB file

Source

pub fn from_file(filename: &Path) -> Result<Self>

Open a file and interpret the data as GVDB

let path = std::path::PathBuf::from("test-data/test3.gresource");
let file = gvdb::read::File::from_file(&path).unwrap();
Source

pub unsafe fn from_file_mmap(filename: &Path) -> Result<Self>

Open a file and mmap it into memory.

§Safety

This is marked unsafe as the file could be modified on-disk while the mmap is active. This will cause undefined behavior. You must make sure to employ your own locking and to reload the file yourself when any modification occurs.

Trait Implementations§

Source§

impl Debug for File<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for File<'a>

§

impl<'a> RefUnwindSafe for File<'a>

§

impl<'a> Send for File<'a>

§

impl<'a> Sync for File<'a>

§

impl<'a> Unpin for File<'a>

§

impl<'a> UnwindSafe for File<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.