// SPDX-License-Identifier: LGPL-2.1-only
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>

//! Error type returned by theme loading and parsing.

use std::fmt;
use std::io;
use std::path::PathBuf;

/// What went wrong while locating, reading or parsing a theme.
#[ derive( Debug ) ]
pub enum ThemeError
{
	/// An I/O error reading the theme file at the given path.
	Io( PathBuf, io::Error ),
	/// The theme file at the given path was not valid JSON.
	ParseJson( PathBuf, serde_json::Error ),
	/// No theme with this id was found in any search path.
	NotFound( String ),
	/// A colour literal was not a recognised form (`#RRGGBB`, `#RRGGBBAA` or
	/// `rgb[a](…)`).
	InvalidColor( String ),
	/// A `@name` reference was not defined in the top-level `colors`,
	/// `gradients` or `inset_stacks`.
	UnknownColorRef( String ),
}

impl fmt::Display for ThemeError
{
	fn fmt( &self, f: &mut fmt::Formatter<'_> ) -> fmt::Result
	{
		match self
		{
			ThemeError::Io( p, e )            => write!( f, "reading {}: {}", p.display(), e ),
			ThemeError::ParseJson( p, e )     => write!( f, "parsing {}: {}", p.display(), e ),
			ThemeError::NotFound( id )        => write!( f, "theme `{}` not found in any search path", id ),
			ThemeError::InvalidColor( s )     => write!( f, "invalid colour `{}` (expected #RRGGBB, #RRGGBBAA or rgb[a](…))", s ),
			ThemeError::UnknownColorRef( s )  => write!( f, "unknown reference `@{}` (not defined in top-level `colors`, `gradients` or `inset_stacks`)", s ),
		}
	}
}

impl std::error::Error for ThemeError {}
