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
use anyhow::{anyhow, Result};
use format_buf::format;

use std::any::Any;

use super::{as_arguments_0, as_arguments_1, FormatSpecifier, Type, Value};

impl<T: 'static + Value + Clone> Value for Option<T> {
	fn call(&self, func: &str, args: &[Box<dyn Value>]) -> Result<Box<dyn Value>> {
		match func {
			"is_none" => {
				as_arguments_0(args)?;
				Ok(Box::new(self.is_none()))
			}
			"is_some" => {
				as_arguments_0(args)?;
				Ok(Box::new(self.is_some()))
			}
			"expect" => {
				let (message,) = as_arguments_1::<String>(args)?;
				self
					.clone()
					.map(|value| Box::new(value) as Box<dyn Value>)
					.ok_or_else(|| {
						anyhow!("Could not unwrap Option (object does not contain a value)").context(message.to_string())
					})
			}
			"unwrap" => {
				as_arguments_0(args)?;
				self
					.clone()
					.map(|value| Box::new(value) as Box<dyn Value>)
					.ok_or_else(|| anyhow!("Could not unwrap Option (object does not contain a value)"))
			}
			"?" => {
				as_arguments_0(args)?;
				self
					.clone()
					.map(|value| Box::new(value) as Box<dyn Value>)
					.ok_or_else(|| anyhow!("Could not unwrap Option (object does not contain a value)"))
			}
			_ => self.call_base(func, args),
		}
	}

	fn get_type(&self) -> Type {
		Type::Option
	}

	fn as_any(&self) -> &dyn Any {
		self
	}

	fn format(&self, buffer: &mut String, spec: FormatSpecifier) {
		match spec {
			FormatSpecifier::Default => match self {
				Some(value) => value.format(buffer, spec),
				None => *buffer += "None",
			},
			FormatSpecifier::Debug => format!(buffer, "{:?}", self),
			FormatSpecifier::DebugAlt => format!(buffer, "{:#?}", self),
		}
	}
}