summaryrefslogtreecommitdiff
path: root/iced_builder/src/lib.rs
blob: 971e0e3d9268fb6780da086ad24f4aead00c26ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
pub mod codegen;
pub mod types;

use std::path::PathBuf;

use iced::widget::{pane_grid, text_editor};
use types::{project::Project, rendered_element::RenderedElement, DesignerPage};

#[derive(Debug, Clone)]
pub enum Error {
    IOError(std::io::ErrorKind),
    SerdeError(String),
    FormatError(String),
    DialogClosed,
    String(String),
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::SerdeError(string) | Self::FormatError(string) | Self::String(string) => {
                write!(f, "{}", string)
            }
            Self::IOError(kind) => {
                write!(f, "{}", kind)
            }
            Self::DialogClosed => {
                write!(
                    f,
                    "The file dialog has been closed without selecting a valid option."
                )
            }
        }
    }
}

impl From<std::io::Error> for Error {
    fn from(value: std::io::Error) -> Self {
        Self::IOError(value.kind())
    }
}

impl From<serde_json::Error> for Error {
    fn from(value: serde_json::Error) -> Self {
        Self::SerdeError(value.to_string())
    }
}

impl From<rust_format::Error> for Error {
    fn from(value: rust_format::Error) -> Self {
        Self::FormatError(value.to_string())
    }
}

impl From<&'static str> for Error {
    fn from(value: &'static str) -> Self {
        Self::String(value.to_owned())
    }
}

#[derive(Debug, Clone)]
pub enum Message {
    ToggleTheme,
    CopyCode,
    SwitchPage(DesignerPage),
    EditorAction(text_editor::Action),
    DropNewElement(types::ElementName, iced::Point, iced::Rectangle),
    HandleNew(
        types::ElementName,
        Vec<(iced::advanced::widget::Id, iced::Rectangle)>,
    ),
    MoveElement(RenderedElement, iced::Point, iced::Rectangle),
    HandleMove(
        RenderedElement,
        Vec<(iced::advanced::widget::Id, iced::Rectangle)>,
    ),
    Resized(pane_grid::ResizeEvent),
    Clicked(pane_grid::Pane),
    PaneDragged(pane_grid::DragEvent),
    NewFile,
    OpenFile,
    FileOpened(Result<(PathBuf, Project), Error>),
    SaveFile,
    FileSaved(Result<PathBuf, Error>),
}