typst_library/foundations/auto.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
use std::fmt::{self, Debug, Formatter};
use ecow::EcoString;
use crate::diag::HintedStrResult;
use crate::foundations::{
ty, CastInfo, Fold, FromValue, IntoValue, Reflect, Repr, Resolve, StyleChain, Type,
Value,
};
/// A value that indicates a smart default.
///
/// The auto type has exactly one value: `{auto}`.
///
/// Parameters that support the `{auto}` value have some smart default or
/// contextual behaviour. A good example is the [text direction]($text.dir)
/// parameter. Setting it to `{auto}` lets Typst automatically determine the
/// direction from the [text language]($text.lang).
#[ty(cast, name = "auto")]
#[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct AutoValue;
impl IntoValue for AutoValue {
fn into_value(self) -> Value {
Value::Auto
}
}
impl FromValue for AutoValue {
fn from_value(value: Value) -> HintedStrResult<Self> {
match value {
Value::Auto => Ok(Self),
_ => Err(Self::error(&value)),
}
}
}
impl Reflect for AutoValue {
fn input() -> CastInfo {
CastInfo::Type(Type::of::<Self>())
}
fn output() -> CastInfo {
CastInfo::Type(Type::of::<Self>())
}
fn castable(value: &Value) -> bool {
matches!(value, Value::Auto)
}
}
impl Debug for AutoValue {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.write_str("Auto")
}
}
impl Repr for AutoValue {
fn repr(&self) -> EcoString {
"auto".into()
}
}
/// A value that can be automatically determined.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum Smart<T> {
/// The value should be determined smartly based on the circumstances.
Auto,
/// A specific value.
Custom(T),
}
impl<T> Smart<T> {
/// Whether the value is `Auto`.
pub fn is_auto(&self) -> bool {
matches!(self, Self::Auto)
}
/// Whether this holds a custom value.
pub fn is_custom(&self) -> bool {
matches!(self, Self::Custom(_))
}
/// Whether this is a `Smart::Custom(x)` and `f(x)` is true.
pub fn is_custom_and<F>(self, f: F) -> bool
where
F: Fn(T) -> bool,
{
match self {
Self::Auto => false,
Self::Custom(x) => f(x),
}
}
/// Returns a `Smart<&T>` borrowing the inner `T`.
pub fn as_ref(&self) -> Smart<&T> {
match self {
Smart::Auto => Smart::Auto,
Smart::Custom(v) => Smart::Custom(v),
}
}
/// Returns the contained custom value.
///
/// If the value is [`Smart::Auto`], returns `None`.
///
/// Equivalently, this just converts `Smart` to `Option`.
pub fn custom(self) -> Option<T> {
match self {
Self::Auto => None,
Self::Custom(x) => Some(x),
}
}
/// Map the contained custom value with `f`.
pub fn map<F, U>(self, f: F) -> Smart<U>
where
F: FnOnce(T) -> U,
{
match self {
Self::Auto => Smart::Auto,
Self::Custom(x) => Smart::Custom(f(x)),
}
}
/// Map the contained custom value with `f` if it contains a custom value,
/// otherwise returns `default`.
pub fn map_or<F, U>(self, default: U, f: F) -> U
where
F: FnOnce(T) -> U,
{
match self {
Self::Auto => default,
Self::Custom(x) => f(x),
}
}
/// Keeps `self` if it contains a custom value, otherwise returns `other`.
pub fn or(self, other: Smart<T>) -> Self {
match self {
Self::Custom(x) => Self::Custom(x),
Self::Auto => other,
}
}
/// Keeps `self` if it contains a custom value, otherwise returns the
/// output of the given function.
pub fn or_else<F>(self, f: F) -> Self
where
F: FnOnce() -> Self,
{
match self {
Self::Custom(x) => Self::Custom(x),
Self::Auto => f(),
}
}
/// Returns `Auto` if `self` is `Auto`, otherwise calls the provided
/// function on the contained value and returns the result.
pub fn and_then<F, U>(self, f: F) -> Smart<U>
where
F: FnOnce(T) -> Smart<U>,
{
match self {
Smart::Auto => Smart::Auto,
Smart::Custom(x) => f(x),
}
}
/// Returns the contained custom value or a provided default value.
pub fn unwrap_or(self, default: T) -> T {
match self {
Self::Auto => default,
Self::Custom(x) => x,
}
}
/// Returns the contained custom value or computes a default value.
pub fn unwrap_or_else<F>(self, f: F) -> T
where
F: FnOnce() -> T,
{
match self {
Self::Auto => f(),
Self::Custom(x) => x,
}
}
/// Returns the contained custom value or the default value.
pub fn unwrap_or_default(self) -> T
where
T: Default,
{
// we want to do this; the Clippy lint is not type-aware
#[allow(clippy::unwrap_or_default)]
self.unwrap_or_else(T::default)
}
}
impl<T> Smart<Smart<T>> {
/// Removes a single level of nesting, returns `Auto` if the inner or outer value is `Auto`.
pub fn flatten(self) -> Smart<T> {
match self {
Smart::Custom(Smart::Auto) | Smart::Auto => Smart::Auto,
Smart::Custom(Smart::Custom(v)) => Smart::Custom(v),
}
}
}
impl<T> Default for Smart<T> {
fn default() -> Self {
Self::Auto
}
}
impl<T: Reflect> Reflect for Smart<T> {
fn input() -> CastInfo {
T::input() + AutoValue::input()
}
fn output() -> CastInfo {
T::output() + AutoValue::output()
}
fn castable(value: &Value) -> bool {
AutoValue::castable(value) || T::castable(value)
}
}
impl<T: IntoValue> IntoValue for Smart<T> {
fn into_value(self) -> Value {
match self {
Smart::Custom(v) => v.into_value(),
Smart::Auto => Value::Auto,
}
}
}
impl<T: FromValue> FromValue for Smart<T> {
fn from_value(value: Value) -> HintedStrResult<Self> {
match value {
Value::Auto => Ok(Self::Auto),
v if T::castable(&v) => Ok(Self::Custom(T::from_value(v)?)),
_ => Err(Self::error(&value)),
}
}
}
impl<T: Resolve> Resolve for Smart<T> {
type Output = Smart<T::Output>;
fn resolve(self, styles: StyleChain) -> Self::Output {
self.map(|v| v.resolve(styles))
}
}
impl<T: Fold> Fold for Smart<T> {
fn fold(self, outer: Self) -> Self {
use Smart::Custom;
match (self, outer) {
(Custom(inner), Custom(outer)) => Custom(inner.fold(outer)),
// An explicit `auto` should be respected, thus we don't do
// `inner.or(outer)`.
(inner, _) => inner,
}
}
}