// Copyright (c) ZeroC, Inc.

use super::*;

#[derive(Debug)]
pub struct SlicedFormat {
    pub sliced_args: bool,
    pub sliced_return: bool,
}

impl SlicedFormat {
    pub fn parse_from(Unparsed { directive, args }: &Unparsed, span: &Span, diagnostics: &mut Diagnostics) -> Self {
        debug_assert_eq!(directive, Self::directive());

        check_that_arguments_were_provided(args, Self::directive(), span, diagnostics);

        let (mut sliced_args, mut sliced_return) = (false, false);
        for arg in args {
            match arg.as_str() {
                "Args" => sliced_args = true,
                "Return" => sliced_return = true,
                _ => {
                    Diagnostic::new(Error::ArgumentNotSupported {
                        argument: arg.clone(),
                        directive: Self::directive().to_owned(),
                    })
                    .set_span(span)
                    .add_note("'Args' and 'Return' are the only valid arguments", None)
                    .push_into(diagnostics);
                }
            }
        }

        SlicedFormat {
            sliced_args,
            sliced_return,
        }
    }

    pub fn validate_on(&self, applied_on: Attributables, span: &Span, diagnostics: &mut Diagnostics) {
        if !matches!(applied_on, Attributables::Operation(_)) {
            let note = "the slicedFormat attribute can only be applied to operations";
            report_unexpected_attribute(self, span, Some(note), diagnostics);
        }
    }
}

implement_attribute_kind_for!(SlicedFormat, "slicedFormat", false);
