1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 15:24:46 -05:00

Remove some more unnecessary 'to_string()' calls (#7190)

This commit is contained in:
Bert Belder 2020-08-26 00:48:25 +02:00
parent 9bfb0df805
commit aead9dc91e
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461
10 changed files with 42 additions and 67 deletions

View file

@ -44,12 +44,9 @@ pub fn create_http_client(ca_file: Option<&str>) -> Result<Client, ErrBox> {
builder = builder.add_root_certificate(cert); builder = builder.add_root_certificate(cert);
} }
builder.build().map_err(|_| { builder
ErrBox::from(io::Error::new( .build()
io::ErrorKind::Other, .map_err(|_| ErrBox::error("Unable to build http client"))
"Unable to build http client".to_string(),
))
})
} }
/// Construct the next uri based on base uri and location header fragment /// Construct the next uri based on base uri and location header fragment
/// See <https://tools.ietf.org/html/rfc3986#section-4.2> /// See <https://tools.ietf.org/html/rfc3986#section-4.2>

View file

@ -76,8 +76,10 @@ fn validate_no_file_from_remote(
match specifier_url.scheme() { match specifier_url.scheme() {
"http" | "https" => {} "http" | "https" => {}
_ => { _ => {
let e = ErrBox::new("PermissionDenied", let e = ErrBox::new(
"Remote modules are not allowed to statically import local modules. Use dynamic import instead.".to_string() "PermissionDenied",
"Remote modules are not allowed to statically import local \
modules. Use dynamic import instead.",
); );
return Err(err_with_location(e, maybe_location)); return Err(err_with_location(e, maybe_location));
} }

View file

@ -89,7 +89,7 @@ fn test_error_record() {
arg: -1, arg: -1,
error_len: 11, error_len: 11,
error_class: b"BadResource", error_class: b"BadResource",
error_message: "Error".to_string().as_bytes().to_owned(), error_message: b"Error".to_vec(),
}; };
let buf: Buf = err_record.into(); let buf: Buf = err_record.into();
assert_eq!(buf, expected.into_boxed_slice()); assert_eq!(buf, expected.into_boxed_slice());

View file

@ -199,9 +199,7 @@ fn op_seek(
let mut resource_table = resource_table.borrow_mut(); let mut resource_table = resource_table.borrow_mut();
let pos = std_file_resource(&mut resource_table, rid, |r| match r { let pos = std_file_resource(&mut resource_table, rid, |r| match r {
Ok(std_file) => std_file.seek(seek_from).map_err(ErrBox::from), Ok(std_file) => std_file.seek(seek_from).map_err(ErrBox::from),
Err(_) => Err(ErrBox::type_error( Err(_) => Err(ErrBox::type_error("cannot seek on this type of resource")),
"cannot seek on this type of resource".to_string(),
)),
})?; })?;
Ok(JsonOp::Sync(json!(pos))) Ok(JsonOp::Sync(json!(pos)))
} else { } else {
@ -211,9 +209,9 @@ fn op_seek(
let mut resource_table = resource_table.borrow_mut(); let mut resource_table = resource_table.borrow_mut();
let pos = std_file_resource(&mut resource_table, rid, |r| match r { let pos = std_file_resource(&mut resource_table, rid, |r| match r {
Ok(std_file) => std_file.seek(seek_from).map_err(ErrBox::from), Ok(std_file) => std_file.seek(seek_from).map_err(ErrBox::from),
Err(_) => Err(ErrBox::type_error( Err(_) => {
"cannot seek on this type of resource".to_string(), Err(ErrBox::type_error("cannot seek on this type of resource"))
)), }
})?; })?;
Ok(json!(pos)) Ok(json!(pos))
}; };
@ -245,9 +243,7 @@ fn op_fdatasync(
let mut resource_table = resource_table.borrow_mut(); let mut resource_table = resource_table.borrow_mut();
std_file_resource(&mut resource_table, rid, |r| match r { std_file_resource(&mut resource_table, rid, |r| match r {
Ok(std_file) => std_file.sync_data().map_err(ErrBox::from), Ok(std_file) => std_file.sync_data().map_err(ErrBox::from),
Err(_) => Err(ErrBox::type_error( Err(_) => Err(ErrBox::type_error("cannot sync this type of resource")),
"cannot sync this type of resource".to_string(),
)),
})?; })?;
Ok(JsonOp::Sync(json!({}))) Ok(JsonOp::Sync(json!({})))
} else { } else {
@ -255,9 +251,7 @@ fn op_fdatasync(
let mut resource_table = resource_table.borrow_mut(); let mut resource_table = resource_table.borrow_mut();
std_file_resource(&mut resource_table, rid, |r| match r { std_file_resource(&mut resource_table, rid, |r| match r {
Ok(std_file) => std_file.sync_data().map_err(ErrBox::from), Ok(std_file) => std_file.sync_data().map_err(ErrBox::from),
Err(_) => Err(ErrBox::type_error( Err(_) => Err(ErrBox::type_error("cannot sync this type of resource")),
"cannot sync this type of resource".to_string(),
)),
})?; })?;
Ok(json!({})) Ok(json!({}))
}; };
@ -289,9 +283,7 @@ fn op_fsync(
let mut resource_table = resource_table.borrow_mut(); let mut resource_table = resource_table.borrow_mut();
std_file_resource(&mut resource_table, rid, |r| match r { std_file_resource(&mut resource_table, rid, |r| match r {
Ok(std_file) => std_file.sync_all().map_err(ErrBox::from), Ok(std_file) => std_file.sync_all().map_err(ErrBox::from),
Err(_) => Err(ErrBox::type_error( Err(_) => Err(ErrBox::type_error("cannot sync this type of resource")),
"cannot sync this type of resource".to_string(),
)),
})?; })?;
Ok(JsonOp::Sync(json!({}))) Ok(JsonOp::Sync(json!({})))
} else { } else {
@ -299,9 +291,7 @@ fn op_fsync(
let mut resource_table = resource_table.borrow_mut(); let mut resource_table = resource_table.borrow_mut();
std_file_resource(&mut resource_table, rid, |r| match r { std_file_resource(&mut resource_table, rid, |r| match r {
Ok(std_file) => std_file.sync_all().map_err(ErrBox::from), Ok(std_file) => std_file.sync_all().map_err(ErrBox::from),
Err(_) => Err(ErrBox::type_error( Err(_) => Err(ErrBox::type_error("cannot sync this type of resource")),
"cannot sync this type of resource".to_string(),
)),
})?; })?;
Ok(json!({})) Ok(json!({}))
}; };
@ -333,9 +323,7 @@ fn op_fstat(
let mut resource_table = resource_table.borrow_mut(); let mut resource_table = resource_table.borrow_mut();
let metadata = std_file_resource(&mut resource_table, rid, |r| match r { let metadata = std_file_resource(&mut resource_table, rid, |r| match r {
Ok(std_file) => std_file.metadata().map_err(ErrBox::from), Ok(std_file) => std_file.metadata().map_err(ErrBox::from),
Err(_) => Err(ErrBox::type_error( Err(_) => Err(ErrBox::type_error("cannot stat this type of resource")),
"cannot stat this type of resource".to_string(),
)),
})?; })?;
Ok(JsonOp::Sync(get_stat_json(metadata).unwrap())) Ok(JsonOp::Sync(get_stat_json(metadata).unwrap()))
} else { } else {
@ -344,9 +332,9 @@ fn op_fstat(
let metadata = let metadata =
std_file_resource(&mut resource_table, rid, |r| match r { std_file_resource(&mut resource_table, rid, |r| match r {
Ok(std_file) => std_file.metadata().map_err(ErrBox::from), Ok(std_file) => std_file.metadata().map_err(ErrBox::from),
Err(_) => Err(ErrBox::type_error( Err(_) => {
"cannot stat this type of resource".to_string(), Err(ErrBox::type_error("cannot stat this type of resource"))
)), }
})?; })?;
Ok(get_stat_json(metadata).unwrap()) Ok(get_stat_json(metadata).unwrap())
}; };
@ -879,7 +867,7 @@ fn op_symlink(
Some(options) => match options._type.as_ref() { Some(options) => match options._type.as_ref() {
"file" => symlink_file(&oldpath, &newpath)?, "file" => symlink_file(&oldpath, &newpath)?,
"dir" => symlink_dir(&oldpath, &newpath)?, "dir" => symlink_dir(&oldpath, &newpath)?,
_ => return Err(ErrBox::type_error("unsupported type".to_string())), _ => return Err(ErrBox::type_error("unsupported type")),
}, },
None => { None => {
let old_meta = std::fs::metadata(&oldpath); let old_meta = std::fs::metadata(&oldpath);
@ -955,9 +943,9 @@ fn op_ftruncate(
let mut resource_table = resource_table.borrow_mut(); let mut resource_table = resource_table.borrow_mut();
std_file_resource(&mut resource_table, rid, |r| match r { std_file_resource(&mut resource_table, rid, |r| match r {
Ok(std_file) => std_file.set_len(len).map_err(ErrBox::from), Ok(std_file) => std_file.set_len(len).map_err(ErrBox::from),
Err(_) => Err(ErrBox::type_error( Err(_) => {
"cannot truncate this type of resource".to_string(), Err(ErrBox::type_error("cannot truncate this type of resource"))
)), }
})?; })?;
Ok(JsonOp::Sync(json!({}))) Ok(JsonOp::Sync(json!({})))
} else { } else {
@ -965,9 +953,9 @@ fn op_ftruncate(
let mut resource_table = resource_table.borrow_mut(); let mut resource_table = resource_table.borrow_mut();
std_file_resource(&mut resource_table, rid, |r| match r { std_file_resource(&mut resource_table, rid, |r| match r {
Ok(std_file) => std_file.set_len(len).map_err(ErrBox::from), Ok(std_file) => std_file.set_len(len).map_err(ErrBox::from),
Err(_) => Err(ErrBox::type_error( Err(_) => {
"cannot truncate this type of resource".to_string(), Err(ErrBox::type_error("cannot truncate this type of resource"))
)), }
})?; })?;
Ok(json!({})) Ok(json!({}))
}; };

View file

@ -117,7 +117,7 @@ fn get_stdio_stream(
} }
fn no_buffer_specified() -> ErrBox { fn no_buffer_specified() -> ErrBox {
ErrBox::type_error("no buffer specified".to_string()) ErrBox::type_error("no buffer specified")
} }
#[cfg(unix)] #[cfg(unix)]
@ -262,9 +262,9 @@ pub fn op_read(
.map(|n: usize| n as i32) .map(|n: usize| n as i32)
.map_err(ErrBox::from) .map_err(ErrBox::from)
} }
Err(_) => Err(ErrBox::type_error( Err(_) => {
"sync read not allowed on this resource".to_string(), Err(ErrBox::type_error("sync read not allowed on this resource"))
)), }
}) })
}) })
} else { } else {
@ -383,9 +383,9 @@ pub fn op_write(
.map(|nwritten: usize| nwritten as i32) .map(|nwritten: usize| nwritten as i32)
.map_err(ErrBox::from) .map_err(ErrBox::from)
} }
Err(_) => Err(ErrBox::type_error( Err(_) => {
"sync read not allowed on this resource".to_string(), Err(ErrBox::type_error("sync read not allowed on this resource"))
)), }
}) })
}) })
} else { } else {

View file

@ -53,9 +53,7 @@ fn accept_tcp(
let mut resource_table = resource_table.borrow_mut(); let mut resource_table = resource_table.borrow_mut();
let listener_resource = resource_table let listener_resource = resource_table
.get_mut::<TcpListenerResource>(rid) .get_mut::<TcpListenerResource>(rid)
.ok_or_else(|| { .ok_or_else(|| ErrBox::bad_resource("Listener has been closed"))?;
ErrBox::bad_resource("Listener has been closed".to_string())
})?;
let listener = &mut listener_resource.listener; let listener = &mut listener_resource.listener;
match listener.poll_accept(cx).map_err(ErrBox::from) { match listener.poll_accept(cx).map_err(ErrBox::from) {
Poll::Ready(Ok((stream, addr))) => { Poll::Ready(Ok((stream, addr))) => {
@ -142,9 +140,7 @@ fn receive_udp(
let mut resource_table = resource_table.borrow_mut(); let mut resource_table = resource_table.borrow_mut();
let resource = resource_table let resource = resource_table
.get_mut::<UdpSocketResource>(rid) .get_mut::<UdpSocketResource>(rid)
.ok_or_else(|| { .ok_or_else(|| ErrBox::bad_resource("Socket has been closed"))?;
ErrBox::bad_resource("Socket has been closed".to_string())
})?;
let socket = &mut resource.socket; let socket = &mut resource.socket;
socket socket
.poll_recv_from(cx, &mut zero_copy) .poll_recv_from(cx, &mut zero_copy)
@ -216,9 +212,7 @@ fn op_datagram_send(
let mut resource_table = resource_table.borrow_mut(); let mut resource_table = resource_table.borrow_mut();
let resource = resource_table let resource = resource_table
.get_mut::<UdpSocketResource>(rid as u32) .get_mut::<UdpSocketResource>(rid as u32)
.ok_or_else(|| { .ok_or_else(|| ErrBox::bad_resource("Socket has been closed"))?;
ErrBox::bad_resource("Socket has been closed".to_string())
})?;
resource resource
.socket .socket
.poll_send_to(cx, &zero_copy, &addr) .poll_send_to(cx, &zero_copy, &addr)

View file

@ -43,9 +43,7 @@ pub fn accept_unix(
let listener_resource = { let listener_resource = {
resource_table_ resource_table_
.get_mut::<UnixListenerResource>(rid) .get_mut::<UnixListenerResource>(rid)
.ok_or_else(|| { .ok_or_else(|| ErrBox::bad_resource("Listener has been closed"))?
ErrBox::bad_resource("Listener has been closed".to_string())
})?
}; };
let (unix_stream, _socket_addr) = let (unix_stream, _socket_addr) =
@ -90,9 +88,7 @@ pub fn receive_unix_packet(
let mut resource_table_ = resource_table.borrow_mut(); let mut resource_table_ = resource_table.borrow_mut();
let resource = resource_table_ let resource = resource_table_
.get_mut::<UnixDatagramResource>(rid) .get_mut::<UnixDatagramResource>(rid)
.ok_or_else(|| { .ok_or_else(|| ErrBox::bad_resource("Socket has been closed"))?;
ErrBox::bad_resource("Socket has been closed".to_string())
})?;
let (size, remote_addr) = resource.socket.recv_from(&mut zero_copy).await?; let (size, remote_addr) = resource.socket.recv_from(&mut zero_copy).await?;
Ok(json!({ Ok(json!({
"size": size, "size": size,

View file

@ -371,9 +371,7 @@ fn op_accept_tls(
let mut resource_table = resource_table.borrow_mut(); let mut resource_table = resource_table.borrow_mut();
let listener_resource = resource_table let listener_resource = resource_table
.get_mut::<TlsListenerResource>(rid) .get_mut::<TlsListenerResource>(rid)
.ok_or_else(|| { .ok_or_else(|| ErrBox::bad_resource("Listener has been closed"))?;
ErrBox::bad_resource("Listener has been closed".to_string())
})?;
let listener = &mut listener_resource.listener; let listener = &mut listener_resource.listener;
match listener.poll_accept(cx).map_err(ErrBox::from) { match listener.poll_accept(cx).map_err(ErrBox::from) {
Poll::Ready(Ok((stream, addr))) => { Poll::Ready(Ok((stream, addr))) => {

View file

@ -49,7 +49,7 @@ pub fn kill(pid: i32, signal: i32) -> Result<(), ErrBox> {
} }
} }
_ => { _ => {
return Err(ErrBox::type_error("unsupported signal".to_string())); return Err(ErrBox::type_error("unsupported signal"));
} }
} }
Ok(()) Ok(())

View file

@ -450,7 +450,7 @@ impl fmt::Debug for ErrWithV8Handle {
mod tests { mod tests {
#[test] #[test]
fn test_bad_resource() { fn test_bad_resource() {
let err = ErrBox::bad_resource("Resource has been closed".to_string()); let err = ErrBox::bad_resource("Resource has been closed");
assert_eq!(err.1, "BadResource"); assert_eq!(err.1, "BadResource");
assert_eq!(err.to_string(), "Resource has been closed"); assert_eq!(err.to_string(), "Resource has been closed");
} }