From 4056a1658ccab3d832058b5167e59c82c33456db Mon Sep 17 00:00:00 2001 From: user Date: Thu, 15 Aug 2019 11:09:30 +0200 Subject: [PATCH] Minimal mytilde implementation --- Cargo.toml | 20 ++++++++++++++++++++ src/lib.rs | 38 +++++++++++++++++++++++++++++++++++++- src/main.rs | 5 +++++ 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..2b9e700 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "mytilde" +version = "0.1.0" +authors = ["southerntofu "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +serde_json = "1.0" +serde = { version = "1.0", features = ["serde_derive"] } + +[lib] +name = "mytilde" +path = "src/lib.rs" + +[[bin]] +name = "mytilde" +path = "src/main.rs" + diff --git a/src/lib.rs b/src/lib.rs index 98f741f..c963b32 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,29 +1,65 @@ +use serde_json::{Value, Map}; +use serde::{Deserialize, Serialize}; +use std::convert::From; + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] enum Aliases { One(String), More(Vec), } +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] enum Domain { - Name(string), + Name(String), Config(DomainConfig), } +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] struct DomainConfig { hostname: String, alias: Option, web: Option, } +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] enum Web { Template(String), Config(WebConfig) } +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] struct WebConfig { template: Option, vars: Map, } +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] struct MyTilde { domains: Vec, } + +#[derive(Debug)] +enum Error { + File(std::io::Error), + Json(serde_json::Error), +} + +impl From for Error { + fn from(e: std::io::Error) -> Error { + Error::File(e) + } +} + +impl From for Error { + fn from(e: serde_json::Error) -> Error { + Error::Json(e) + } +} + +impl MyTilde { + fn from_file>(path: P) -> Result { + let content = std::fs::read_to_string(path)?; + let json: Value = serde_json::from_str(&content)?; + Ok(serde_json::from_value(json)?) + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..43d2f7c --- /dev/null +++ b/src/main.rs @@ -0,0 +1,5 @@ +use mytilde; + +fn main() { + println!("Echo world!"); +}