/*-
 * Scram-rs - a SCRAM authentification authorization library
 * 
 * Copyright (C) 2021  Aleksandr Morozov
 * Copyright (C) 2025 Aleksandr Morozov
 * 
 * The syslog-rs crate can be redistributed and/or modified
 * under the terms of either of the following licenses:
 *
 *   1. the Mozilla Public License Version 2.0 (the “MPL”) OR
 *
 *   2. The MIT License (MIT)
 *                     
 *   3. EUROPEAN UNION PUBLIC LICENCE v. 1.2 EUPL © the European Union 2007, 2016
 */

 #[cfg(not(feature = "std"))]
use core::borrow::Borrow;

#[cfg(feature = "std")]
use std::borrow::Borrow;

#[cfg(not(feature = "std"))]
use alloc::string::String;

#[cfg(not(feature = "std"))]
use alloc::boxed::Box;



use crate::{ScramResult, ScramResultClient, ScramResultServer};

/// A trait for the Server which can be used to convert the instance 
/// into the dyn Object in order to store it in the list as a generalized 
/// instance.
pub trait ScramServerDyn: Borrow<usize>
{
    /// Returns the assigned instance's ID. 
    fn get_id(&self) -> usize;

    /// Returns the username provided by the client. Can be used to form a
    ///     log message. On early stages before client sends username, it is
    ///     not available.
    /// 
    /// # Returns
    /// 
    /// [Option] is returned with:
    ///  * [Option::Some] with ref to username [String]
    ///  * [Option::None] if not yet available 
    fn get_auth_username(&self) -> Option<&String>;

    /// Returns the authzid (a=) if provided by the client. Can be used to form a
    ///     log message. On early stages before client sends username, it is
    ///     not availabel.
    /// 
    /// # Returns
    /// 
    /// [Option] is returned with:
    ///  * [Option::Some] with ref to authzid [String]
    ///  * [Option::None] if not yet available 
    fn get_auth_authzid(&self) -> Option<&String>;

    /// Decodes the input from received base64, performes parsing and result computation.  
    /// 
    /// # Arguments
    /// 
    /// * `input` - A response from client as a byte slice.
    /// 
    /// # Returns
    /// 
    /// * The [ScramResultServer] is returned with the result. 
    fn parse_response_base64(&mut self, input: &[u8]) -> ScramResultServer;

    /// Performes parsing of the response from client and result computation.  
    /// It is assumed that resp is UTF-8 valid sequences
    /// 
    /// # Arguments
    /// 
    /// * `resp` - A response from client as ref str.
    /// 
    /// # Returns
    /// 
    /// * The [ScramResultServer] is returned with the result.
    fn parse_response(&mut self, resp: &str) -> ScramResultServer;
}



pub trait ScramClientDyn
{
    fn init_client_msg(&mut self) -> ScramResultClient;
    fn parse_response_base64(&mut self, input: &[u8]) -> ScramResult<ScramResultClient>;
    fn parse_response(&mut self, resp: &str) -> ScramResult<ScramResultClient>;
}

/*
pub struct ScramServerDynHolder<'ss>
{
    obj: Box<dyn ScramServerDyn + 'ss>,
}

impl<'ss> ScramServerDynHolder<'ss>
{
    pub 
    fn new(obj: Box<dyn ScramServerDyn + 'ss>) -> Self
    {
        return Self{ obj: obj };
    }
}
*/
