From 4e652e46a173c3d9c9046ad7807a34a46741728d Mon Sep 17 00:00:00 2001 From: n0m1s Date: Sun, 5 Jan 2020 22:18:17 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20load=20gitmojis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 45 +++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/gitmoji.rs | 36 +++++++++++++++++++++++++++++++++++- src/main.rs | 6 ++++++ 4 files changed, 88 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 558b24d..d115e71 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -191,6 +191,8 @@ dependencies = [ "derive_more 0.99.2 (registry+https://github.com/rust-lang/crates.io-index)", "dirs 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "git2 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.44 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -211,6 +213,11 @@ dependencies = [ "unicode-normalization 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "itoa" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "jobserver" version = "0.1.17" @@ -385,6 +392,11 @@ name = "rustc-demangle" version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "ryu" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "schannel" version = "0.1.16" @@ -399,6 +411,34 @@ name = "scoped_threadpool" version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "serde" +version = "1.0.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_derive" +version = "1.0.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_json" +version = "1.0.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", + "ryu 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "socket2" version = "0.3.11" @@ -526,6 +566,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum git2 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "77519ef7c5beee314d0804d4534f01e0f9e8d9acdee2b7a48627e590b27e0ec4" "checksum hermit-abi 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f629dc602392d3ec14bfc8a09b5e644d7ffd725102b48b81e59f90f2633621d7" "checksum idna 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "02e2673c30ee86b5b96a9cb52ad15718aa1f966f5ab9ad54a8b95d5ca33120a9" +"checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f" "checksum jobserver 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)" = "f2b1d42ef453b30b7387e113da1c83ab1605d90c5b4e0eb8e96d016ed3b8c160" "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" "checksum libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)" = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558" @@ -548,8 +589,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1" "checksum redox_users 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "214a97e49be64fd2c86f568dd0cb2c757d2cc53de95b273b6ad0a1c908482f26" "checksum rustc-demangle 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "bcfe5b13211b4d78e5c2cadfebd7769197d95c639c35a50057eb4c05de811395" +"checksum ryu 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "bfa8506c1de11c9c4e4c38863ccbe02a305c8188e85a05a784c9e11e1c3910c8" "checksum schannel 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "87f550b06b6cba9c8b8be3ee73f391990116bf527450d2556e9b9ce263b9a021" "checksum scoped_threadpool 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8" +"checksum serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)" = "414115f25f818d7dfccec8ee535d76949ae78584fc4f79a6f45a904bf8ab4449" +"checksum serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)" = "128f9e303a5a29922045a830221b8f78ec74a5f544944f3d5984f8ec3895ef64" +"checksum serde_json 1.0.44 (registry+https://github.com/rust-lang/crates.io-index)" = "48c575e0cc52bdd09b47f330f646cf59afc586e9c4e3ccd6fc1f625b8ea1dad7" "checksum socket2 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)" = "e8b74de517221a2cb01a53349cf54182acdc31a074727d3079068448c0676d85" "checksum syn 0.14.9 (registry+https://github.com/rust-lang/crates.io-index)" = "261ae9ecaa397c42b960649561949d69311f08eeaea86a65696e6e46517cf741" "checksum syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)" = "dff0acdb207ae2fe6d5976617f887eb1e35a2ba52c13c7234c790960cdad9238" diff --git a/Cargo.toml b/Cargo.toml index 18761a2..1df9d45 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,5 @@ dirs = "1.0.4" cfg-if="0.1.10" curl = "0.4.25" derive_more = "0.99.2" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" diff --git a/src/gitmoji.rs b/src/gitmoji.rs index c575998..849c37b 100644 --- a/src/gitmoji.rs +++ b/src/gitmoji.rs @@ -1,10 +1,12 @@ extern crate git2; extern crate curl; extern crate derive_more; +extern crate serde; +extern crate serde_json; use std::path::{Path, PathBuf}; -use std::io::Write; use std::fs::File; +use std::io::{Read, Write}; use self::derive_more::{Display, From}; @@ -13,6 +15,8 @@ use self::git2::build::RepoBuilder; use self::curl::easy::Easy; +use self::serde::{Serialize, Deserialize}; + #[derive(Debug, From, Display)] pub enum RetrievingError { Git(git2::Error), @@ -20,6 +24,12 @@ pub enum RetrievingError { IO(std::io::Error), } +#[derive(Debug, From, Display)] +pub enum LoadError { + IO(std::io::Error), + JSON(self::serde_json::Error), +} + pub struct Url { repo_url: String, json_url: String, @@ -114,3 +124,27 @@ pub fn update(url: &Url, repo_path: &Path, json_path: &Path) -> Result<(), Retri Ok(()) } + +#[derive(Serialize, Deserialize, Debug)] +pub struct Gitmoji { + pub emoji: String, + pub entity: Option, + pub code: String, + pub description: String, + pub name: String, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct Gitmojis { + pub gitmojis: Vec, +} + +impl Gitmojis { + pub fn load(file: &Path) -> Result { + let mut json = String::new(); + File::open(file)?.read_to_string(&mut json)?; + let ret: Gitmojis = serde_json::from_str(&json)?; + + Ok(ret) + } +} diff --git a/src/main.rs b/src/main.rs index 4aa2515..28160ea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,9 +2,15 @@ mod settings; mod gitmoji; use settings::Settings; +use gitmoji::Gitmojis; fn main() { let settings = Settings::new(None); gitmoji::update(&gitmoji::Url::default_github(), &settings.repo_path, &settings.json_path).unwrap(); + + let gitmojis = Gitmojis::load(&settings.json_path).unwrap(); + for gitmoji in gitmojis.gitmojis { + println!("{} - {}", gitmoji.emoji, gitmoji.description); + } }