summaryrefslogtreecommitdiff
path: root/src/model
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-11-04 09:30:44 +0100
committerLaurenz <laurmaedje@gmail.com>2022-11-04 11:38:09 +0100
commiteb951c008beea502042db4a3a0e8d1f8b51f6f52 (patch)
tree9856ee4ed0222222669de10e616a580b2a60135e /src/model
parent33928a00dc58250e24da1dae4e5db17e7b598d70 (diff)
Style changes
Diffstat (limited to 'src/model')
-rw-r--r--src/model/args.rs6
-rw-r--r--src/model/array.rs10
-rw-r--r--src/model/content.rs5
-rw-r--r--src/model/eval.rs18
-rw-r--r--src/model/func.rs24
-rw-r--r--src/model/mod.rs30
-rw-r--r--src/model/ops.rs38
-rw-r--r--src/model/str.rs19
-rw-r--r--src/model/styles.rs28
-rw-r--r--src/model/value.rs17
10 files changed, 69 insertions, 126 deletions
diff --git a/src/model/args.rs b/src/model/args.rs
index 9fb30b9c..fe5f8254 100644
--- a/src/model/args.rs
+++ b/src/model/args.rs
@@ -29,11 +29,7 @@ impl Args {
pub fn new(span: Span, values: impl IntoIterator<Item = Value>) -> Self {
let items = values
.into_iter()
- .map(|value| Arg {
- span,
- name: None,
- value: Spanned::new(value, span),
- })
+ .map(|value| Arg { span, name: None, value: Spanned::new(value, span) })
.collect();
Self { span, items }
}
diff --git a/src/model/array.rs b/src/model/array.rs
index 053248ec..40b063e2 100644
--- a/src/model/array.rs
+++ b/src/model/array.rs
@@ -119,7 +119,7 @@ impl Array {
.ok_or_else(|| out_of_bounds(end, len))?
.max(start);
- Ok(Self::from_vec(self.0[start .. end].to_vec()))
+ Ok(Self::from_vec(self.0[start..end].to_vec()))
}
/// Whether the array contains a specific value.
@@ -287,12 +287,8 @@ impl Array {
/// Resolve an index.
fn locate(&self, index: i64) -> Option<usize> {
- usize::try_from(if index >= 0 {
- index
- } else {
- self.len().checked_add(index)?
- })
- .ok()
+ usize::try_from(if index >= 0 { index } else { self.len().checked_add(index)? })
+ .ok()
}
}
diff --git a/src/model/content.rs b/src/model/content.rs
index 372f6ff6..7b09c697 100644
--- a/src/model/content.rs
+++ b/src/model/content.rs
@@ -161,10 +161,7 @@ impl Add for Content {
return lhs;
}
- let seq = match (
- lhs.downcast::<SequenceNode>(),
- rhs.downcast::<SequenceNode>(),
- ) {
+ let seq = match (lhs.downcast::<SequenceNode>(), rhs.downcast::<SequenceNode>()) {
(Some(lhs), Some(rhs)) => lhs.0.iter().chain(&rhs.0).cloned().collect(),
(Some(lhs), None) => lhs.0.iter().cloned().chain(iter::once(rhs)).collect(),
(None, Some(rhs)) => iter::once(lhs).chain(rhs.0.iter().cloned()).collect(),
diff --git a/src/model/eval.rs b/src/model/eval.rs
index 8e287f14..16e66818 100644
--- a/src/model/eval.rs
+++ b/src/model/eval.rs
@@ -140,7 +140,6 @@ fn eval_markup(
vm.scopes.top.define(wrap.binding().take(), tail);
wrap.body().eval(vm)?.display(vm.world)
}
-
_ => node.eval(vm)?,
});
@@ -162,7 +161,7 @@ impl Eval for ast::MarkupNode {
fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> {
match self {
Self::Space(v) => Ok(match v.newlines() {
- 0 ..= 1 => (vm.items.space)(),
+ 0..=1 => (vm.items.space)(),
_ => (vm.items.parbreak)(),
}),
Self::Linebreak(v) => v.eval(vm),
@@ -369,10 +368,7 @@ impl Eval for ast::Frac {
type Output = Content;
fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> {
- Ok((vm.items.math_frac)(
- self.num().eval(vm)?,
- self.denom().eval(vm)?,
- ))
+ Ok((vm.items.math_frac)(self.num().eval(vm)?, self.denom().eval(vm)?))
}
}
@@ -501,7 +497,6 @@ fn eval_code(
vm.scopes.top.define(wrap.binding().take(), tail);
wrap.body().eval(vm)?
}
-
_ => expr.eval(vm)?,
};
@@ -676,18 +671,12 @@ impl Eval for ast::FieldAccess {
Ok(match object {
Value::Dict(dict) => dict.get(&field).at(span)?.clone(),
-
Value::Content(node) => node
.to::<dyn Show>()
.and_then(|node| node.field(&field))
.ok_or_else(|| format!("unknown field {field:?}"))
.at(span)?,
-
- v => bail!(
- self.target().span(),
- "cannot access field on {}",
- v.type_name()
- ),
+ v => bail!(self.target().span(), "cannot access field on {}", v.type_name()),
})
}
}
@@ -706,7 +695,6 @@ impl Eval for ast::FuncCall {
let point = || Tracepoint::Call(func.name().map(Into::into));
func.call(vm, args).trace(vm.world, point, self.span())?
}
-
v => bail!(
self.callee().span(),
"expected callable or collection, found {}",
diff --git a/src/model/func.rs b/src/model/func.rs
index 5be1aae3..456b6aa6 100644
--- a/src/model/func.rs
+++ b/src/model/func.rs
@@ -32,12 +32,7 @@ impl Func {
name: &'static str,
func: fn(&mut Vm, &mut Args) -> SourceResult<Value>,
) -> Self {
- Self(Arc::new(Repr::Native(Native {
- name,
- func,
- set: None,
- node: None,
- })))
+ Self(Arc::new(Repr::Native(Native { name, func, set: None, node: None })))
}
/// Create a new function from a native rust node.
@@ -92,7 +87,7 @@ impl Func {
Repr::Native(native) => (native.func)(vm, &mut args)?,
Repr::Closure(closure) => closure.call(vm, &mut args)?,
Repr::With(wrapped, applied) => {
- args.items.splice(.. 0, applied.items.iter().cloned());
+ args.items.splice(..0, applied.items.iter().cloned());
return wrapped.call(vm, args);
}
};
@@ -194,12 +189,15 @@ impl Closure {
// Parse the arguments according to the parameter list.
for (param, default) in &self.params {
- scopes.top.define(param.clone(), match default {
- None => args.expect::<Value>(param)?,
- Some(default) => {
- args.named::<Value>(param)?.unwrap_or_else(|| default.clone())
- }
- });
+ scopes.top.define(
+ param.clone(),
+ match default {
+ Some(default) => {
+ args.named::<Value>(param)?.unwrap_or_else(|| default.clone())
+ }
+ None => args.expect::<Value>(param)?,
+ },
+ );
}
// Put the remaining arguments into the sink.
diff --git a/src/model/mod.rs b/src/model/mod.rs
index fdebce0a..20bca106 100644
--- a/src/model/mod.rs
+++ b/src/model/mod.rs
@@ -1,4 +1,4 @@
-//! Layout and computation model.
+//! Document and computation model.
#[macro_use]
mod items;
@@ -23,18 +23,18 @@ mod ops;
mod scope;
mod vm;
-pub use self::str::*;
-pub use args::*;
-pub use array::*;
-pub use cast::*;
-pub use content::*;
-pub use dict::*;
-pub use eval::*;
-pub use func::*;
-pub use items::*;
-pub use scope::*;
-pub use styles::*;
-pub use value::*;
-pub use vm::*;
-
pub use typst_macros::{capability, node};
+
+pub use self::args::*;
+pub use self::array::*;
+pub use self::cast::*;
+pub use self::content::*;
+pub use self::dict::*;
+pub use self::eval::*;
+pub use self::func::*;
+pub use self::items::*;
+pub use self::scope::*;
+pub use self::str::*;
+pub use self::styles::*;
+pub use self::value::*;
+pub use self::vm::*;
diff --git a/src/model/ops.rs b/src/model/ops.rs
index ee126b03..9d55fa63 100644
--- a/src/model/ops.rs
+++ b/src/model/ops.rs
@@ -103,17 +103,17 @@ pub fn add(lhs: Value, rhs: Value) -> StrResult<Value> {
if let (Some(&a), Some(&b)) =
(a.downcast::<GenAlign>(), b.downcast::<GenAlign>())
{
- if a.axis() != b.axis() {
- Value::dynamic(match a.axis() {
- Axis::X => Axes { x: a, y: b },
- Axis::Y => Axes { x: b, y: a },
- })
- } else {
+ if a.axis() == b.axis() {
return Err(format!("cannot add two {:?} alignments", a.axis()));
}
- } else {
- mismatch!("cannot add {} and {}", a, b);
- }
+
+ return Ok(Value::dynamic(match a.axis() {
+ Axis::X => Axes { x: a, y: b },
+ Axis::Y => Axes { x: b, y: a },
+ }));
+ };
+
+ mismatch!("cannot add {} and {}", a, b);
}
(a, b) => mismatch!("cannot add {} and {}", a, b),
@@ -370,17 +370,11 @@ pub fn not_in(lhs: Value, rhs: Value) -> StrResult<Value> {
/// Test for containment.
pub fn contains(lhs: &Value, rhs: &Value) -> Option<bool> {
- Some(match (lhs, rhs) {
- (Str(a), Str(b)) => b.as_str().contains(a.as_str()),
- (Dyn(a), Str(b)) => {
- if let Some(regex) = a.downcast::<Regex>() {
- regex.is_match(b)
- } else {
- return Option::None;
- }
- }
- (Str(a), Dict(b)) => b.contains(a),
- (a, Array(b)) => b.contains(a),
- _ => return Option::None,
- })
+ match (lhs, rhs) {
+ (Str(a), Str(b)) => Some(b.as_str().contains(a.as_str())),
+ (Dyn(a), Str(b)) => a.downcast::<Regex>().map(|regex| regex.is_match(b)),
+ (Str(a), Dict(b)) => Some(b.contains(a)),
+ (a, Array(b)) => Some(b.contains(a)),
+ _ => Option::None,
+ }
}
diff --git a/src/model/str.rs b/src/model/str.rs
index 4aa40c54..1fcf7075 100644
--- a/src/model/str.rs
+++ b/src/model/str.rs
@@ -67,17 +67,13 @@ impl Str {
.ok_or_else(|| out_of_bounds(end, len))?
.max(start);
- Ok(self.0[start .. end].into())
+ Ok(self.0[start..end].into())
}
/// Resolve an index.
fn locate(&self, index: i64) -> Option<usize> {
- usize::try_from(if index >= 0 {
- index
- } else {
- self.len().checked_add(index)?
- })
- .ok()
+ usize::try_from(if index >= 0 { index } else { self.len().checked_add(index)? })
+ .ok()
}
/// Whether the given pattern exists in this string.
@@ -207,7 +203,7 @@ impl Str {
Some(StrPattern::Regex(re)) => {
let s = self.as_str();
let mut last = 0;
- let mut range = 0 .. s.len();
+ let mut range = 0..s.len();
for m in re.find_iter(s) {
// Does this match follow directly after the last one?
@@ -235,7 +231,7 @@ impl Str {
range.end = s.len();
}
- &s[range.start .. range.start.max(range.end)]
+ &s[range.start..range.start.max(range.end)]
}
};
@@ -271,10 +267,7 @@ impl Str {
/// The out of bounds access error message.
#[cold]
fn out_of_bounds(index: i64, len: i64) -> String {
- format!(
- "string index out of bounds (index: {}, len: {})",
- index, len
- )
+ format!("string index out of bounds (index: {}, len: {})", index, len)
}
/// Convert an item of std's `match_indices` to a dictionary.
diff --git a/src/model/styles.rs b/src/model/styles.rs
index 24566b09..9463e55e 100644
--- a/src/model/styles.rs
+++ b/src/model/styles.rs
@@ -94,7 +94,7 @@ impl StyleMap {
/// This is useful over `chain` when you want to combine two maps, but you
/// still need an owned map without a lifetime.
pub fn apply_map(&mut self, tail: &Self) {
- self.0.splice(0 .. 0, tail.0.iter().cloned());
+ self.0.splice(0..0, tail.0.iter().cloned());
}
/// Mark all contained properties as _scoped_. This means that they only
@@ -159,10 +159,7 @@ impl StyleEntry {
}
}
- StyleChain {
- head: std::slice::from_ref(self),
- tail: Some(tail),
- }
+ StyleChain { head: std::slice::from_ref(self), tail: Some(tail) }
}
/// If this is a property, return it.
@@ -328,7 +325,7 @@ impl<'a> StyleChain<'a> {
let mut suffix = StyleMap::new();
let take = self.links().count().saturating_sub(len);
for link in self.links().take(take) {
- suffix.0.splice(0 .. 0, link.iter().cloned());
+ suffix.0.splice(0..0, link.iter().cloned());
}
suffix
}
@@ -344,10 +341,7 @@ impl<'a> StyleChain<'a> {
/// Iterate over the entries of the chain.
fn entries(self) -> Entries<'a> {
- Entries {
- inner: [].as_slice().iter(),
- links: self.links(),
- }
+ Entries { inner: [].as_slice().iter(), links: self.links() }
}
/// Iterate over the links of the chain.
@@ -582,12 +576,12 @@ impl<'a, T> StyleVecBuilder<'a, T> {
for &(mut chain, _) in iter {
let len = chain.links().count();
if len < shared {
- for _ in 0 .. shared - len {
+ for _ in 0..shared - len {
trunk.pop();
}
shared = len;
} else if len > shared {
- for _ in 0 .. len - shared {
+ for _ in 0..len - shared {
chain.pop();
}
}
@@ -1017,7 +1011,7 @@ impl Recipe {
for mat in regex.find_iter(text) {
let start = mat.start();
if cursor < start {
- result.push(make(text[cursor .. start].into()));
+ result.push(make(text[cursor..start].into()));
}
result.push(self.call(world, || Value::Str(mat.as_str().into()))?);
@@ -1029,7 +1023,7 @@ impl Recipe {
}
if cursor < text.len() {
- result.push(make(text[cursor ..].into()));
+ result.push(make(text[cursor..].into()));
}
Content::sequence(result)
@@ -1066,11 +1060,7 @@ impl Recipe {
impl Debug for Recipe {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
- write!(
- f,
- "Recipe matching {:?} from {:?}",
- self.pattern, self.func.span
- )
+ write!(f, "Recipe matching {:?} from {:?}", self.pattern, self.func.span)
}
}
diff --git a/src/model/value.rs b/src/model/value.rs
index 07719883..825e6f55 100644
--- a/src/model/value.rs
+++ b/src/model/value.rs
@@ -294,11 +294,8 @@ where
}
fn dyn_eq(&self, other: &Dynamic) -> bool {
- if let Some(other) = other.downcast::<Self>() {
- self == other
- } else {
- false
- }
+ let Some(other) = other.downcast::<Self>() else { return false };
+ self == other
}
fn dyn_type_name(&self) -> &'static str {
@@ -411,15 +408,9 @@ mod tests {
test(Abs::pt(5.5), "5.5pt");
test(Angle::deg(90.0), "90deg");
test(Ratio::one() / 2.0, "50%");
- test(
- Ratio::new(0.3) + Length::from(Abs::cm(2.0)),
- "30% + 56.69pt",
- );
+ test(Ratio::new(0.3) + Length::from(Abs::cm(2.0)), "30% + 56.69pt");
test(Fr::one() * 7.55, "7.55fr");
- test(
- Color::Rgba(RgbaColor::new(1, 1, 1, 0xff)),
- "rgb(\"#010101\")",
- );
+ test(Color::Rgba(RgbaColor::new(1, 1, 1, 0xff)), "rgb(\"#010101\")");
// Collections.
test("hello", r#""hello""#);