| 1 | pub(crate) use spanned::*; | 
| 2 |  | 
|---|
| 3 | /// An optional spanned value. | 
|---|
| 4 | #[ derive(Debug, Clone)] | 
|---|
| 5 | pub struct OptWithLine<T>(Option<Spanned<T>>); | 
|---|
| 6 |  | 
|---|
| 7 | impl<T> std::ops::Deref for OptWithLine<T> { | 
|---|
| 8 | type Target = Option<Spanned<T>>; | 
|---|
| 9 |  | 
|---|
| 10 | fn deref(&self) -> &Self::Target { | 
|---|
| 11 | &self.0 | 
|---|
| 12 | } | 
|---|
| 13 | } | 
|---|
| 14 |  | 
|---|
| 15 | impl<T> From<Option<Spanned<T>>> for OptWithLine<T> { | 
|---|
| 16 | fn from(value: Option<Spanned<T>>) -> Self { | 
|---|
| 17 | Self(value) | 
|---|
| 18 | } | 
|---|
| 19 | } | 
|---|
| 20 |  | 
|---|
| 21 | impl<T> From<Spanned<T>> for OptWithLine<T> { | 
|---|
| 22 | fn from(value: Spanned<T>) -> Self { | 
|---|
| 23 | Self(Some(value)) | 
|---|
| 24 | } | 
|---|
| 25 | } | 
|---|
| 26 |  | 
|---|
| 27 | impl<T> Default for OptWithLine<T> { | 
|---|
| 28 | fn default() -> Self { | 
|---|
| 29 | Self(Default::default()) | 
|---|
| 30 | } | 
|---|
| 31 | } | 
|---|
| 32 |  | 
|---|
| 33 | impl<T> OptWithLine<T> { | 
|---|
| 34 | /// Creates a new optional spanned value. | 
|---|
| 35 | pub fn new(data: T, span: Span) -> Self { | 
|---|
| 36 | Self(Some(Spanned::new(data, span))) | 
|---|
| 37 | } | 
|---|
| 38 |  | 
|---|
| 39 | /// Tries to set the value if not already set. Returns newly passed | 
|---|
| 40 | /// value in case there was already a value there. | 
|---|
| 41 | #[ must_use] | 
|---|
| 42 | pub fn set(&mut self, data: T, span: Span) -> Option<Spanned<T>> { | 
|---|
| 43 | let new = Spanned::new(data, span); | 
|---|
| 44 | if self.0.is_some() { | 
|---|
| 45 | Some(new) | 
|---|
| 46 | } else { | 
|---|
| 47 | self.0 = Some(new); | 
|---|
| 48 | None | 
|---|
| 49 | } | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | /// Consumes `self` and returns the inner value. | 
|---|
| 53 | #[ must_use] | 
|---|
| 54 | pub fn into_inner(self) -> Option<Spanned<T>> { | 
|---|
| 55 | self.0 | 
|---|
| 56 | } | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|