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
//! Exposes the [`Owned`](trait.Owned.html) that nodes implement to produce an owned version of themselves.
//! Owned versions are represented as the node with a lifetime of `'static`. For example, if you have
//! an [`Ast<'a>`](../struct.Ast.html), calling `ast.owned()` on it will produce an owned `Ast<'static>`.
use super::*;
use crate::tokenizer::*;

use std::borrow::Cow;

/// A trait for getting an owned version of a node.
/// Refer to the [module documentation](index.html) for more details.
/// This trait is sealed and cannot be implemented for types outside of `full-moon`
pub trait Owned
where
    Self: crate::private::Sealed,
{
    /// What an owned version of the object looks like. Usually contains a `'static` lifetime.
    type Owned;
    /// Returns an owned version of the object.
    fn owned(&self) -> Self::Owned;
}

impl Owned for AstError<'_> {
    type Owned = AstError<'static>;

    fn owned(&self) -> Self::Owned {
        match self {
            AstError::UnexpectedToken { token, additional } => AstError::UnexpectedToken {
                additional: additional.clone().map(Cow::into_owned).map(Cow::Owned),
                token: token.owned(),
            },

            AstError::Empty => AstError::Empty,
            AstError::NoEof => AstError::NoEof,
        }
    }
}

impl<T> Owned for Pair<'_, T>
where
    T: Owned,
{
    type Owned = Pair<'static, <T as Owned>::Owned>;

    fn owned(&self) -> Self::Owned {
        match self {
            Pair::End(token) => Pair::End(token.owned()),
            Pair::Punctuated(token, sep) => Pair::Punctuated(token.owned(), sep.owned()),
        }
    }
}

impl<T> Owned for Punctuated<'_, T>
where
    T: Owned,
{
    type Owned = Punctuated<'static, <T as Owned>::Owned>;

    fn owned(&self) -> Self::Owned {
        let mut owned = Punctuated::new();
        owned.extend(&mut self.pairs().map(Pair::owned));
        owned
    }
}

impl Owned for Token<'_> {
    type Owned = Token<'static>;

    fn owned(&self) -> Self::Owned {
        Token {
            start_position: self.start_position,
            end_position: self.end_position,
            token_type: self.token_type().owned(),
        }
    }
}

impl Owned for TokenizerError {
    type Owned = TokenizerError;

    fn owned(&self) -> Self::Owned {
        self.clone()
    }
}

impl Owned for TokenType<'_> {
    type Owned = TokenType<'static>;

    fn owned(&self) -> Self::Owned {
        match self {
            TokenType::Eof => TokenType::Eof,
            TokenType::Identifier { identifier } => TokenType::Identifier {
                identifier: Cow::Owned(identifier.clone().into_owned()),
            },
            TokenType::MultiLineComment { blocks, comment } => TokenType::MultiLineComment {
                blocks: *blocks,
                comment: Cow::Owned(comment.clone().into_owned()),
            },
            TokenType::Number { text } => TokenType::Number {
                text: Cow::Owned(text.clone().into_owned()),
            },
            TokenType::SingleLineComment { comment } => TokenType::SingleLineComment {
                comment: Cow::Owned(comment.clone().into_owned()),
            },
            TokenType::StringLiteral {
                literal,
                multi_line,
                quote_type,
            } => TokenType::StringLiteral {
                literal: Cow::Owned(literal.clone().into_owned()),
                multi_line: *multi_line,
                quote_type: *quote_type,
            },
            TokenType::Symbol { symbol } => TokenType::Symbol { symbol: *symbol },
            TokenType::Whitespace { characters } => TokenType::Whitespace {
                characters: Cow::Owned(characters.clone().into_owned()),
            },
        }
    }
}

impl<T> Owned for Box<T>
where
    T: Owned,
{
    type Owned = Box<<T as Owned>::Owned>;

    fn owned(&self) -> Self::Owned {
        Box::new((**self).owned())
    }
}

impl<T> Owned for Option<T>
where
    T: Owned,
{
    type Owned = Option<<T as Owned>::Owned>;

    fn owned(&self) -> Self::Owned {
        Some(self.as_ref()?.owned())
    }
}

impl<T> Owned for Vec<T>
where
    T: Owned,
{
    type Owned = Vec<<T as Owned>::Owned>;

    fn owned(&self) -> Self::Owned {
        self.iter().map(Owned::owned).collect()
    }
}

impl<A, B> Owned for (A, B)
where
    A: Owned,
    B: Owned,
{
    type Owned = (<A as Owned>::Owned, <B as Owned>::Owned);

    fn owned(&self) -> Self::Owned {
        (self.0.owned(), self.1.owned())
    }
}

// TODO: Generic Cow impl
impl<'a> Owned for Cow<'a, TokenReference<'a>> {
    type Owned = Cow<'static, TokenReference<'static>>;

    fn owned(&self) -> <Self as Owned>::Owned {
        Cow::Owned((*self.to_owned()).owned())
    }
}