grass/src/common.rs

162 lines
3.6 KiB
Rust
Raw Normal View History

use std::fmt::{self, Display};
2020-01-06 00:25:40 -05:00
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Op {
Equal,
NotEqual,
2020-01-12 12:18:44 -05:00
GreaterThan,
2020-01-06 00:25:40 -05:00
GreaterThanEqual,
2020-01-12 12:18:44 -05:00
LessThan,
2020-01-06 00:25:40 -05:00
LessThanEqual,
2020-01-25 09:56:27 -05:00
Plus,
Minus,
Mul,
Div,
Rem,
2020-01-06 00:25:40 -05:00
}
impl Display for Op {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
2020-01-18 19:11:19 -05:00
Self::Equal => write!(f, "=="),
Self::NotEqual => write!(f, "!="),
Self::GreaterThanEqual => write!(f, ">="),
Self::LessThanEqual => write!(f, "<="),
Self::GreaterThan => write!(f, ">"),
Self::LessThan => write!(f, "<"),
2020-01-25 09:56:27 -05:00
Self::Plus => write!(f, "+"),
Self::Minus => write!(f, "-"),
Self::Mul => write!(f, "*"),
Self::Div => write!(f, "/"),
Self::Rem => write!(f, "%"),
2020-01-06 00:25:40 -05:00
}
}
}
2020-04-01 15:32:52 -04:00
impl Op {
/// Get order of precedence for an operator
///
/// Higher numbers are evaluated first.
/// Do not rely on the number itself, but rather the size relative to other numbers
///
/// If precedence is equal, the leftmost operation is evaluated first
pub fn precedence(&self) -> usize {
match self {
Self::Equal
| Self::NotEqual
| Self::GreaterThan
| Self::GreaterThanEqual
| Self::LessThan
| Self::LessThanEqual => 0,
Self::Plus | Self::Minus => 1,
Self::Mul | Self::Div | Self::Rem => 2,
}
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Pos {
line: u32,
column: u32,
}
impl Pos {
pub const fn new() -> Self {
Pos { line: 1, column: 1 }
}
pub const fn line(self) -> u32 {
self.line
}
pub const fn column(self) -> u32 {
self.column
}
pub fn newline(&mut self) {
self.line += 1;
self.column = 0;
}
pub fn next_char(&mut self) {
self.column += 1;
}
pub fn chars(&mut self, num: u32) {
self.column += num;
}
}
2020-01-06 18:26:32 -05:00
impl Display for Pos {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "line:{} col:{}", self.line, self.column)
}
2020-01-06 19:23:52 -05:00
}
2020-01-12 19:56:33 -05:00
2020-01-26 16:23:37 -05:00
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub(crate) enum QuoteKind {
Single,
Double,
None,
}
2020-03-23 12:12:08 -04:00
impl QuoteKind {
/// SASS will prefer double quotes over single quotes after
/// operations, e.g. `'foo' + red` => `"foored"`
pub fn normalize(self) -> QuoteKind {
match self {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
QuoteKind::None => QuoteKind::None,
}
}
}
2020-02-02 14:46:58 -05:00
impl Display for QuoteKind {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Single => write!(f, "'"),
Self::Double => write!(f, "\""),
Self::None => write!(f, ""),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Brackets {
None,
Bracketed,
}
2020-02-08 16:07:37 -05:00
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ListSeparator {
Space,
Comma,
}
impl ListSeparator {
pub fn as_str(self) -> &'static str {
match self {
Self::Space => " ",
Self::Comma => ", ",
}
}
pub fn name(self) -> &'static str {
match self {
Self::Space => "space",
Self::Comma => "comma",
}
}
}
impl Display for ListSeparator {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Space => write!(f, " "),
Self::Comma => write!(f, ", "),
}
}
}