Move EcoString and OptionExt into util

This commit is contained in:
Laurenz 2021-07-29 11:35:49 +02:00
parent 6ebe621834
commit 312dcd070c
18 changed files with 67 additions and 57 deletions

View File

@ -5,7 +5,7 @@ use std::ops::{Add, AddAssign};
use std::rc::Rc;
use super::Value;
use crate::eco::EcoString;
use crate::util::EcoString;
/// Create a new [`Dict`] from key-value pairs.
#[macro_export]
@ -13,7 +13,7 @@ macro_rules! dict {
($($key:expr => $value:expr),* $(,)?) => {{
#[allow(unused_mut)]
let mut map = std::collections::BTreeMap::new();
$(map.insert($crate::eco::EcoString::from($key), $crate::eval::Value::from($value));)*
$(map.insert($crate::util::EcoString::from($key), $crate::eval::Value::from($value));)*
$crate::eval::Dict::from_map(map)
}};
}

View File

@ -3,7 +3,7 @@ use std::ops::Deref;
use std::rc::Rc;
use super::{Cast, EvalContext, Value};
use crate::eco::EcoString;
use crate::util::EcoString;
use crate::syntax::{Span, Spanned};
/// An evaluatable function.

View File

@ -26,7 +26,7 @@ use std::path::Path;
use std::rc::Rc;
use crate::diag::{Diag, DiagSet, Pass};
use crate::eco::EcoString;
use crate::util::EcoString;
use crate::geom::{Angle, Fractional, Length, Relative};
use crate::image::ImageCache;
use crate::loading::{FileId, Loader};

View File

@ -4,7 +4,7 @@ use std::ops::{Add, Deref};
use std::rc::Rc;
use super::Value;
use crate::eco::EcoString;
use crate::util::EcoString;
use crate::exec::ExecContext;
use crate::syntax::{Expr, SyntaxTree};

View File

@ -5,7 +5,7 @@ use std::rc::Rc;
use super::{ops, Array, Dict, Function, Template, TemplateFunc};
use crate::color::{Color, RgbaColor};
use crate::eco::EcoString;
use crate::util::EcoString;
use crate::exec::ExecContext;
use crate::geom::{Angle, Fractional, Length, Linear, Relative};
use crate::syntax::Spanned;

View File

@ -3,7 +3,7 @@ use std::rc::Rc;
use super::{Exec, ExecWithMap, State};
use crate::diag::{Diag, DiagSet, Pass};
use crate::eco::EcoString;
use crate::util::EcoString;
use crate::eval::{ExprMap, Template};
use crate::geom::{Align, Dir, Gen, GenAxis, Length, Linear, Sides, Size};
use crate::layout::{

View File

@ -9,7 +9,7 @@ pub use state::*;
use std::fmt::Write;
use crate::diag::Pass;
use crate::eco::EcoString;
use crate::util::EcoString;
use crate::eval::{ExprMap, Template, TemplateFunc, TemplateNode, TemplateTree, Value};
use crate::geom::{Dir, Gen};
use crate::layout::{LayoutTree, StackChild, StackNode};

View File

@ -269,46 +269,9 @@ impl Constraints {
let current = regions.current.to_spec();
let base = regions.base.to_spec();
self.exact.horizontal.set_if_some(current.horizontal);
self.exact.vertical.set_if_some(current.vertical);
self.base.horizontal.set_if_some(base.horizontal);
self.base.vertical.set_if_some(base.vertical);
}
}
/// Extends length-related options by providing convenience methods for setting
/// minimum and maximum lengths on them, even if they are `None`.
pub trait OptionExt {
/// Sets `other` as the value if `self` is `None` or if it contains a
/// value larger than `other`.
fn set_min(&mut self, other: Length);
/// Sets `other` as the value if `self` is `None` or if it contains a
/// value smaller than `other`.
fn set_max(&mut self, other: Length);
/// Sets `other` as the value if `self` is `Some`.
fn set_if_some(&mut self, other: Length);
}
impl OptionExt for Option<Length> {
fn set_min(&mut self, other: Length) {
match self {
Some(x) => x.set_min(other),
None => *self = Some(other),
}
}
fn set_max(&mut self, other: Length) {
match self {
Some(x) => x.set_max(other),
None => *self = Some(other),
}
}
fn set_if_some(&mut self, other: Length) {
if self.is_some() {
*self = Some(other);
}
self.exact.horizontal.and_set(Some(current.horizontal));
self.exact.vertical.and_set(Some(current.vertical));
self.base.horizontal.and_set(Some(base.horizontal));
self.base.vertical.and_set(Some(base.vertical));
}
}

View File

@ -32,6 +32,7 @@ use std::rc::Rc;
use crate::font::FontCache;
use crate::geom::*;
use crate::image::ImageCache;
use crate::util::OptionExt;
use crate::Context;
/// Layout a tree into a collection of frames.

View File

@ -5,9 +5,8 @@ use unicode_bidi::{BidiInfo, Level};
use xi_unicode::LineBreakIterator;
use super::*;
use crate::eco::EcoString;
use crate::exec::TextState;
use crate::util::{RangeExt, SliceExt};
use crate::util::{EcoString, RangeExt, SliceExt};
type Range = std::ops::Range<usize>;

View File

@ -33,7 +33,6 @@ pub mod diag;
#[macro_use]
pub mod eval;
pub mod color;
pub mod eco;
pub mod exec;
pub mod export;
pub mod font;

View File

@ -17,12 +17,12 @@ use std::fmt::{self, Display, Formatter};
use std::rc::Rc;
use crate::color::{Color, RgbaColor};
use crate::eco::EcoString;
use crate::eval::{EvalContext, FuncArgs, Scope, Template, Type, Value};
use crate::exec::{Exec, FontFamily};
use crate::font::{FontStyle, FontWeight, VerticalFontMetric};
use crate::geom::*;
use crate::syntax::Spanned;
use crate::util::EcoString;
/// Construct a scope containing all standard library definitions.
pub fn new() -> Scope {

View File

@ -15,8 +15,8 @@ pub use tokens::*;
use std::rc::Rc;
use crate::diag::Pass;
use crate::eco::EcoString;
use crate::syntax::*;
use crate::util::EcoString;
/// Parse a string of source code.
pub fn parse(src: &str) -> Pass<SyntaxTree> {

View File

@ -1,6 +1,6 @@
use super::{is_newline, Scanner};
use crate::eco::EcoString;
use crate::syntax::{Ident, RawNode, Span};
use crate::util::EcoString;
/// Resolve all escape sequences in a string.
pub fn resolve_string(string: &str) -> EcoString {

View File

@ -3,7 +3,7 @@ use std::ops::Deref;
use unicode_xid::UnicodeXID;
use super::Span;
use crate::eco::EcoString;
use crate::util::EcoString;
/// An unicode identifier with a few extra permissible characters.
///

View File

@ -13,7 +13,7 @@ pub use node::*;
pub use span::*;
pub use token::*;
use crate::eco::EcoString;
use crate::util::EcoString;
/// The abstract syntax tree.
///

View File

@ -1,9 +1,57 @@
//! Utilities.
mod eco;
pub use eco::EcoString;
use std::cmp::Ordering;
use std::ops::Range;
use std::path::{Component, Path, PathBuf};
/// Additional methods for options.
pub trait OptionExt<T> {
/// Replace `self` with `other` if `self` is `Some`.
fn and_set(&mut self, other: Option<T>);
/// Sets `other` as the value if `self` is `None` or if it contains a value
/// larger than `other`.
fn set_min(&mut self, other: T)
where
T: Ord;
/// Sets `other` as the value if `self` is `None` or if it contains a value
/// smaller than `other`.
fn set_max(&mut self, other: T)
where
T: Ord;
}
impl<T> OptionExt<T> for Option<T> {
fn and_set(&mut self, other: Option<T>) {
if self.is_some() {
*self = other;
}
}
fn set_min(&mut self, other: T)
where
T: Ord,
{
if self.as_ref().map_or(true, |x| other < *x) {
*self = Some(other);
}
}
fn set_max(&mut self, other: T)
where
T: Ord,
{
if self.as_ref().map_or(true, |x| other > *x) {
*self = Some(other);
}
}
}
/// Additional methods for slices.
pub trait SliceExt<T> {
/// Split a slice into consecutive groups with the same key.