document up the config logic

Signed-off-by: Christine Dodrill <me@christine.website>
This commit is contained in:
Cadey Ratio 2021-09-06 20:40:52 -04:00
parent 9a2394ee01
commit 1b3d12435a
2 changed files with 9 additions and 5 deletions

View File

@ -1,11 +1,6 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{env, ffi::OsString, fs, io, path::PathBuf}; use std::{env, ffi::OsString, fs, io, path::PathBuf};
#[cfg(feature = "log-facade")]
mod log;
#[cfg(feature = "log-facade")]
pub use self::log::*;
pub(crate) fn cache_dir() -> Option<OsString> { pub(crate) fn cache_dir() -> Option<OsString> {
let dir = env::var_os("STATE_DIRECTORY").or_else(|| { let dir = env::var_os("STATE_DIRECTORY").or_else(|| {
env::var_os("HOME").and_then(|dir| { env::var_os("HOME").and_then(|dir| {
@ -24,13 +19,18 @@ pub(crate) fn state_file(name: &str) -> Option<PathBuf> {
Some(dir) Some(dir)
} }
/// The logtail configuration file. This contains cached copies of public and private
/// IDs, however in practice the most read value is the private ID.
#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)] #[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
pub struct Config { pub struct Config {
// TODO(Xe): make this use a custom serde format, see https://serde.rs/custom-date-format.html
private_id: String, private_id: String,
public_id: String, public_id: String,
} }
impl Config { impl Config {
/// Constructs a brand new configuration, including a rng'd private ID and its public
/// counterpart.
pub fn new() -> Self { pub fn new() -> Self {
let private_id = logtail::PrivateID::new(); let private_id = logtail::PrivateID::new();
@ -40,10 +40,13 @@ impl Config {
} }
} }
/// Returns the logtail private ID for this Config.
pub fn private_id(self) -> logtail::PrivateID { pub fn private_id(self) -> logtail::PrivateID {
logtail::PrivateID::from_hex(self.private_id).unwrap() logtail::PrivateID::from_hex(self.private_id).unwrap()
} }
/// Loads the relevant configuration file off disk, and creates a new one if it
/// doesn't already exist. This function is idempotent.
pub fn load<S>(collection: S) -> io::Result<Self> pub fn load<S>(collection: S) -> io::Result<Self>
where where
S: Into<String>, S: Into<String>,

View File

@ -36,6 +36,7 @@ impl PrivateID {
hex::encode(self.0) hex::encode(self.0)
} }
/// Converts this private ID into its public equivalent.
pub fn as_public(&self) -> PublicID { pub fn as_public(&self) -> PublicID {
let mut hasher = Sha256::new(); let mut hasher = Sha256::new();
hasher.update(&self.0); hasher.update(&self.0);