0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-01 09:24:20 -04:00
denoland-deno/cli/ops/idna.rs
Bert Belder 9bfb0df805
refactor: remove OpError, use ErrBox everywhere (#7187)
Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
2020-08-26 00:22:15 +02:00

40 lines
1.1 KiB
Rust

// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
//! https://url.spec.whatwg.org/#idna
use super::dispatch_json::{Deserialize, JsonOp, Value};
use crate::state::State;
use deno_core::CoreIsolate;
use deno_core::ErrBox;
use deno_core::ZeroCopyBuf;
use idna::{domain_to_ascii, domain_to_ascii_strict};
use std::rc::Rc;
pub fn init(i: &mut CoreIsolate, s: &Rc<State>) {
i.register_op("op_domain_to_ascii", s.stateful_json_op(op_domain_to_ascii));
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct DomainToAscii {
domain: String,
be_strict: bool,
}
fn op_domain_to_ascii(
_state: &Rc<State>,
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<JsonOp, ErrBox> {
let args: DomainToAscii = serde_json::from_value(args)?;
if args.be_strict {
domain_to_ascii_strict(args.domain.as_str())
} else {
domain_to_ascii(args.domain.as_str())
}
.map_err(|err| {
let message = format!("Invalid IDNA encoded domain name: {:?}", err);
ErrBox::new("URIError", message)
})
.map(|domain| JsonOp::Sync(json!(domain)))
}