forc_migrate/modifying/
mod.rs

1//! This module contains common API for building new and modifying existing
2//! elements within a lexed tree.
3
4use sway_types::Span;
5
6mod annotated;
7mod attribute;
8mod expr;
9mod function;
10mod literal;
11mod module;
12mod path_expression_segment;
13mod storage_field;
14
15/// A wrapper around a lexed tree element that will be modified.
16pub(crate) struct Modifier<'a, T> {
17    element: &'a mut T,
18}
19
20impl<'a, T> Modifier<'a, T> {
21    // Private, so that we enforce creating modifiers with the
22    // `modify` function.
23    fn new(element: &'a mut T) -> Self {
24        Self { element }
25    }
26}
27
28pub(crate) fn modify<T>(element: &mut T) -> Modifier<'_, T> {
29    Modifier::new(element)
30}
31
32// Empty struct for creating new lexed elements.
33// Constructors for each lexed element are in separate modules,
34// grouped by lexed elements they construct, and each module
35// has its own `New` impl.
36pub(crate) struct New {}
37
38/// Trait for setting all spans within `Self` to the same insert span.
39///
40/// New elements inserted into lexed tree should have their spans set
41/// to the same zero-sized [Span]. This ensures that they will always
42/// be properly rendered. Sometimes, new elements are copied from existing
43/// elements and modified. Such new elements might not have all spans
44/// set to the same, zero-sized insert span. Implementing this trait
45/// ensures proper setting of the insert span.
46// TODO: Implement `SetInsertSpan` for lexed tree elements.
47#[allow(dead_code)]
48pub(crate) trait SetInsertSpan {
49    fn set_insert_span(&mut self, insert_span: Span);
50}
51
52#[macro_export]
53macro_rules! assert_insert_span {
54    ($insert_span: ident) => {
55        assert!(
56            stringify!($insert_span) == "insert_span",
57            "the insert span function argument must be called `insert_span`"
58        );
59        assert!($insert_span.is_empty(), "`insert_span` must be empty");
60        assert!(
61            !$insert_span.is_dummy(),
62            "`insert_span` must not be a `Span::dummy()`"
63        );
64    };
65}