1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
use crate::error::LSError;
use serde::Serialize;
use std::io::stdout;
use std::io::Write;

/// Returns the extension which includes `.` from the url string
pub fn extension_from_url_str(url_str: &str) -> Option<String> {
    Some(String::from(".") + url_str.split('.').last().unwrap())
}

pub fn send_stdout<T>(message: &T) -> Result<(), LSError>
where
    T: ?Sized + Serialize,
{
    let msg = serde_json::to_string(message)?;
    let mut stdout = stdout().lock();
    write!(stdout, "Content-Length: {}\r\n\r\n{}", msg.len(), msg)?;
    stdout.flush()?;
    Ok(())
}