1mod format;
34mod layout;
35mod props;
36mod reload;
37mod schema;
38mod theme;
39mod value;
40
41use std::fmt;
42
43pub use format::to_form_text;
44pub use layout::{load, load_value, save, save_value, validate};
45pub use props::{prop_from_json, prop_to_json, set_props};
46pub use reload::{ReloadReport, reload};
47pub use schema::{catalog, class_info, schema};
48pub use theme::{load_theme, save_theme, theme_from_value, theme_to_value};
49
50pub const FORMAT_VERSION: u64 = 1;
52
53#[derive(Debug)]
55pub enum Error {
56 Json(serde_json::Error),
58 Version(u64),
60 Structure { path: String, message: String },
62 Property { path: String, property: String, message: String },
64 Component { path: String, source: aylis_core::Error },
66}
67
68impl fmt::Display for Error {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70 match self {
71 Error::Json(e) => write!(f, "JSON inválido: {e}"),
72 Error::Version(v) => {
73 if *v > FORMAT_VERSION {
74 write!(f, "formato {v} é mais novo que este aylis (que lê o {FORMAT_VERSION}): atualize o aylis")
75 } else {
76 write!(f, "formato {v} é de uma versão anterior do Aylis (este lê o {FORMAT_VERSION}): rode o aylis-migrate no projeto")
77 }
78 }
79 Error::Structure { path, message } => write!(f, "{}{message}", at(path)),
80 Error::Property { path, property, message } => {
81 write!(f, "{}propriedade '{property}': {message}", at(path))
82 }
83 Error::Component { path, source } => write!(f, "{}{source}", at(path)),
84 }
85 }
86}
87
88fn at(path: &str) -> String {
89 if path.is_empty() { String::new() } else { format!("{path}: ") }
90}
91
92impl std::error::Error for Error {
93 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
94 match self {
95 Error::Json(e) => Some(e),
96 Error::Component { source, .. } => Some(source),
97 _ => None,
98 }
99 }
100}
101
102impl From<serde_json::Error> for Error {
103 fn from(e: serde_json::Error) -> Self {
104 Error::Json(e)
105 }
106}
107
108pub type Result<T, E = Error> = std::result::Result<T, E>;