Add FileSystem Rust module.
This commit is contained in:
53
src/filesystem.rs
Normal file
53
src/filesystem.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
#[cxx_qt::bridge]
|
||||
pub mod qobject {
|
||||
unsafe extern "C++" {
|
||||
// Import Qt Types from C++
|
||||
include!("cxx-qt-lib/qstring.h");
|
||||
type QString = cxx_qt_lib::QString;
|
||||
include!("cxx-qt-lib/qmodelindex.h");
|
||||
type QModelIndex = cxx_qt_lib::QModelIndex;
|
||||
}
|
||||
|
||||
unsafe extern "RustQt" {
|
||||
// Export QML Types from Rust
|
||||
#[qobject]
|
||||
#[qml_element]
|
||||
#[qml_singleton]
|
||||
#[qproperty(QString, file_path, cxx_name = "filePath")]
|
||||
type FileSystem = super::FileSystemImpl;
|
||||
|
||||
#[qinvokable]
|
||||
#[cxx_name = "readFile"]
|
||||
fn read_file(self: &FileSystem, path: &QString) -> QString;
|
||||
}
|
||||
}
|
||||
|
||||
use cxx_qt_lib::{QModelIndex, QString};
|
||||
use std::fs;
|
||||
pub struct FileSystemImpl {
|
||||
file_path: QString,
|
||||
model_index: QModelIndex,
|
||||
}
|
||||
|
||||
// Default is explicit to make the editor open this source file initially.
|
||||
impl Default for FileSystemImpl {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
file_path: QString::from(file!()),
|
||||
model_index: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl qobject::FileSystem {
|
||||
fn read_file(&self, path: &QString) -> QString {
|
||||
if path.is_empty() {
|
||||
return QString::default();
|
||||
}
|
||||
// TODO: What if the file is binary or something horrible?
|
||||
QString::from(
|
||||
fs::read_to_string(path.to_string())
|
||||
.expect(format!("Failed to read file {}", path).as_str()),
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user