mirror of
https://github.com/Orange-OpenSource/hurl.git
synced 2024-12-24 03:21:40 +03:00
Implement Display Trait in core
This commit is contained in:
parent
9bf8d2e64f
commit
92db6e505d
@ -16,12 +16,10 @@
|
||||
*
|
||||
*/
|
||||
use super::json;
|
||||
use core::fmt;
|
||||
|
||||
///
|
||||
/// Hurl AST
|
||||
///
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct HurlFile {
|
||||
pub entries: Vec<Entry>,
|
||||
@ -130,22 +128,6 @@ pub enum Method {
|
||||
Patch,
|
||||
}
|
||||
|
||||
impl Method {
|
||||
pub fn as_str<'a>(&self) -> &'a str {
|
||||
match self {
|
||||
Method::Get => "GET",
|
||||
Method::Head => "HEAD",
|
||||
Method::Post => "POST",
|
||||
Method::Put => "PUT",
|
||||
Method::Delete => "DELETE",
|
||||
Method::Connect => "CONNECT",
|
||||
Method::Options => "OPTIONS",
|
||||
Method::Trace => "TRACE",
|
||||
Method::Patch => "PATCH",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Version {
|
||||
pub value: VersionValue,
|
||||
@ -183,16 +165,7 @@ pub enum StatusValue {
|
||||
Specific(u64),
|
||||
}
|
||||
|
||||
impl fmt::Display for StatusValue {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
StatusValue::Any => write!(f, "*"),
|
||||
StatusValue::Specific(v) => write!(f, "{}", v.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type Header = KeyValue;
|
||||
pub type Header = KeyValue;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Body {
|
||||
@ -509,16 +482,6 @@ pub struct Float {
|
||||
pub decimal_digits: usize, // number of digits
|
||||
}
|
||||
|
||||
impl fmt::Display for Float {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let decimal_str: String = format!("{:018}", self.decimal)
|
||||
.chars()
|
||||
.take(self.decimal_digits)
|
||||
.collect();
|
||||
write!(f, "{}.{}", self.int, decimal_str)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct LineTerminator {
|
||||
pub space0: Whitespace,
|
||||
@ -583,13 +546,6 @@ impl SourceInfo {
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// template
|
||||
//
|
||||
// might include expression
|
||||
// which can only simple ASCII (even in json value)
|
||||
// optional delimiter/encoding
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Expr {
|
||||
pub space0: Whitespace,
|
||||
@ -602,48 +558,3 @@ pub struct Variable {
|
||||
pub name: String,
|
||||
pub source_info: SourceInfo,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_float() {
|
||||
assert_eq!(
|
||||
Float {
|
||||
int: 1,
|
||||
decimal: 0,
|
||||
decimal_digits: 1,
|
||||
}
|
||||
.to_string(),
|
||||
"1.0"
|
||||
);
|
||||
assert_eq!(
|
||||
Float {
|
||||
int: 1,
|
||||
decimal: 10_000_000_000_000_000,
|
||||
decimal_digits: 2,
|
||||
}
|
||||
.to_string(),
|
||||
"1.01"
|
||||
);
|
||||
assert_eq!(
|
||||
Float {
|
||||
int: 1,
|
||||
decimal: 10_000_000_000_000_000,
|
||||
decimal_digits: 3,
|
||||
}
|
||||
.to_string(),
|
||||
"1.010"
|
||||
);
|
||||
assert_eq!(
|
||||
Float {
|
||||
int: -1,
|
||||
decimal: 333_333_333_333_333_333,
|
||||
decimal_digits: 3,
|
||||
}
|
||||
.to_string(),
|
||||
"-1.333"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
191
packages/hurl_core/src/ast/display.rs
Normal file
191
packages/hurl_core/src/ast/display.rs
Normal file
@ -0,0 +1,191 @@
|
||||
/*
|
||||
* hurl (https://hurl.dev)
|
||||
* Copyright (C) 2020 Orange
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
use super::core::*;
|
||||
use core::fmt;
|
||||
|
||||
impl fmt::Display for Method {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let s = match self {
|
||||
Method::Get => "GET",
|
||||
Method::Head => "HEAD",
|
||||
Method::Post => "POST",
|
||||
Method::Put => "PUT",
|
||||
Method::Delete => "DELETE",
|
||||
Method::Connect => "CONNECT",
|
||||
Method::Options => "OPTIONS",
|
||||
Method::Trace => "TRACE",
|
||||
Method::Patch => "PATCH",
|
||||
};
|
||||
write!(f, "{}", s)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Version {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.value.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for VersionValue {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let s = match self {
|
||||
VersionValue::Version1 => "1.0",
|
||||
VersionValue::Version11 => "1.1",
|
||||
VersionValue::Version2 => "2",
|
||||
VersionValue::VersionAny => "*",
|
||||
};
|
||||
write!(f, "{}", s)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Status {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.value.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for StatusValue {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
StatusValue::Any => write!(f, "*"),
|
||||
StatusValue::Specific(v) => write!(f, "{}", v.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Template {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let mut buffer = String::from("");
|
||||
for element in self.elements.clone() {
|
||||
buffer.push_str(element.to_string().as_str());
|
||||
}
|
||||
write!(f, "{}", buffer)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for TemplateElement {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let s = match self {
|
||||
TemplateElement::String { value, .. } => value.clone(),
|
||||
TemplateElement::Expression(value) => format!("{{{{{}}}}}", value.to_string()),
|
||||
};
|
||||
write!(f, "{}", s)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Float {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let decimal_str: String = format!("{:018}", self.decimal)
|
||||
.chars()
|
||||
.take(self.decimal_digits)
|
||||
.collect();
|
||||
write!(f, "{}.{}", self.int, decimal_str)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Expr {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.variable.name)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn whitespace() -> Whitespace {
|
||||
Whitespace {
|
||||
value: "".to_string(),
|
||||
source_info: SourceInfo::init(0, 0, 0, 0),
|
||||
}
|
||||
}
|
||||
|
||||
fn variable_expr() -> Expr {
|
||||
Expr {
|
||||
space0: whitespace(),
|
||||
variable: Variable {
|
||||
name: "name".to_string(),
|
||||
source_info: SourceInfo::init(0, 0, 0, 0),
|
||||
},
|
||||
space1: whitespace(),
|
||||
}
|
||||
}
|
||||
|
||||
fn hello_template() -> Template {
|
||||
Template {
|
||||
quotes: false,
|
||||
elements: vec![
|
||||
TemplateElement::String {
|
||||
value: "Hello ".to_string(),
|
||||
encoded: "Hello ".to_string(),
|
||||
},
|
||||
TemplateElement::Expression(variable_expr()),
|
||||
TemplateElement::String {
|
||||
value: "!".to_string(),
|
||||
encoded: "!".to_string(),
|
||||
},
|
||||
],
|
||||
source_info: SourceInfo::init(0, 0, 0, 0),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_float() {
|
||||
assert_eq!(
|
||||
Float {
|
||||
int: 1,
|
||||
decimal: 0,
|
||||
decimal_digits: 1,
|
||||
}
|
||||
.to_string(),
|
||||
"1.0"
|
||||
);
|
||||
assert_eq!(
|
||||
Float {
|
||||
int: 1,
|
||||
decimal: 10_000_000_000_000_000,
|
||||
decimal_digits: 2,
|
||||
}
|
||||
.to_string(),
|
||||
"1.01"
|
||||
);
|
||||
assert_eq!(
|
||||
Float {
|
||||
int: 1,
|
||||
decimal: 10_000_000_000_000_000,
|
||||
decimal_digits: 3,
|
||||
}
|
||||
.to_string(),
|
||||
"1.010"
|
||||
);
|
||||
assert_eq!(
|
||||
Float {
|
||||
int: -1,
|
||||
decimal: 333_333_333_333_333_333,
|
||||
decimal_digits: 3,
|
||||
}
|
||||
.to_string(),
|
||||
"-1.333"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_template() {
|
||||
assert_eq!(hello_template().to_string(), "Hello {{name}}!");
|
||||
}
|
||||
}
|
@ -22,4 +22,5 @@ pub use self::json::ObjectElement as JsonObjectElement;
|
||||
pub use self::json::Value as JsonValue;
|
||||
|
||||
mod core;
|
||||
mod display;
|
||||
mod json;
|
||||
|
@ -120,7 +120,7 @@ impl Htmlable for Response {
|
||||
|
||||
impl Htmlable for Method {
|
||||
fn to_html(&self) -> String {
|
||||
return format!("<span class=\"method\">{}</span>", self.as_str());
|
||||
return format!("<span class=\"method\">{}</span>", self.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -92,7 +92,7 @@ impl Tokenizable for Request {
|
||||
.collect(),
|
||||
);
|
||||
add_tokens(&mut tokens, self.space0.tokenize());
|
||||
tokens.push(Token::Method(self.method.as_str().to_string()));
|
||||
tokens.push(Token::Method(self.method.to_string()));
|
||||
add_tokens(&mut tokens, self.space1.tokenize());
|
||||
add_tokens(&mut tokens, self.url.tokenize());
|
||||
add_tokens(&mut tokens, self.line_terminator0.tokenize());
|
||||
|
Loading…
Reference in New Issue
Block a user