Skip to main content

charts_rs/charts/
error.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5//     http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12
13//! The single crate-level error type. It replaces what used to be four
14//! separate per-module enums (`canvas`, `font`, `component`, `encoder`), so
15//! callers match one type and `?` composes across the whole crate. Each of
16//! those modules now re-exports `Error`/`Result` from here, keeping the old
17//! `canvas::Error`, `font::Error`, … paths valid.
18
19use snafu::Snafu;
20
21#[derive(Debug, Snafu)]
22#[snafu(visibility(pub(crate)))]
23pub enum Error {
24    #[snafu(display("Params is invalid: {message}"))]
25    Params { message: String },
26    #[snafu(display("Json is invalid: {source}"))]
27    Json { source: serde_json::Error },
28    #[snafu(display("Error font: {name} not found"))]
29    FontNotFound { name: String },
30    #[snafu(display("Error parse font: {message}"))]
31    ParseFont { message: String },
32
33    // Raster encoding (image-encoder feature); the source types live behind
34    // the optional `resvg` / `image` dependencies, so the variants are gated.
35    #[cfg(feature = "image-encoder")]
36    #[snafu(display("Io {file}: {source}"))]
37    Io {
38        file: String,
39        source: std::io::Error,
40    },
41    #[cfg(feature = "image-encoder")]
42    #[snafu(display("Image size is invalid, width: {width}, height: {height}"))]
43    Size { width: u32, height: u32 },
44    #[cfg(feature = "image-encoder")]
45    #[snafu(display("Image from raw is fail, size:{size}"))]
46    Raw { size: usize },
47    #[cfg(feature = "image-encoder")]
48    #[snafu(display("Error to parse: {source}"))]
49    Parse { source: resvg::usvg::Error },
50    #[cfg(feature = "image-encoder")]
51    #[snafu(display("Encode fail: {source}"))]
52    Image { source: image::ImageError },
53}
54
55impl From<serde_json::Error> for Error {
56    fn from(value: serde_json::Error) -> Self {
57        Error::Json { source: value }
58    }
59}
60
61impl From<&str> for Error {
62    fn from(value: &str) -> Self {
63        Error::ParseFont {
64            message: value.to_string(),
65        }
66    }
67}
68
69pub type Result<T, E = Error> = std::result::Result<T, E>;