Skip to content

Commit 48c3fb3

Browse files
authored
Hint that deprecated items will be removed in 0.15.0 (typst#6617)
1 parent 178c3be commit 48c3fb3

File tree

23 files changed

+160
-43
lines changed

23 files changed

+160
-43
lines changed

crates/typst-library/src/diag.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,18 +234,23 @@ impl From<SyntaxError> for SourceDiagnostic {
234234

235235
/// Destination for a deprecation message when accessing a deprecated value.
236236
pub trait DeprecationSink {
237-
/// Emits the given deprecation message into this sink.
238-
fn emit(self, message: &str);
237+
/// Emits the given deprecation message into this sink alongside a version
238+
/// in which the deprecated item is planned to be removed.
239+
fn emit(self, message: &str, until: Option<&str>);
239240
}
240241

241242
impl DeprecationSink for () {
242-
fn emit(self, _: &str) {}
243+
fn emit(self, _: &str, _: Option<&str>) {}
243244
}
244245

245246
impl DeprecationSink for (&mut Engine<'_>, Span) {
246247
/// Emits the deprecation message as a warning.
247-
fn emit(self, message: &str) {
248-
self.0.sink.warn(SourceDiagnostic::warning(self.1, message));
248+
fn emit(self, message: &str, version: Option<&str>) {
249+
self.0
250+
.sink
251+
.warn(SourceDiagnostic::warning(self.1, message).with_hints(
252+
version.map(|v| eco_format!("it will be removed in Typst {}", v)),
253+
));
249254
}
250255
}
251256

crates/typst-library/src/foundations/scope.rs

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@ pub struct Binding {
253253
span: Span,
254254
/// The category of the binding.
255255
category: Option<Category>,
256-
/// A deprecation message for the definition.
257-
deprecation: Option<&'static str>,
256+
/// The deprecation information if this item is deprecated.
257+
deprecation: Option<Box<Deprecation>>,
258258
}
259259

260260
/// The different kinds of slots.
@@ -284,8 +284,8 @@ impl Binding {
284284
}
285285

286286
/// Marks this binding as deprecated, with the given `message`.
287-
pub fn deprecated(&mut self, message: &'static str) -> &mut Self {
288-
self.deprecation = Some(message);
287+
pub fn deprecated(&mut self, deprecation: Deprecation) -> &mut Self {
288+
self.deprecation = Some(Box::new(deprecation));
289289
self
290290
}
291291

@@ -300,8 +300,8 @@ impl Binding {
300300
/// - pass `()` to ignore the message.
301301
/// - pass `(&mut engine, span)` to emit a warning into the engine.
302302
pub fn read_checked(&self, sink: impl DeprecationSink) -> &Value {
303-
if let Some(message) = self.deprecation {
304-
sink.emit(message);
303+
if let Some(info) = &self.deprecation {
304+
sink.emit(info.message, info.until);
305305
}
306306
&self.value
307307
}
@@ -337,8 +337,8 @@ impl Binding {
337337
}
338338

339339
/// A deprecation message for the value, if any.
340-
pub fn deprecation(&self) -> Option<&'static str> {
341-
self.deprecation
340+
pub fn deprecation(&self) -> Option<&Deprecation> {
341+
self.deprecation.as_deref()
342342
}
343343

344344
/// The category of the value, if any.
@@ -356,6 +356,51 @@ pub enum Capturer {
356356
Context,
357357
}
358358

359+
/// Information about a deprecated binding.
360+
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
361+
pub struct Deprecation {
362+
/// A deprecation message for the definition.
363+
message: &'static str,
364+
/// A version in which the deprecated binding is planned to be removed.
365+
until: Option<&'static str>,
366+
}
367+
368+
impl Deprecation {
369+
/// Creates new deprecation info with a default message to display when
370+
/// emitting the deprecation warning.
371+
pub fn new() -> Self {
372+
Self { message: "item is deprecated", until: None }
373+
}
374+
375+
/// Set the message to display when emitting the deprecation warning.
376+
pub fn with_message(mut self, message: &'static str) -> Self {
377+
self.message = message;
378+
self
379+
}
380+
381+
/// Set the version in which the binding is planned to be removed.
382+
pub fn with_until(mut self, version: &'static str) -> Self {
383+
self.until = Some(version);
384+
self
385+
}
386+
387+
/// The message to display when emitting the deprecation warning.
388+
pub fn message(&self) -> &'static str {
389+
self.message
390+
}
391+
392+
/// The version in which the binding is planned to be removed.
393+
pub fn until(&self) -> Option<&'static str> {
394+
self.until
395+
}
396+
}
397+
398+
impl Default for Deprecation {
399+
fn default() -> Self {
400+
Self::new()
401+
}
402+
}
403+
359404
/// The error message when trying to mutate a variable from the standard
360405
/// library.
361406
#[cold]

crates/typst-library/src/foundations/symbol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl Symbol {
151151
modifiers.best_match_in(list.variants().map(|(m, _, d)| (m, d)))
152152
{
153153
if let Some(message) = deprecation {
154-
sink.emit(message)
154+
sink.emit(message, None)
155155
}
156156
return Ok(self);
157157
}

crates/typst-library/src/loading/cbor.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ pub fn cbor(
3333
impl cbor {
3434
/// Reads structured data from CBOR bytes.
3535
#[func(title = "Decode CBOR")]
36-
#[deprecated = "`cbor.decode` is deprecated, directly pass bytes to `cbor` instead"]
36+
#[deprecated(
37+
message = "`cbor.decode` is deprecated, directly pass bytes to `cbor` instead",
38+
until = "0.15.0"
39+
)]
3740
pub fn decode(
3841
engine: &mut Engine,
3942
/// CBOR data.

crates/typst-library/src/loading/csv.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ pub fn csv(
9595
impl csv {
9696
/// Reads structured data from a CSV string/bytes.
9797
#[func(title = "Decode CSV")]
98-
#[deprecated = "`csv.decode` is deprecated, directly pass bytes to `csv` instead"]
98+
#[deprecated(
99+
message = "`csv.decode` is deprecated, directly pass bytes to `csv` instead",
100+
until = "0.15.0"
101+
)]
99102
pub fn decode(
100103
engine: &mut Engine,
101104
/// CSV data.

crates/typst-library/src/loading/json.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ pub fn json(
6767
impl json {
6868
/// Reads structured data from a JSON string/bytes.
6969
#[func(title = "Decode JSON")]
70-
#[deprecated = "`json.decode` is deprecated, directly pass bytes to `json` instead"]
70+
#[deprecated(
71+
message = "`json.decode` is deprecated, directly pass bytes to `json` instead",
72+
until = "0.15.0"
73+
)]
7174
pub fn decode(
7275
engine: &mut Engine,
7376
/// JSON data.

crates/typst-library/src/loading/toml.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ pub fn toml(
4141
impl toml {
4242
/// Reads structured data from a TOML string/bytes.
4343
#[func(title = "Decode TOML")]
44-
#[deprecated = "`toml.decode` is deprecated, directly pass bytes to `toml` instead"]
44+
#[deprecated(
45+
message = "`toml.decode` is deprecated, directly pass bytes to `toml` instead",
46+
until = "0.15.0"
47+
)]
4548
pub fn decode(
4649
engine: &mut Engine,
4750
/// TOML data.

crates/typst-library/src/loading/xml.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ pub fn xml(
7575
impl xml {
7676
/// Reads structured data from an XML string/bytes.
7777
#[func(title = "Decode XML")]
78-
#[deprecated = "`xml.decode` is deprecated, directly pass bytes to `xml` instead"]
78+
#[deprecated(
79+
message = "`xml.decode` is deprecated, directly pass bytes to `xml` instead",
80+
until = "0.15.0"
81+
)]
7982
pub fn decode(
8083
engine: &mut Engine,
8184
/// XML data.

crates/typst-library/src/loading/yaml.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ pub fn yaml(
5454
impl yaml {
5555
/// Reads structured data from a YAML string/bytes.
5656
#[func(title = "Decode YAML")]
57-
#[deprecated = "`yaml.decode` is deprecated, directly pass bytes to `yaml` instead"]
57+
#[deprecated(
58+
message = "`yaml.decode` is deprecated, directly pass bytes to `yaml` instead",
59+
until = "0.15.0"
60+
)]
5861
pub fn decode(
5962
engine: &mut Engine,
6063
/// YAML data.

crates/typst-library/src/symbols.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Modifiable symbols.
22
3-
use crate::foundations::{Module, Scope, Symbol, Value};
3+
use crate::foundations::{Deprecation, Module, Scope, Symbol, Value};
44

55
/// Hook up all `symbol` definitions.
66
pub(super) fn define(global: &mut Scope) {
@@ -23,7 +23,7 @@ fn extend_scope_from_codex_module(scope: &mut Scope, module: codex::Module) {
2323

2424
let scope_binding = scope.define(name, value);
2525
if let Some(message) = binding.deprecation {
26-
scope_binding.deprecated(message);
26+
scope_binding.deprecated(Deprecation::new().with_message(message));
2727
}
2828
}
2929
}

0 commit comments

Comments
 (0)