1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
use std::{
    fs::{File, OpenOptions},
    io::Write,
};

pub struct MailslotClient {
    file: File,
    has_domain: bool,
}

use crate::{Error, MailslotName};

impl MailslotClient {
    pub fn new(name: &MailslotName) -> Result<Self, Error> {
        let has_domain = name.domain == ".";
        let file = OpenOptions::new().write(true).open(&name.to_string())?;
        Ok(Self { file, has_domain })
    }
    pub fn send_message(&mut self, msg: &[u8]) -> Result<(), Error> {
        if self.has_domain && msg.len() > 424 {
            return Err(Error::MessageTooLarge(msg.len()));
        }
        self.file.write_all(msg)?;
        Ok(())
    }
}