Add library package for shared code

This commit is contained in:
Nick Rolfe
2020-10-23 13:01:17 +01:00
parent 305fd566a8
commit 849e109583
6 changed files with 32 additions and 12 deletions

11
node-types/Cargo.toml Normal file
View File

@@ -0,0 +1,11 @@
[package]
name = "node-types"
version = "0.1.0"
authors = ["GitHub"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

74
node-types/src/lib.rs Normal file
View File

@@ -0,0 +1,74 @@
use serde::Deserialize;
use std::collections::BTreeMap;
use std::fmt;
use std::path::Path;
#[derive(Deserialize)]
pub struct NodeInfo {
#[serde(rename = "type")]
pub kind: String,
pub named: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub fields: Option<BTreeMap<String, FieldInfo>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub children: Option<FieldInfo>,
#[serde(skip_serializing_if = "Option::is_none")]
pub subtypes: Option<Vec<NodeType>>,
}
#[derive(Deserialize)]
pub struct NodeType {
#[serde(rename = "type")]
pub kind: String,
pub named: bool,
}
#[derive(Deserialize)]
pub struct FieldInfo {
pub multiple: bool,
pub required: bool,
pub types: Vec<NodeType>,
}
impl Default for FieldInfo {
fn default() -> Self {
FieldInfo {
multiple: false,
required: true,
types: Vec::new(),
}
}
}
pub enum Error {
IOError(std::io::Error),
JsonError(serde_json::error::Error),
}
impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Self {
Error::IOError(error)
}
}
impl From<serde_json::Error> for Error {
fn from(error: serde_json::Error) -> Self {
Error::JsonError(error)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::IOError(e) => write!(f, "{}", e),
Error::JsonError(e) => write!(f, "{}", e),
}
}
}
/// Deserializes the node types from the JSON at the given `path`.
pub fn read(path: &Path) -> Result<Vec<NodeInfo>, Error> {
let json_data = std::fs::read_to_string(path)?;
let node_types: Vec<NodeInfo> = serde_json::from_str(&json_data)?;
Ok(node_types)
}