Added Record.
All checks were successful
MoreThanText/morethantext/pipeline/head This commit looks good

This commit is contained in:
Jeff Baskin 2025-02-11 11:33:54 -05:00
parent 74c64890e2
commit ca40b8495c

View File

@ -9,7 +9,11 @@ use client::{Client, ClientMsg};
use message::{Message, MsgData};
use router::Router;
use session::{Session, SessionData, SessionMsg};
use std::sync::mpsc::{channel, Sender};
use std::{
collections::HashMap,
ops::Deref,
sync::mpsc::{channel, Sender},
};
struct Request;
@ -25,7 +29,7 @@ impl From<String> for Field {
impl From<&str> for Field {
fn from(value: &str) -> Self {
Field::Static(value.to_string())
Field::Static(value.into())
}
}
@ -54,31 +58,96 @@ mod fields {
}
}
struct Record;
struct Record {
data: HashMap<String, Field>,
}
impl Record {
fn new() -> Self {
Self {
data: HashMap::new(),
}
}
fn add<S, F>(&mut self, name: S, data: F)
where
S: Into<String>,
F: Into<Field>,
{
self.data.insert(name.into(), data.into());
}
}
impl Deref for Record {
type Target = HashMap<String, Field>;
fn deref(&self) -> &Self::Target {
&self.data
}
}
#[cfg(test)]
mod records {
use super::*;
#[test]
fn initialize() {
let rec = Record::new();
assert!(rec.is_empty());
}
#[test]
fn bad_get_return() {
let rec = Record::new();
match rec.get("empty") {
Some(_) => unreachable!("Should_have returned a None"),
None => {}
}
}
#[test]
fn add_data() {
let name = "name1x";
let data = "data1";
let mut rec = Record::new();
rec.add(name, data);
match rec.get(name) {
None => unreachable!("Should return data"),
Some(result) => match result {
Field::Static(txt) => assert_eq!(txt, data),
},
}
}
#[test]
fn add_data_strings() {
let name = "field".to_string();
let data = "info".to_string();
let mut rec = Record::new();
rec.add(name.clone(), data.clone());
match rec.get(&name) {
None => unreachable!("Should return data"),
Some(result) => match result {
Field::Static(txt) => assert_eq!(txt, &data),
},
}
}
}
struct Response {
headers: Vec<String>,
records: Vec<Record>,
data: HashMap<String, Vec<Field>>,
}
impl Response {
fn new() -> Self {
Self {
headers: Vec::new(),
records: Vec::new(),
data: HashMap::new(),
}
}
fn count(&self) -> usize {
0
}
fn add_header<S>(&mut self, name: S)
where
S: Into<String>,
{
self.headers.push(name.into());
}
}
#[cfg(test)]
@ -88,19 +157,9 @@ mod responses {
#[test]
fn create_response() {
let res = Response::new();
assert!(res.headers.is_empty());
assert!(res.records.is_empty());
assert!(res.data.is_empty());
assert_eq!(res.count(), 0);
}
#[test]
fn addA_header() {
let mut res = Response::new();
res.add_header("one");
assert_eq!(res.headers, ["one"]);
res.add_header("two".to_string());
assert_eq!(res.headers, ["one", "two"]);
}
}
/// Support functions for Messages.