mirror of
https://github.com/typst/typst
synced 2025-05-13 20:46:23 +08:00
Move EcoString and OptionExt into util
This commit is contained in:
parent
6ebe621834
commit
312dcd070c
@ -5,7 +5,7 @@ use std::ops::{Add, AddAssign};
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use super::Value;
|
use super::Value;
|
||||||
use crate::eco::EcoString;
|
use crate::util::EcoString;
|
||||||
|
|
||||||
/// Create a new [`Dict`] from key-value pairs.
|
/// Create a new [`Dict`] from key-value pairs.
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
@ -13,7 +13,7 @@ macro_rules! dict {
|
|||||||
($($key:expr => $value:expr),* $(,)?) => {{
|
($($key:expr => $value:expr),* $(,)?) => {{
|
||||||
#[allow(unused_mut)]
|
#[allow(unused_mut)]
|
||||||
let mut map = std::collections::BTreeMap::new();
|
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)
|
$crate::eval::Dict::from_map(map)
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ use std::ops::Deref;
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use super::{Cast, EvalContext, Value};
|
use super::{Cast, EvalContext, Value};
|
||||||
use crate::eco::EcoString;
|
use crate::util::EcoString;
|
||||||
use crate::syntax::{Span, Spanned};
|
use crate::syntax::{Span, Spanned};
|
||||||
|
|
||||||
/// An evaluatable function.
|
/// An evaluatable function.
|
||||||
|
@ -26,7 +26,7 @@ use std::path::Path;
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use crate::diag::{Diag, DiagSet, Pass};
|
use crate::diag::{Diag, DiagSet, Pass};
|
||||||
use crate::eco::EcoString;
|
use crate::util::EcoString;
|
||||||
use crate::geom::{Angle, Fractional, Length, Relative};
|
use crate::geom::{Angle, Fractional, Length, Relative};
|
||||||
use crate::image::ImageCache;
|
use crate::image::ImageCache;
|
||||||
use crate::loading::{FileId, Loader};
|
use crate::loading::{FileId, Loader};
|
||||||
|
@ -4,7 +4,7 @@ use std::ops::{Add, Deref};
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use super::Value;
|
use super::Value;
|
||||||
use crate::eco::EcoString;
|
use crate::util::EcoString;
|
||||||
use crate::exec::ExecContext;
|
use crate::exec::ExecContext;
|
||||||
use crate::syntax::{Expr, SyntaxTree};
|
use crate::syntax::{Expr, SyntaxTree};
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ use std::rc::Rc;
|
|||||||
|
|
||||||
use super::{ops, Array, Dict, Function, Template, TemplateFunc};
|
use super::{ops, Array, Dict, Function, Template, TemplateFunc};
|
||||||
use crate::color::{Color, RgbaColor};
|
use crate::color::{Color, RgbaColor};
|
||||||
use crate::eco::EcoString;
|
use crate::util::EcoString;
|
||||||
use crate::exec::ExecContext;
|
use crate::exec::ExecContext;
|
||||||
use crate::geom::{Angle, Fractional, Length, Linear, Relative};
|
use crate::geom::{Angle, Fractional, Length, Linear, Relative};
|
||||||
use crate::syntax::Spanned;
|
use crate::syntax::Spanned;
|
||||||
|
@ -3,7 +3,7 @@ use std::rc::Rc;
|
|||||||
|
|
||||||
use super::{Exec, ExecWithMap, State};
|
use super::{Exec, ExecWithMap, State};
|
||||||
use crate::diag::{Diag, DiagSet, Pass};
|
use crate::diag::{Diag, DiagSet, Pass};
|
||||||
use crate::eco::EcoString;
|
use crate::util::EcoString;
|
||||||
use crate::eval::{ExprMap, Template};
|
use crate::eval::{ExprMap, Template};
|
||||||
use crate::geom::{Align, Dir, Gen, GenAxis, Length, Linear, Sides, Size};
|
use crate::geom::{Align, Dir, Gen, GenAxis, Length, Linear, Sides, Size};
|
||||||
use crate::layout::{
|
use crate::layout::{
|
||||||
|
@ -9,7 +9,7 @@ pub use state::*;
|
|||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
|
|
||||||
use crate::diag::Pass;
|
use crate::diag::Pass;
|
||||||
use crate::eco::EcoString;
|
use crate::util::EcoString;
|
||||||
use crate::eval::{ExprMap, Template, TemplateFunc, TemplateNode, TemplateTree, Value};
|
use crate::eval::{ExprMap, Template, TemplateFunc, TemplateNode, TemplateTree, Value};
|
||||||
use crate::geom::{Dir, Gen};
|
use crate::geom::{Dir, Gen};
|
||||||
use crate::layout::{LayoutTree, StackChild, StackNode};
|
use crate::layout::{LayoutTree, StackChild, StackNode};
|
||||||
|
@ -269,46 +269,9 @@ impl Constraints {
|
|||||||
let current = regions.current.to_spec();
|
let current = regions.current.to_spec();
|
||||||
let base = regions.base.to_spec();
|
let base = regions.base.to_spec();
|
||||||
|
|
||||||
self.exact.horizontal.set_if_some(current.horizontal);
|
self.exact.horizontal.and_set(Some(current.horizontal));
|
||||||
self.exact.vertical.set_if_some(current.vertical);
|
self.exact.vertical.and_set(Some(current.vertical));
|
||||||
self.base.horizontal.set_if_some(base.horizontal);
|
self.base.horizontal.and_set(Some(base.horizontal));
|
||||||
self.base.vertical.set_if_some(base.vertical);
|
self.base.vertical.and_set(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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,6 +32,7 @@ use std::rc::Rc;
|
|||||||
use crate::font::FontCache;
|
use crate::font::FontCache;
|
||||||
use crate::geom::*;
|
use crate::geom::*;
|
||||||
use crate::image::ImageCache;
|
use crate::image::ImageCache;
|
||||||
|
use crate::util::OptionExt;
|
||||||
use crate::Context;
|
use crate::Context;
|
||||||
|
|
||||||
/// Layout a tree into a collection of frames.
|
/// Layout a tree into a collection of frames.
|
||||||
|
@ -5,9 +5,8 @@ use unicode_bidi::{BidiInfo, Level};
|
|||||||
use xi_unicode::LineBreakIterator;
|
use xi_unicode::LineBreakIterator;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::eco::EcoString;
|
|
||||||
use crate::exec::TextState;
|
use crate::exec::TextState;
|
||||||
use crate::util::{RangeExt, SliceExt};
|
use crate::util::{EcoString, RangeExt, SliceExt};
|
||||||
|
|
||||||
type Range = std::ops::Range<usize>;
|
type Range = std::ops::Range<usize>;
|
||||||
|
|
||||||
|
@ -33,7 +33,6 @@ pub mod diag;
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
pub mod eval;
|
pub mod eval;
|
||||||
pub mod color;
|
pub mod color;
|
||||||
pub mod eco;
|
|
||||||
pub mod exec;
|
pub mod exec;
|
||||||
pub mod export;
|
pub mod export;
|
||||||
pub mod font;
|
pub mod font;
|
||||||
|
@ -17,12 +17,12 @@ use std::fmt::{self, Display, Formatter};
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use crate::color::{Color, RgbaColor};
|
use crate::color::{Color, RgbaColor};
|
||||||
use crate::eco::EcoString;
|
|
||||||
use crate::eval::{EvalContext, FuncArgs, Scope, Template, Type, Value};
|
use crate::eval::{EvalContext, FuncArgs, Scope, Template, Type, Value};
|
||||||
use crate::exec::{Exec, FontFamily};
|
use crate::exec::{Exec, FontFamily};
|
||||||
use crate::font::{FontStyle, FontWeight, VerticalFontMetric};
|
use crate::font::{FontStyle, FontWeight, VerticalFontMetric};
|
||||||
use crate::geom::*;
|
use crate::geom::*;
|
||||||
use crate::syntax::Spanned;
|
use crate::syntax::Spanned;
|
||||||
|
use crate::util::EcoString;
|
||||||
|
|
||||||
/// Construct a scope containing all standard library definitions.
|
/// Construct a scope containing all standard library definitions.
|
||||||
pub fn new() -> Scope {
|
pub fn new() -> Scope {
|
||||||
|
@ -15,8 +15,8 @@ pub use tokens::*;
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use crate::diag::Pass;
|
use crate::diag::Pass;
|
||||||
use crate::eco::EcoString;
|
|
||||||
use crate::syntax::*;
|
use crate::syntax::*;
|
||||||
|
use crate::util::EcoString;
|
||||||
|
|
||||||
/// Parse a string of source code.
|
/// Parse a string of source code.
|
||||||
pub fn parse(src: &str) -> Pass<SyntaxTree> {
|
pub fn parse(src: &str) -> Pass<SyntaxTree> {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use super::{is_newline, Scanner};
|
use super::{is_newline, Scanner};
|
||||||
use crate::eco::EcoString;
|
|
||||||
use crate::syntax::{Ident, RawNode, Span};
|
use crate::syntax::{Ident, RawNode, Span};
|
||||||
|
use crate::util::EcoString;
|
||||||
|
|
||||||
/// Resolve all escape sequences in a string.
|
/// Resolve all escape sequences in a string.
|
||||||
pub fn resolve_string(string: &str) -> EcoString {
|
pub fn resolve_string(string: &str) -> EcoString {
|
||||||
|
@ -3,7 +3,7 @@ use std::ops::Deref;
|
|||||||
use unicode_xid::UnicodeXID;
|
use unicode_xid::UnicodeXID;
|
||||||
|
|
||||||
use super::Span;
|
use super::Span;
|
||||||
use crate::eco::EcoString;
|
use crate::util::EcoString;
|
||||||
|
|
||||||
/// An unicode identifier with a few extra permissible characters.
|
/// An unicode identifier with a few extra permissible characters.
|
||||||
///
|
///
|
||||||
|
@ -13,7 +13,7 @@ pub use node::*;
|
|||||||
pub use span::*;
|
pub use span::*;
|
||||||
pub use token::*;
|
pub use token::*;
|
||||||
|
|
||||||
use crate::eco::EcoString;
|
use crate::util::EcoString;
|
||||||
|
|
||||||
/// The abstract syntax tree.
|
/// The abstract syntax tree.
|
||||||
///
|
///
|
||||||
|
@ -1,9 +1,57 @@
|
|||||||
//! Utilities.
|
//! Utilities.
|
||||||
|
|
||||||
|
mod eco;
|
||||||
|
|
||||||
|
pub use eco::EcoString;
|
||||||
|
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::ops::Range;
|
use std::ops::Range;
|
||||||
use std::path::{Component, Path, PathBuf};
|
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.
|
/// Additional methods for slices.
|
||||||
pub trait SliceExt<T> {
|
pub trait SliceExt<T> {
|
||||||
/// Split a slice into consecutive groups with the same key.
|
/// Split a slice into consecutive groups with the same key.
|
Loading…
x
Reference in New Issue
Block a user