runtime/globals/fetch/response/
options.rs

1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 */
6
7use std::fmt;
8use std::fmt::{Display, Formatter};
9
10use http::StatusCode;
11use ion::conversions::FromValue as _;
12use ion::{Context, Error, ErrorKind, FromValue, Result, Traceable, Value};
13use mozjs::conversions::ConversionBehavior;
14
15use crate::globals::fetch::header::HeadersInit;
16
17#[derive(Default, FromValue)]
18pub struct ResponseInit<'cx> {
19	#[ion(default)]
20	pub(crate) headers: HeadersInit<'cx>,
21
22	#[ion(default, parser = |s| parse_status(__ion_cx, &s))]
23	pub(crate) status: StatusCode,
24	#[ion(default)]
25	pub(crate) status_text: Option<String>,
26}
27
28fn parse_status(cx: &Context, status: &Value) -> Result<StatusCode> {
29	let code = u16::from_value(cx, status, true, ConversionBehavior::Clamp).map(StatusCode::from_u16);
30	match code {
31		Ok(Ok(code)) => Ok(code),
32		_ => Err(Error::new("Invalid response status code", ErrorKind::Range)),
33	}
34}
35
36#[derive(Clone, Copy, Debug, Default, PartialEq, Traceable)]
37pub enum ResponseKind {
38	Basic,
39	Cors,
40	#[default]
41	Default,
42	Error,
43	Opaque,
44	OpaqueRedirect,
45}
46
47impl Display for ResponseKind {
48	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
49		match self {
50			ResponseKind::Basic => f.write_str("basic"),
51			ResponseKind::Cors => f.write_str("cors"),
52			ResponseKind::Default => f.write_str("default"),
53			ResponseKind::Error => f.write_str("error"),
54			ResponseKind::Opaque => f.write_str("opaque"),
55			ResponseKind::OpaqueRedirect => f.write_str("opaqueredirect"),
56		}
57	}
58}
59
60#[derive(Clone, Copy, Debug, Default, PartialEq)]
61pub enum ResponseTaint {
62	#[default]
63	Basic,
64	Cors,
65	Opaque,
66}