diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-01-24 16:23:57 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2020-01-24 16:23:57 +0100 |
| commit | 0a087cd28bbee5fcdffbb9d49b0ba9f413ad7f92 (patch) | |
| tree | fe00c96969ed2ee69e6d3b42de8ff2558f792edd /src/syntax/func | |
| parent | 03fddaf3aea778057aedd74dbcb27bae971ec22f (diff) | |
Reorganize modules 🧱
Diffstat (limited to 'src/syntax/func')
| -rw-r--r-- | src/syntax/func/keys.rs | 9 | ||||
| -rw-r--r-- | src/syntax/func/maps.rs | 9 | ||||
| -rw-r--r-- | src/syntax/func/mod.rs | 113 | ||||
| -rw-r--r-- | src/syntax/func/values.rs | 35 |
4 files changed, 54 insertions, 112 deletions
diff --git a/src/syntax/func/keys.rs b/src/syntax/func/keys.rs index dff97bde..116cd4e6 100644 --- a/src/syntax/func/keys.rs +++ b/src/syntax/func/keys.rs @@ -1,9 +1,10 @@ use crate::layout::prelude::*; +use super::values::AlignmentValue::{self, *}; use super::*; -use AxisKey::*; -use PaddingKey::*; -use AlignmentValue::*; +use self::AxisKey::*; +use self::PaddingKey::*; + pub trait Key { @@ -28,7 +29,7 @@ macro_rules! key { fn parse(key: Spanned<&str>) -> Option<Self::Output> { match key.v { $($($p)|* => Some($r)),*, - other => None, + _ => None, } } } diff --git a/src/syntax/func/maps.rs b/src/syntax/func/maps.rs index 452c8ab1..8941024e 100644 --- a/src/syntax/func/maps.rs +++ b/src/syntax/func/maps.rs @@ -1,9 +1,11 @@ //! Deduplicating maps and keys for argument parsing. -use std::collections::HashMap; -use std::hash::Hash; -use crate::layout::{LayoutAxes, SpecificAxis, GenericAxis}; +use crate::error::Errors; +use crate::layout::prelude::*; use crate::size::{PSize, ValueBox}; +use crate::syntax::span::Spanned; +use super::keys::*; +use super::values::*; use super::*; @@ -179,7 +181,6 @@ impl PaddingMap { padding: &mut ValueBox<Option<PSize>> ) { use PaddingKey::*; - use SpecificAxis::*; let map = self.0.dedup(errors, |key, &val| { (match key { diff --git a/src/syntax/func/mod.rs b/src/syntax/func/mod.rs index b6691ab5..e66b4d6a 100644 --- a/src/syntax/func/mod.rs +++ b/src/syntax/func/mod.rs @@ -1,8 +1,10 @@ -use super::*; +use crate::error::{Error, Errors}; +use super::expr::{Expr, Ident, Tuple, Object, Pair}; +use super::span::{Span, Spanned}; -pub_use_mod!(maps); -pub_use_mod!(keys); -pub_use_mod!(values); +pub mod maps; +pub mod keys; +pub mod values; #[derive(Debug, Clone, PartialEq)] @@ -17,22 +19,6 @@ pub struct FuncArgs { pub key: Object, } -#[derive(Debug, Clone, PartialEq)] -pub enum Arg { - Pos(Spanned<Expr>), - Key(Pair), -} - -impl Arg { - /// The span or the value or combined span of key and value. - pub fn span(&self) -> Span { - match self { - Arg::Pos(item) => item.span, - Arg::Key(Pair { key, value }) => Span::merge(key.span, value.span), - } - } -} - impl FuncArgs { pub fn new() -> FuncArgs { FuncArgs { @@ -42,10 +28,10 @@ impl FuncArgs { } /// Add an argument. - pub fn add(&mut self, arg: Arg) { + pub fn add(&mut self, arg: FuncArg) { match arg { - Arg::Pos(item) => self.add_pos(item), - Arg::Key(pair) => self.add_key_pair(pair), + FuncArg::Pos(item) => self.add_pos(item), + FuncArg::Key(pair) => self.add_key_pair(pair), } } @@ -64,74 +50,27 @@ impl FuncArgs { self.key.add_pair(pair); } - pub fn into_iter(self) -> impl Iterator<Item=Arg> { - self.pos.items.into_iter().map(|item| Arg::Pos(item)) - .chain(self.key.pairs.into_iter().map(|pair| Arg::Key(pair))) + pub fn into_iter(self) -> impl Iterator<Item=FuncArg> { + self.pos.items.into_iter().map(|item| FuncArg::Pos(item)) + .chain(self.key.pairs.into_iter().map(|pair| FuncArg::Key(pair))) } +} - // /// Force-extract the first positional argument. - // pub fn get_pos<E: ExpressionKind>(&mut self) -> ParseResult<E> { - // expect(self.get_pos_opt()) - // } - - // /// Extract the first positional argument. - // pub fn get_pos_opt<E: ExpressionKind>(&mut self) -> ParseResult<Option<E>> { - // Ok(if !self.positional.items.is_empty() { - // let spanned = self.positional.items.remove(0); - // Some(E::from_expr(spanned)?) - // } else { - // None - // }) - // } - - // /// Force-extract a keyword argument. - // pub fn get_key<E: ExpressionKind>(&mut self, name: &str) -> ParseResult<E> { - // expect(self.get_key_opt(name)) - // } - - // /// Extract a keyword argument. - // pub fn get_key_opt<E: ExpressionKind>(&mut self, name: &str) -> ParseResult<Option<E>> { - // self.keyword.pairs.iter() - // .position(|p| p.key.v.0 == name) - // .map(|index| { - // let value = self.keyword.pairs.swap_remove(index).value; - // E::from_expr(value) - // }) - // .transpose() - // } - - // /// Iterator over positional arguments. - // pub fn iter_pos(&mut self) -> std::vec::IntoIter<Spanned<Expr>> { - // let tuple = std::mem::replace(&mut self.positional, Tuple::new()); - // tuple.items.into_iter() - // } - - // /// Iterator over all keyword arguments. - // pub fn iter_keys(&mut self) -> std::vec::IntoIter<Pair> { - // let object = std::mem::replace(&mut self.keyword, Object::new()); - // object.pairs.into_iter() - // } - - // /// Clear the argument lists. - // pub fn clear(&mut self) { - // self.positional.items.clear(); - // self.keyword.pairs.clear(); - // } - - // /// Whether both the positional and keyword argument lists are empty. - // pub fn is_empty(&self) -> bool { - // self.positional.items.is_empty() && self.keyword.pairs.is_empty() - // } +#[derive(Debug, Clone, PartialEq)] +pub enum FuncArg { + Pos(Spanned<Expr>), + Key(Pair), } -// /// Extract the option expression kind from the option or return an error. -// fn expect<E: ExpressionKind>(opt: ParseResult<Option<E>>) -> ParseResult<E> { -// match opt { -// Ok(Some(spanned)) => Ok(spanned), -// Ok(None) => error!("expected {}", E::NAME), -// Err(e) => Err(e), -// } -// } +impl FuncArg { + /// The span or the value or combined span of key and value. + pub fn span(&self) -> Span { + match self { + FuncArg::Pos(item) => item.span, + FuncArg::Key(Pair { key, value }) => Span::merge(key.span, value.span), + } + } +} pub trait OptionExt: Sized { fn or_missing(self, errors: &mut Errors, span: Span, what: &str) -> Self; diff --git a/src/syntax/func/values.rs b/src/syntax/func/values.rs index b29b9726..a767aef6 100644 --- a/src/syntax/func/values.rs +++ b/src/syntax/func/values.rs @@ -1,12 +1,13 @@ +use std::fmt::{self, Display, Formatter}; use std::marker::PhantomData; use toddle::query::{FontStyle, FontWeight}; use crate::layout::prelude::*; -use crate::size::ScaleSize; +use crate::size::{Size, ScaleSize}; use crate::style::Paper; use super::*; -use AlignmentValue::*; +use self::AlignmentValue::*; pub trait Value { @@ -76,20 +77,6 @@ impl<T: Value> Value for Defaultable<T> { } } -impl Value for Direction { - type Output = Self; - - fn parse(expr: Spanned<Expr>) -> Result<Self::Output, Error> { - Ok(match Ident::parse(expr)?.as_str() { - "left-to-right" | "ltr" | "LTR" => Direction::LeftToRight, - "right-to-left" | "rtl" | "RTL" => Direction::RightToLeft, - "top-to-bottom" | "ttb" | "TTB" => Direction::TopToBottom, - "bottom-to-top" | "btt" | "BTT" => Direction::BottomToTop, - other => return Err(err!("invalid direction")) - }) - } -} - impl Value for FontStyle { type Output = Self; @@ -134,6 +121,20 @@ impl Value for Paper { } } +impl Value for Direction { + type Output = Self; + + fn parse(expr: Spanned<Expr>) -> Result<Self::Output, Error> { + Ok(match Ident::parse(expr)?.as_str() { + "left-to-right" | "ltr" | "LTR" => LeftToRight, + "right-to-left" | "rtl" | "RTL" => RightToLeft, + "top-to-bottom" | "ttb" | "TTB" => TopToBottom, + "bottom-to-top" | "btt" | "BTT" => BottomToTop, + _ => return Err(err!("invalid direction")) + }) + } +} + #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] pub enum AlignmentValue { Align(Alignment), @@ -203,7 +204,7 @@ impl Value for AlignmentValue { "top" => Top, "right" => Right, "bottom" => Bottom, - other => return Err(err!("invalid alignment")) + _ => return Err(err!("invalid alignment")) }) } } |
