less-rs 0.0.0

A Rust implementation of the Less CSS preprocessor.
Documentation
use cssparser::{Delimiter, Token};
use less_allocator::Allocator;

use crate::{error::ParserError, traits::Parse};

/// A Less variable reference
///
///    @font-size: 16px;
///    font-size: @font-size;
///
#[derive(Clone, Debug, PartialEq, Eq, Hash, Default)]
pub struct VariableReference<'i>(pub(crate) cssparser::CowRcStr<'i>);

impl<'i> Parse<'i> for VariableReference<'i> {
  fn parse<'t>(
    _allocator: &'i Allocator,
    input: &mut cssparser::Parser<'i, 't>,
  ) -> Result<Self, cssparser::ParseError<'i, ParserError<'i>>> {
    input.parse_until_before(Delimiter::Semicolon, |parser| {
      // Token-wise `AtKeyword` and less variable reference are the almost the same, with
      // `AtKeyword` being more adaptive with regards of the definition of less variable reference.
      // The exact type of token cannot be determined until we start to parse.
      if let Ok(Token::AtKeyword(keyword)) = parser.next() {
        Ok(VariableReference(keyword.clone()))
      } else {
        Err(parser.new_custom_error(ParserError::InvalidLessVariable))
      }
    })
  }
}