netdoc: remove ArgError.

This commit is contained in:
Nick Mathewson 2020-05-15 08:47:32 -04:00
parent dea1530492
commit d82cb7e5d1
2 changed files with 23 additions and 44 deletions

View File

@ -9,35 +9,17 @@ pub use b64impl::*;
pub use curve25519impl::*;
pub use timeimpl::*;
use thiserror::Error;
/// A problem that can occur when parsing an argument.
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum ArgError {
/// Invalid base64.
#[error("bad base64 encoding: {0}")]
Base64(#[from] base64::DecodeError),
/// A time that was in the wrong form, or out of range.
#[error("invalid time: {0}")]
BadTime(#[from] chrono::ParseError),
/// Some other error, represented as a string.
#[error("{0}")]
Generic(&'static str),
}
mod b64impl {
use super::ArgError;
use crate::{Error, Pos, Result};
pub struct B64(Vec<u8>);
impl std::str::FromStr for B64 {
type Err = ArgError;
fn from_str(s: &str) -> Result<Self, ArgError> {
Ok(B64(
base64::decode_config(s, base64::STANDARD_NO_PAD).map_err(ArgError::Base64)?
))
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
let bytes = base64::decode_config(s, base64::STANDARD_NO_PAD)
.map_err(|e| Error::BadArgument(0, Pos::at(s), format!("Invalid base64: {}", e)))?;
Ok(B64(bytes))
}
}
@ -57,20 +39,20 @@ mod b64impl {
// ============================================================
mod curve25519impl {
use super::{ArgError, B64};
use super::B64;
use crate::{Error, Pos, Result};
use std::convert::TryInto;
use tor_llcrypto::pk::curve25519::PublicKey;
pub struct Curve25519Public(PublicKey);
impl std::str::FromStr for Curve25519Public {
type Err = ArgError;
fn from_str(s: &str) -> Result<Self, ArgError> {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
let b64: B64 = s.parse()?;
let arry: [u8; 32] = b64
.as_bytes()
.try_into()
.map_err(|_| ArgError::Generic("wrong length"))?;
let arry: [u8; 32] = b64.as_bytes().try_into().map_err(|_| {
Error::BadArgument(0, Pos::at(s), "bad length for curve25519 key.".into())
})?;
Ok(Curve25519Public(arry.into()))
}
}
@ -85,17 +67,17 @@ mod curve25519impl {
// ============================================================
mod timeimpl {
use super::ArgError;
use crate::{Error, Pos, Result};
use std::time::SystemTime;
pub struct ISO8601TimeSp(SystemTime);
impl std::str::FromStr for ISO8601TimeSp {
type Err = ArgError;
fn from_str(s: &str) -> Result<ISO8601TimeSp, ArgError> {
type Err = Error;
fn from_str(s: &str) -> Result<ISO8601TimeSp> {
use chrono::{DateTime, NaiveDateTime, Utc};
let d =
NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S").map_err(ArgError::BadTime)?;
let d = NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S")
.map_err(|e| Error::BadArgument(0, Pos::at(s), format!("invalid time: {}", e)))?;
let dt = DateTime::<Utc>::from_utc(d, Utc);
Ok(ISO8601TimeSp(dt.into()))
}

View File

@ -396,14 +396,11 @@ impl RouterDesc {
let uptime = body.maybe(UPTIME).parse_arg(0)?;
// published time.
let published = {
let p_item = body.get_required(PUBLISHED)?;
let p: ISO8601TimeSp = p_item
.args_as_str()
.parse()
.map_err(|e: ArgError| Error::BadArgument(1, p_item.pos(), e.to_string()))?;
p.into()
};
let published = body
.get_required(PUBLISHED)?
.args_as_str()
.parse::<ISO8601TimeSp>()?
.into();
// ntor key
// XXXX technically this isn't "required"