Skip to content

Commit

Permalink
Add FileName type
Browse files Browse the repository at this point in the history
  • Loading branch information
A6GibKm committed Jan 23, 2023
1 parent e575e04 commit 175a758
Showing 1 changed file with 44 additions and 4 deletions.
48 changes: 44 additions & 4 deletions src/backend/file_chooser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use async_std::sync::Mutex;
use std::ffi::{CString, OsStr};
use std::os::unix::ffi::OsStrExt;
use std::path::Path;
use std::sync::Arc;

use async_trait::async_trait;
Expand All @@ -7,6 +10,7 @@ use futures_channel::{
oneshot,
};
use futures_util::SinkExt;
use serde::Deserialize;
use zbus::dbus_interface;

use crate::{
Expand All @@ -22,6 +26,42 @@ use crate::{
AppID, WindowIdentifierType,
};

// FIXME Manually implement Deserialize to catch non null-terminated arrays.
// FIXME Use OsString directly once
// https://gitlab.freedesktop.org/dbus/zbus/-/merge_requests/638 is merged.
/// A file name represented as a null-terminated byte array.
///
/// Implements `AsRef<Path>`.
#[derive(Debug, Default)]
pub struct FileName(CString);

impl AsRef<Path> for FileName {
fn as_ref(&self) -> &Path {
let os_str = OsStr::from_bytes(self.0.as_bytes());

Path::new(os_str)
}
}

impl Type for FileName {
fn signature() -> zbus::zvariant::Signature<'static> {
<&[u8]>::signature()
}
}

impl<'de> Deserialize<'de> for FileName {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let bytes = <Vec<u8>>::deserialize(deserializer)?;
let c_string = CString::from_vec_with_nul(bytes)
.map_err(|_| serde::de::Error::custom("Bytes are not null-terminated"))?;

Ok(Self(c_string))
}
}

// Does not coincide with the one in desktop/file_chooser.rs
#[derive(DeserializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
Expand All @@ -46,8 +86,8 @@ pub struct SaveFileOptions {
pub current_filter: Option<FileFilter>,
pub choices: Option<Vec<Choice>>,
pub current_name: Option<String>,
pub current_folder: Option<Vec<u8>>,
pub current_file: Option<Vec<u8>>,
pub current_folder: Option<FileName>,
pub current_file: Option<FileName>,
}

#[derive(DeserializeDict, Type, Debug, Default)]
Expand All @@ -59,8 +99,8 @@ pub struct SaveFilesOptions {
pub accept_label: Option<String>,
pub modal: Option<bool>,
pub choices: Option<Vec<Choice>>,
pub current_folder: Option<Vec<u8>>,
pub files: Option<Vec<Vec<u8>>>,
pub current_folder: Option<FileName>,
pub files: Option<Vec<FileName>>,
}

#[derive(DeserializeDict, SerializeDict, Type, Debug, Default)]
Expand Down

0 comments on commit 175a758

Please sign in to comment.