Feature/add node description api (#605)
* Starting to build out node descriptions * Renamed the mod to node_description instead of descriptor * Returning results loaded from file * Taking user input for node describe command * Readline without new dependencies * Adding input format hint * Making sure the node can still start even when the descriptor file isn't there yet * Adding some additional runtime checks * Removing -dev from minimum node version * Removing initial :: on serde * Fixing comment on the /description endpoint * Swapped json for toml * Being a bit more specific with my startup message. * Cleaning up path building * Nicer runtime checks * Put path building inside the file methods
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use std::path::PathBuf;
|
||||
use std::{fs, io};
|
||||
|
||||
pub(crate) const DESCRIPTION_FILE: &str = "description.toml";
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
|
||||
pub struct NodeDescription {
|
||||
pub(crate) name: String,
|
||||
pub(crate) description: String,
|
||||
pub(crate) link: String,
|
||||
}
|
||||
|
||||
impl Default for NodeDescription {
|
||||
fn default() -> Self {
|
||||
NodeDescription {
|
||||
name: "This node has not yet set a name".to_string(),
|
||||
description: "This node has not yet set a description".to_string(),
|
||||
link: "https://nymtech.net".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl NodeDescription {
|
||||
pub(crate) fn load_from_file(config_path: PathBuf) -> io::Result<NodeDescription> {
|
||||
let description_file_path: PathBuf = [config_path.to_str().unwrap(), DESCRIPTION_FILE]
|
||||
.iter()
|
||||
.collect();
|
||||
let toml = fs::read_to_string(description_file_path)?;
|
||||
toml::from_str(&toml).map_err(|toml_err| io::Error::new(io::ErrorKind::Other, toml_err))
|
||||
}
|
||||
|
||||
pub(crate) fn save_to_file(
|
||||
description: &NodeDescription,
|
||||
config_path: PathBuf,
|
||||
) -> io::Result<()> {
|
||||
let description_file_path: PathBuf = [config_path.to_str().unwrap(), DESCRIPTION_FILE]
|
||||
.iter()
|
||||
.collect();
|
||||
let description_toml =
|
||||
toml::to_string(description).expect("could not encode description to toml");
|
||||
fs::write(description_file_path, description_toml)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user