Facade Design pattern in Rust

Facade pattern hides the complexities of the system and provides an interface to the client using which the client can access the system. This type of design pattern comes under structural pattern as this pattern adds an interface to the existing system to hide its complexities.

This pattern involves a single class which provides simplified methods required by client and delegates calls to methods of existing system classes

use std::collections::HashMap;
use std::io::prelude::*;
use std::fs::File;

extern crate csv;
extern crate rustc_serialize;

struct Record {
    mail_addr: String,
    user_name: String,

struct Database {}

impl Database {
    fn get_properties(db_name: String) -> HashMap<String, String> {
        let mut prop = HashMap::new();
        let file_name = format!("{}.txt", db_name);
        let mut rdr = match csv::Reader::from_file(file_name) {
            Ok(r) => r.has_headers(false),
            Err(e) => panic!(e),

        for record in rdr.decode() {
            let record: Record = record.unwrap();
            prop.insert(record.mail_addr, record.user_name);

struct HtmlWriter {
    writer: File,

impl HtmlWriter {
    fn new(writer: File) -> HtmlWriter {
        HtmlWriter {
            writer: writer,

    fn title(&mut self, title: String) {
        write!(self.writer, "<html>");
        write!(self.writer, "<head>");
        write!(self.writer, "<title>{}</title>", title);
        write!(self.writer, "</head>");
        write!(self.writer, "<body> ");
        write!(self.writer, "<h1>{}</h1> ", title);

    fn paragraph(&mut self, msg: String) {
        write!(self.writer, "<p>{}</p> ", msg);

    fn link(&mut self, href: String, caption: String) {
        self.paragraph(format!("<a href="{}">{}</a>", href, caption));

    fn mail_to(&mut self, mail_addr: String, user_name: String) {
        self.link(format!("mailto:{}", mail_addr), user_name);

    fn close(&mut self) {
        write!(self.writer, "</body>");
        write!(self.writer, "<html> ");

struct PageMaker {}

impl PageMaker {
    fn make_welcome_page(mail_addr: String, file_name: String) {
        let mail_prop = Database::get_properties("maildata".to_string());
        let user_name = match mail_prop.get(&mail_addr) {
            Some(n) => n,
            None => panic!("{} is not found", mail_addr),

        let file = File::create(file_name.clone()).expect("Unable to create file");
        let mut writer = HtmlWriter::new(file);
        writer.title(format!("Welcome to {}'s page!", user_name));
        writer. paragraph ( format! ( "Welcome to the page of {}" , user_name));
        writer. paragraph ( "Email is gone." . to_string ());
        writer.mail_to(mail_addr.clone(), user_name.to_string());
        println!("{} is created for {} ({})", file_name, mail_addr, user_name);

fn  main () {
    PageMaker::make_welcome_page("[email protected]".to_string(), "welcome.html".to_string());
About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions