summaryrefslogtreecommitdiff
path: root/src/types
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/types/project.rs92
1 files changed, 54 insertions, 38 deletions
diff --git a/src/types/project.rs b/src/types/project.rs
index a9c4430..e1b2d0c 100644
--- a/src/types/project.rs
+++ b/src/types/project.rs
@@ -1,11 +1,10 @@
-use std::path::{Path, PathBuf};
+use std::path::PathBuf;
-use smol::fs;
-
-extern crate fxhash;
use iced::theme::{Base, Mode, Theme};
+use iced::{Task, window};
use rust_format::{Formatter, PostProcess, PrettyPlease};
use serde::{Deserialize, Serialize};
+use smol::fs;
use super::rendered_element::RenderedElement;
use crate::Error;
@@ -47,47 +46,64 @@ impl Project {
Ok((path, project))
}
- pub async fn from_file() -> Result<(PathBuf, Self), Error> {
- let picked_file = rfd::AsyncFileDialog::new()
- .set_title("Open a JSON file...")
- .add_filter("*.json, *.JSON", &["json", "JSON"])
- .pick_file()
- .await
- .ok_or(Error::DialogClosed)?;
-
- let path = picked_file.path().to_owned();
-
- Self::from_path(path).await
+ pub fn from_file() -> Task<Result<(PathBuf, Self), Error>> {
+ window::latest()
+ .and_then(|id| {
+ window::run(id, |window| {
+ rfd::AsyncFileDialog::new()
+ .set_parent(window)
+ .set_title("Open a JSON file...")
+ .add_filter("*.json, *.JSON", &["json", "JSON"])
+ .pick_file()
+ })
+ })
+ .then(Task::future)
+ .map(|picked_file| picked_file.ok_or(Error::DialogClosed))
+ .and_then(|picked_file| {
+ Task::future(Self::from_path(picked_file.path().to_owned()))
+ })
}
- pub async fn write_to_file(
- self,
+ pub fn write_to_file(
+ &self,
path: Option<PathBuf>,
- ) -> Result<PathBuf, Error> {
+ ) -> Task<Result<PathBuf, Error>> {
let path = if let Some(p) = path {
- if let Some(parent) = p.parent()
- && !parent.exists()
- {
- fs::create_dir_all(parent).await?;
- }
-
- p
+ Task::done(Ok(p))
} else {
- rfd::AsyncFileDialog::new()
- .set_title("Save to JSON file...")
- .add_filter("*.json, *.JSON", &["json", "JSON"])
- .save_file()
- .await
- .as_ref()
- .map(rfd::FileHandle::path)
- .map(Path::to_owned)
- .ok_or(Error::DialogClosed)?
+ window::latest()
+ .and_then(|id| {
+ window::run(id, |window| {
+ rfd::AsyncFileDialog::new()
+ .set_parent(window)
+ .set_title("Save to JSON file...")
+ .add_filter("*.json, *.JSON", &["json", "JSON"])
+ .save_file()
+ })
+ })
+ .then(Task::future)
+ .map(|picked_file| picked_file.ok_or(Error::DialogClosed))
+ .and_then(|picked_file| {
+ Task::done(Ok(picked_file.path().to_owned()))
+ })
};
- let contents = serde_json::to_string(&self)?;
- fs::write(&path, contents).await?;
-
- Ok(path)
+ let contents = serde_json::to_string(self).map_err(Error::from);
+
+ path.and_then(move |path| {
+ Task::done(contents.clone().map(|contents| {
+ let path = path.clone();
+
+ async move {
+ fs::write(path.clone(), contents)
+ .await
+ .map(|_| path)
+ .map_err(Error::from)
+ }
+ }))
+ })
+ .and_then(Task::future)
+ .and_then(move |path| Task::done(Ok(path)))
}
pub fn app_code(&mut self, theme_mode: Mode) -> Result<String, Error> {