use std::path::PathBuf;

use thiserror::Error;

pub type Result<T, E = ServerError> = std::result::Result<T, E>;

#[derive(Debug, Error)]
pub enum ServerError {
    #[error("failed to read {path}: {source}")]
    Io {
        path: PathBuf,
        source: std::io::Error,
    },
    #[error("invalid schema config: {0}")]
    SchemaConfig(String),
    #[error("schema build failed: {0}")]
    SchemaBuild(#[from] tidaldb::schema::SchemaError),
    #[error("tidalDB error: {0}")]
    Tidal(#[from] tidaldb::TidalError),
    #[error("http server error: {0}")]
    Http(#[from] std::io::Error),
    #[error("bad request: {0}")]
    BadRequest(String),
}

impl ServerError {
    pub fn io(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
        Self::Io {
            path: path.into(),
            source,
        }
    }
}
