Added request information.
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 2s
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 2s
This commit is contained in:
parent
5d8624656a
commit
445cbb57f1
@ -1,6 +1,7 @@
|
||||
mod action_type;
|
||||
mod message;
|
||||
mod query;
|
||||
mod request_data;
|
||||
mod user;
|
||||
|
||||
pub use action_type::Action;
|
||||
@ -8,3 +9,4 @@ pub use crate::document::{definition::DocDef, field::FieldType};
|
||||
pub use message::MsgAction;
|
||||
pub use query::Query;
|
||||
pub use user::UserAction;
|
||||
use request_data::RequestData;
|
||||
|
||||
88
src/action/request_data.rs
Normal file
88
src/action/request_data.rs
Normal file
@ -0,0 +1,88 @@
|
||||
use crate::{
|
||||
document::field::Field,
|
||||
message::wrapper::CalcValue,
|
||||
name::NameType,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct RequestData {
|
||||
data: HashMap<NameType, CalcValue>,
|
||||
}
|
||||
|
||||
impl RequestData {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
data: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_field<NT, CV>(&mut self, name: NT, field: CV)
|
||||
where
|
||||
CV: Into<CalcValue>,
|
||||
NT: Into<NameType>,
|
||||
{
|
||||
self.data.insert(name.into(), field.into());
|
||||
}
|
||||
|
||||
fn get_field<NT>(&self, name: NT) -> &CalcValue
|
||||
where
|
||||
NT: Into<NameType>,
|
||||
{
|
||||
match self.data.get(&name.into()) {
|
||||
Some(data) => data,
|
||||
None => &CalcValue::None,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_all(&self) -> Vec<(NameType, Field)> {
|
||||
let mut output = Vec::new();
|
||||
for (key, value) in self.data.iter() {
|
||||
output.push((key.clone(), value.get(&Field::None)));
|
||||
}
|
||||
output
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> impl Iterator<Item = (&NameType, &CalcValue)> {
|
||||
self.data.iter()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod request_datum {
|
||||
use super::*;
|
||||
use crate::name::Name;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[test]
|
||||
fn can_add_static_string() {
|
||||
let mut add = RequestData::new();
|
||||
let name = Name::english(Uuid::new_v4().to_string().as_str());
|
||||
let data = Uuid::new_v4().to_string();
|
||||
add.add_field(name.clone(), data.clone());
|
||||
let result = add.get_field(&name);
|
||||
match result {
|
||||
CalcValue::Value(holder) => match holder {
|
||||
Field::StaticString(result) => assert_eq!(result, &data),
|
||||
_ => unreachable!("got {:?}: should have received static string", holder),
|
||||
},
|
||||
_ => unreachable!("got {:?}, should have been value", result),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_add_uuid() {
|
||||
let mut add = RequestData::new();
|
||||
let name = Name::english(Uuid::new_v4().to_string().as_str());
|
||||
let data = Uuid::new_v4();
|
||||
add.add_field(name.clone(), data.clone());
|
||||
let result = add.get_field(&name);
|
||||
match result {
|
||||
CalcValue::Value(holder) => match holder {
|
||||
Field::Uuid(result) => assert_eq!(result, &data),
|
||||
_ => unreachable!("got {:?}: should have received static string", holder),
|
||||
},
|
||||
_ => unreachable!("got {:?}, should have been value", result),
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user