Struct GopherMenu

Source
pub struct GopherMenu<W>
where W: Write,
{ /* private fields */ }

Implementations§

Source§

impl<'a, W> GopherMenu<&'a W>
where &'a W: Write,

Source

pub fn with_write(target: &'a W) -> Self

Examples found in repository?
examples/simple_server.rs (line 16)
9fn handle_client(stream: TcpStream) -> io::Result<()> {
10    let mut line = String::new();
11    BufReader::new(stream.try_clone()?).read_line(&mut line)?;
12    let line = line.trim();
13
14    println!("New request: {}", line);
15
16    let mut menu = GopherMenu::with_write(&stream);
17
18    let menu_link = |text: &str, selector: &str|
19        menu.write_entry(ItemType::Directory, text, selector, HOST, PORT);
20
21    match line {
22        "/" | "" => {
23            menu.info("Hi!")?;
24            menu.info("Welcome to my Gopher server!")?;
25            menu_link("Tomatoes", "/tomato")?;
26            menu.info("Opinion piece about tomatoes")?;
27            menu_link("Potatoes", "/potato")?;
28            menu.info("Opinion piece about potatoes")?;
29            menu_link("Go to unknown link", "/lel")?;
30        }
31        "/tomato" => {
32            menu.info("Tomatoes are not good")?;
33            menu_link("Home page", "/")?;
34        }
35        "/potato" => {
36            menu.info("Potatoes are the best")?;
37            menu_link("Home page", "/")?;
38        }
39        x => {
40            menu.info(&format!("Unknown link: {}", x))?;
41            menu_link("Home page", "/")?;
42        }
43    };
44    menu.end()?;
45    Ok(())
46}
Source

pub fn info(&self, text: &str) -> Result<()>

Examples found in repository?
examples/simple_server.rs (line 23)
9fn handle_client(stream: TcpStream) -> io::Result<()> {
10    let mut line = String::new();
11    BufReader::new(stream.try_clone()?).read_line(&mut line)?;
12    let line = line.trim();
13
14    println!("New request: {}", line);
15
16    let mut menu = GopherMenu::with_write(&stream);
17
18    let menu_link = |text: &str, selector: &str|
19        menu.write_entry(ItemType::Directory, text, selector, HOST, PORT);
20
21    match line {
22        "/" | "" => {
23            menu.info("Hi!")?;
24            menu.info("Welcome to my Gopher server!")?;
25            menu_link("Tomatoes", "/tomato")?;
26            menu.info("Opinion piece about tomatoes")?;
27            menu_link("Potatoes", "/potato")?;
28            menu.info("Opinion piece about potatoes")?;
29            menu_link("Go to unknown link", "/lel")?;
30        }
31        "/tomato" => {
32            menu.info("Tomatoes are not good")?;
33            menu_link("Home page", "/")?;
34        }
35        "/potato" => {
36            menu.info("Potatoes are the best")?;
37            menu_link("Home page", "/")?;
38        }
39        x => {
40            menu.info(&format!("Unknown link: {}", x))?;
41            menu_link("Home page", "/")?;
42        }
43    };
44    menu.end()?;
45    Ok(())
46}
Source

pub fn write_entry( &self, item_type: ItemType, text: &str, selector: &str, host: &str, port: u16, ) -> Result<()>

Examples found in repository?
examples/simple_server.rs (line 19)
9fn handle_client(stream: TcpStream) -> io::Result<()> {
10    let mut line = String::new();
11    BufReader::new(stream.try_clone()?).read_line(&mut line)?;
12    let line = line.trim();
13
14    println!("New request: {}", line);
15
16    let mut menu = GopherMenu::with_write(&stream);
17
18    let menu_link = |text: &str, selector: &str|
19        menu.write_entry(ItemType::Directory, text, selector, HOST, PORT);
20
21    match line {
22        "/" | "" => {
23            menu.info("Hi!")?;
24            menu.info("Welcome to my Gopher server!")?;
25            menu_link("Tomatoes", "/tomato")?;
26            menu.info("Opinion piece about tomatoes")?;
27            menu_link("Potatoes", "/potato")?;
28            menu.info("Opinion piece about potatoes")?;
29            menu_link("Go to unknown link", "/lel")?;
30        }
31        "/tomato" => {
32            menu.info("Tomatoes are not good")?;
33            menu_link("Home page", "/")?;
34        }
35        "/potato" => {
36            menu.info("Potatoes are the best")?;
37            menu_link("Home page", "/")?;
38        }
39        x => {
40            menu.info(&format!("Unknown link: {}", x))?;
41            menu_link("Home page", "/")?;
42        }
43    };
44    menu.end()?;
45    Ok(())
46}
Source

pub fn end(&mut self) -> Result<()>

Examples found in repository?
examples/simple_server.rs (line 44)
9fn handle_client(stream: TcpStream) -> io::Result<()> {
10    let mut line = String::new();
11    BufReader::new(stream.try_clone()?).read_line(&mut line)?;
12    let line = line.trim();
13
14    println!("New request: {}", line);
15
16    let mut menu = GopherMenu::with_write(&stream);
17
18    let menu_link = |text: &str, selector: &str|
19        menu.write_entry(ItemType::Directory, text, selector, HOST, PORT);
20
21    match line {
22        "/" | "" => {
23            menu.info("Hi!")?;
24            menu.info("Welcome to my Gopher server!")?;
25            menu_link("Tomatoes", "/tomato")?;
26            menu.info("Opinion piece about tomatoes")?;
27            menu_link("Potatoes", "/potato")?;
28            menu.info("Opinion piece about potatoes")?;
29            menu_link("Go to unknown link", "/lel")?;
30        }
31        "/tomato" => {
32            menu.info("Tomatoes are not good")?;
33            menu_link("Home page", "/")?;
34        }
35        "/potato" => {
36            menu.info("Potatoes are the best")?;
37            menu_link("Home page", "/")?;
38        }
39        x => {
40            menu.info(&format!("Unknown link: {}", x))?;
41            menu_link("Home page", "/")?;
42        }
43    };
44    menu.end()?;
45    Ok(())
46}

Auto Trait Implementations§

§

impl<W> Freeze for GopherMenu<W>
where W: Freeze,

§

impl<W> RefUnwindSafe for GopherMenu<W>
where W: RefUnwindSafe,

§

impl<W> Send for GopherMenu<W>
where W: Send,

§

impl<W> Sync for GopherMenu<W>
where W: Sync,

§

impl<W> Unpin for GopherMenu<W>
where W: Unpin,

§

impl<W> UnwindSafe for GopherMenu<W>
where W: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.