#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 1998-2026 Stephane Galland <galland@arakhne.org>
#
# This program is free library; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or any later version.
#
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; see the file COPYING.  If not,
# write to the Free Software Foundation, Inc., 59 Temple Place - Suite
# 330, Boston, MA 02111-1307, USA.

"""
Standard callbacks for exiting the program.
"""

import sys
from abc import ABC, abstractmethod
from typing import override


class AutoLaTeXExiter(ABC):
	"""
	Callback that is invoked when the program should be terminated.
	"""
	@abstractmethod
	def exit_on_failure(self):
		raise NotImplementedError

	@abstractmethod
	def exit_on_exception(self, exception : BaseException):
		raise NotImplementedError

	@abstractmethod
	def exit_on_success(self):
		raise NotImplementedError


class AutoLaTeXSysExitExiter(AutoLaTeXExiter):
	"""
	Callback that is invoked when the program should be terminated. This callback call the system exit instructions.
	"""
	@override
	def exit_on_failure(self):
		sys.exit(255)

	@override
	def exit_on_exception(self, exception : BaseException):
		sys.exit(255)

	@override
	def exit_on_success(self):
		sys.exit(0)

class AutoLaTeXExceptionExiter(AutoLaTeXExiter):
	"""
	Callback that is invoked when the program should be terminated. This callback generates an exception on failure.
	"""
	@override
	def exit_on_failure(self):
		raise Exception()

	@override
	def exit_on_exception(self,  exception : BaseException):
		raise exception

	@override
	def exit_on_success(self):
		pass

