From 47330af38568fc8daf58df6ea8829371332d6d6d Mon Sep 17 00:00:00 2001 From: Jun Kurihara Date: Wed, 7 Feb 2024 16:28:10 +0900 Subject: [PATCH] chore: update api --- lib/src/message_component/component.rs | 30 +++++++++++++------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/src/message_component/component.rs b/lib/src/message_component/component.rs index 65622fa..a581e53 100644 --- a/lib/src/message_component/component.rs +++ b/lib/src/message_component/component.rs @@ -1,6 +1,6 @@ +use super::parse::{build_derived_component, build_http_field_component}; use anyhow::{bail, ensure}; use rustc_hash::FxHashSet as HashSet; -use super::parse::{build_derived_component, build_http_field_component}; /* ---------------------------------------------------------------- */ #[derive(Debug, Clone)] @@ -15,12 +15,19 @@ pub struct HttpMessageComponent { impl TryFrom<&str> for HttpMessageComponent { type Error = anyhow::Error; /// Create HttpMessageComponent from serialized string, i.e., `"": ` of lines in the signature base of HTTP header. + /// We suppose that the value was correctly serialized as a line of signature base. fn try_from(val: &str) -> Result { let Some((id, value)) = val.split_once(':') else { bail!("Invalid http message component: {}", val); }; let id = id.trim(); - ensure_component_id(id)?; + + // check if id is wrapped by double quotations + ensure!( + id.starts_with('"') && (id.ends_with('"') || id[1..].contains("\";")), + "Invalid http message component id: {}", + id + ); Ok(Self { id: HttpMessageComponentId::try_from(id)?, @@ -30,22 +37,15 @@ impl TryFrom<&str> for HttpMessageComponent { } impl TryFrom<(&HttpMessageComponentId, &[String])> for HttpMessageComponent { - type Error = anyhow::Error; + type Error = anyhow::Error; - /// Build http message component from given id and its associated field values - fn try_from((id, field_values): (&HttpMessageComponentId, &[String])) -> Result { - match &id.name { - HttpMessageComponentName::HttpField(_) => build_http_field_component(id, field_values), - HttpMessageComponentName::Derived(_) => build_derived_component(id, field_values), - } + /// Build http message component from given id and its associated field values + fn try_from((id, field_values): (&HttpMessageComponentId, &[String])) -> Result { + match &id.name { + HttpMessageComponentName::HttpField(_) => build_http_field_component(id, field_values), + HttpMessageComponentName::Derived(_) => build_derived_component(id, field_values), } -} - -fn ensure_component_id(id: &str) -> anyhow::Result<()> { - if !id.starts_with('"') || !(id.ends_with('"') || id[1..].contains("\";")) { - bail!("Invalid http message component id: {}", id); } - Ok(()) } impl std::fmt::Display for HttpMessageComponent {