use alloc::format;
use alloc::string::{String, ToString};
use crate::classes::object;
use crate::runtime::{Detail, Runtime, RuntimeError, RuntimeFailure, TypeName, Value};
const CONSTRUCTOR: &str = "ClipboardAll.Prototype.__New";
pub(crate) fn construct(
arguments: &[Value],
_rt: &mut Runtime,
line: u32,
) -> Result<Value, RuntimeFailure> {
if arguments.is_empty() {
return Ok(object("clipboardall"));
}
let data = &arguments[0];
match data {
Value::Text(_) => {
return Err(RuntimeFailure::Detailed(
RuntimeError {
message: crate::runtime::requires(1, CONSTRUCTOR, TypeName::Number, data),
line,
},
Detail {
what: CONSTRUCTOR.to_string(),
extra: data.to_text(),
},
));
}
Value::Int(_) | Value::Float(_) => {
let address = data.to_number().unwrap_or(0.0);
if address == 0.0 || crate::runtime::fract(address) != 0.0 {
return Err(RuntimeFailure::Detailed(
RuntimeError {
message: format!("Parameter #1 of {CONSTRUCTOR} is invalid."),
line,
},
Detail {
what: CONSTRUCTOR.to_string(),
extra: data.to_text(),
},
));
}
if arguments.len() < 2 {
return Err(RuntimeFailure::Detailed(
RuntimeError {
message: format!(
"Parameter #2 of {CONSTRUCTOR} must not be omitted in this case."
),
line,
},
Detail {
what: CONSTRUCTOR.to_string(),
extra: String::new(),
},
));
}
}
_ => {}
}
Err(RuntimeFailure::Error(RuntimeError {
message: "the binary data of the clipboard is not carried by this runtime.".to_string(),
line,
}))
}