garage44.eu/jeroen/pinnacle

logclone: git clone https://garage44.eu/jeroen/pinnacle.git

.. /ports/hostname-0.4.2/src/pinnacle.rs

raw

//! Pinnacle: the device's name is what Settings keeps in
//! `system:/settings/name`; "pinnacle" until one is chosen.

use std::ffi::OsString;
#[cfg(feature = "set")]
use std::ffi::OsStr;
use std::io;

const NAME: &str = "system:/settings/name";

pub fn get() -> io::Result<OsString> {
    match std::fs::read_to_string(NAME) {
        Ok(name) if !name.trim().is_empty() => Ok(OsString::from(name.trim())),
        _ => Ok(OsString::from("pinnacle")),
    }
}

#[cfg(feature = "set")]
pub fn set(hostname: &OsStr) -> io::Result<()> {
    let name = hostname
        .to_str()
        .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "a name must be UTF-8"))?;
    std::fs::write(NAME, name)
}