slicec 0.4.0

The Slice parser and other core components for Slice compilers.
Documentation
// 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_argument_count_is_within(1..usize::MAX, 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::from_error(Error::InvalidAttributeArgument {
                        directive: Self::directive().to_owned(),
                        argument: arg.clone(),
                    })
                    .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_invalid_attribute(self, span, Some(note), diagnostics);
        }
    }
}

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