Skip to content

Commit

Permalink
erase warn heapless::new()
Browse files Browse the repository at this point in the history
  • Loading branch information
indexds committed Oct 24, 2024
1 parent c80e3d1 commit 7ca6282
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/env/heapless.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use heapless::String;

#[derive(Debug)]
//Wrapper around heapless::String to allow for TryFrom impl
pub struct HeaplessString<const N: usize>(String<N>);

impl<const N: usize> HeaplessString<N> {

pub fn new() -> Self {
Self(String::<N>::new())
}

pub fn push_str(&mut self, s: &str) -> anyhow::Result<()> {

if s.len() > N {
return Err(anyhow::anyhow!("String too long."))
}

_ = self.0.push_str(s);

Ok(())
}
}

impl<const N: usize> TryInto<HeaplessString<N>> for &str {

type Error = anyhow::Error;

fn try_into(self) -> anyhow::Result<HeaplessString<N>> {

let mut heapless_string = HeaplessString::<N>::new();
heapless_string.push_str(&self)?;

Ok(heapless_string)
}
}

impl<const N: usize> TryInto<HeaplessString<N>> for heapless::String<N> {

type Error = anyhow::Error;

fn try_into(self) -> anyhow::Result<HeaplessString<N>> {

let mut heapless_string = HeaplessString::<N>::new();
heapless_string.push_str(&self)?;

Ok(heapless_string)

}
}

impl<const N: usize> TryInto<heapless::String<N>> for HeaplessString<N> {

type Error = anyhow::Error;

fn try_into(self) -> anyhow::Result<heapless::String<N>> {

Ok(self.0)
}

}
73 changes: 73 additions & 0 deletions src/env/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
mod heapless;
use heapless::HeaplessString;

const DEFAULT_STA_SSID: &str = "wifi";
const DEFAULT_STA_PASSWD: &str = "password";
const DEFAULT_WG_ADDR: &str = "0.0.0.0/24";
const DEFAULT_WG_PORT: &str = "51820";
const DEFAULT_WG_DNS: &str = "1.1.1.1";
const DEFAULT_WG_PSK_CLIENT: &str = "00000000000000000000000000000000";
const DEFAULT_WG_PSK_PUB_SERVER: &str = "00000000000000000000000000000000";

#[allow(dead_code)]
#[derive(Debug)]
pub struct Env{
//Wifi credentials
pub sta_ssid: HeaplessString<32>,
pub sta_passwd: HeaplessString<64>,

//Wireguard credentials
pub wg_addr: HeaplessString<16>,
pub wg_port: HeaplessString<16>,
pub wg_dns: HeaplessString<16>,
pub wg_psk_client: HeaplessString<32>,
pub wg_psk_pub_server: HeaplessString<32>,
}

impl Env{
fn env_var(env_var: &str) -> Result<&str, anyhow::Error>{
let var = include_str!(".env").lines()
.find(|line| line.starts_with(&env_var))
.unwrap_or("")
.split('=')
.nth(1)
.unwrap_or("");

Ok(var)
}

fn construct_field<const N: usize>(var: &str) -> anyhow::Result<HeaplessString<N>> {
let var_env = Env::env_var(var)?;
let mut var = HeaplessString::<N>::new();

_ = var.push_str(var_env);

Ok(var)
}

pub fn new() -> Result<Self, anyhow::Error>{
Ok(Self {
sta_ssid: Env::construct_field::<32>("STA_SSID")
.unwrap_or(DEFAULT_STA_SSID.try_into()?),

sta_passwd: Env::construct_field::<64>("STA_PASSWD")
.unwrap_or(DEFAULT_STA_PASSWD.try_into()?),

wg_addr: Env::construct_field::<16>("WG_ADDR")
.unwrap_or(DEFAULT_WG_ADDR.try_into()?),

wg_port: Env::construct_field::<16>("WG_PORT")
.unwrap_or(DEFAULT_WG_PORT.try_into()?),

wg_dns: Env::construct_field::<16>("WG_DNS")
.unwrap_or(DEFAULT_WG_DNS.try_into()?),

wg_psk_client: Env::construct_field::<32>("WG_PSK_CLIENT")
.unwrap_or(DEFAULT_WG_PSK_CLIENT.try_into()?),

wg_psk_pub_server: Env::construct_field::<32>("WG_PSK_PUB_SERVER")
.unwrap_or(DEFAULT_WG_PSK_PUB_SERVER.try_into()?),

})
}
}

0 comments on commit 7ca6282

Please sign in to comment.