pub use crate::front_of_house::hosting;
use std::{io, option, os::windows, future::Pending};
mod back_of_house;
mod front_of_house;
pub mod mod_intergration_test;
use back_of_house::Appetizer;
pub mod tools;
pub fn eat_at_restaurant(){
println!("eat at restaurant!");
hosting::add_to_waitlist();
let mut meal = back_of_house::Breakfast::summer("Rye");
meal.toast = String::from("wheat");
println!("meal is :{}",meal.to_string());
println!("-----------------------");
println!("i'd like {} toast please",meal.toast);
let soup:Appetizer = Appetizer::Soup;
let salad:Appetizer = Appetizer::Salad;
}
fn deliver_order(){
println!("deliver order!");
}
pub struct AverageCollection{
_list:Vec<i32>,
_average:f64,
}
impl AverageCollection{
pub fn add(&mut self,value:i32)->bool{
self._list.push(value);
self.update_average();
return true;
}
pub fn get_average(&self)->f64{
return self._average;
}
pub fn pop_remove(&mut self)->Option<i32>{
let result = self._list.pop();
match result {
Some(value) => {
self.update_average();
return Some(value);
}
_ => return None,
}
}
pub fn remove_at(&mut self,index:usize)->bool{
if index >= self._list.len() {
return false;
}
self._list.remove(index);
return true;
}
pub fn new()->AverageCollection{
return AverageCollection {
_list:vec![],
_average:0.0,
}
}
fn update_average(&mut self){
let total:i32 = self._list.iter().sum();
self._average = total as f64 / self._list.len() as f64;
}
}
pub trait Draw{
fn draw(& self);
}
pub struct Screen{
pub components : Vec<Box<dyn Draw>>,
}
impl Screen{
pub fn run(&self){
for component in self.components.iter(){
component.draw();
}
}
pub fn create_button(width:u32,height:u32,label:String)->Button{
return Button{
_width:width,
_height:height,
_label:label,
}
}
pub fn create_select_box(width:u32,height:u32,options:Vec<String>)->SelectBox{
return SelectBox {
_width: width,
_height: height,
_options: options
}
}
}
pub struct Button{
pub _width:u32,
pub _height:u32,
pub _label:String,
}
impl Draw for Button{
fn draw(& self) {
println!("button draw...");
}
}
pub struct SelectBox {
_width: u32,
_height: u32,
_options: Vec<String>,
}
impl Draw for SelectBox {
fn draw(&self) {
println!("selec box drawing...");
}
}
trait State {
fn request_review(self:Box<Self>) -> Box<dyn State>;
fn approve(self:Box<Self>) -> Box<dyn State>;
fn content<'x>(&self, post:&'x Post) -> &'x str {
return "";
}
}
struct Draft{
}
impl State for Draft{
fn request_review(self:Box<Self>) -> Box<dyn State> {
return Box::new(PendingReview{});
}
fn approve(self:Box<Self>) -> Box<dyn State> {
return self;
}
}
struct PendingReview{
}
impl State for PendingReview{
fn request_review(self:Box<Self>) -> Box<dyn State> {
return self;
}
fn approve(self:Box<Self>) -> Box<dyn State> {
return Box::new(Published{});
}
}
struct Published{
}
impl State for Published{
fn request_review(self:Box<Self>) -> Box<dyn State> {
return self;
}
fn approve(self:Box<Self>) -> Box<dyn State> {
return self;
}
fn content<'x>(&self, post:&'x Post) -> &'x str {
return &post._content;
}
}
pub struct DraftPost{
_content:String
}
impl DraftPost{
pub fn add_text(&mut self,text:&str){
self._content.push_str(text);
}
pub fn request_review(self)->PendingReviewPost{
return PendingReviewPost{
_content:self._content,
}
}
}
pub struct PendingReviewPost{
_content:String
}
impl PendingReviewPost {
pub fn approve(self)->NewPost{
return NewPost{
_content:self._content,
}
}
}
pub struct NewPost{
_content:String,
}
impl NewPost {
pub fn new()->DraftPost{
return DraftPost{
_content : String::new(),
};
}
pub fn add_text(&mut self,text:&str){
self._content.push_str(text);
}
pub fn content(&self) -> &str{
return &self._content;
}
}
pub struct Post{
_state:Option<Box<dyn State>>,
_content:String,
}
impl Post {
pub fn new()->Post{
return Post{
_state:Some(Box::new(Draft{})),
_content:String::new(),
};
}
pub fn add_text(&mut self,text:&str){
self._content.push_str(text);
}
pub fn content(&self) -> &str{
return "";
}
pub fn request_review(&mut self){
if let Some(old_state) = self._state.take(){
self._state = Some(old_state.request_review());
}
}
pub fn approve(&mut self){
if let Some(old_state) = self._state.take(){
self._state = Some(old_state.approve());
}
}
}