Start refactoring structs out into modules for cleaner code
This commit is contained in:
parent
6d8e7d1b6b
commit
dbfd92614a
|
@ -0,0 +1,106 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use url::Url;
|
||||
|
||||
use crate::AppState;
|
||||
|
||||
/*
|
||||
* Representation of the Janky YAML format
|
||||
*/
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
pub struct JankyYml {
|
||||
pub needs: Vec<String>,
|
||||
pub commands: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Scm {
|
||||
GitHub {
|
||||
owner: String,
|
||||
repo: String,
|
||||
#[serde(rename = "ref")]
|
||||
scm_ref: String,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub struct Project {
|
||||
description: String,
|
||||
pub filename: String,
|
||||
#[serde(with = "serde_yaml::with::singleton_map")]
|
||||
pub scm: Scm,
|
||||
}
|
||||
|
||||
/*
|
||||
* Internal representation of an Agent that has been "loaded" by the server
|
||||
*
|
||||
* Loaded meaning the server has pinged the agent and gotten necessary bootstrap
|
||||
* information
|
||||
*/
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub struct Agent {
|
||||
name: String,
|
||||
pub url: Url,
|
||||
capabilities: Vec<janky::Capability>,
|
||||
}
|
||||
|
||||
impl Agent {
|
||||
pub fn new(name: String, url: Url, capabilities: Vec<janky::Capability>) -> Self {
|
||||
Self {
|
||||
name,
|
||||
url,
|
||||
capabilities,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render_compact(&self, _state: &AppState<'_>) -> String {
|
||||
"".into()
|
||||
//let data = serde_json::to_str(self).unwrap_or(serde_json::Value::Array);
|
||||
|
||||
//state.render("views/components/agent/compact.hbs",
|
||||
// data: data).await.unwrap_or("".into())
|
||||
}
|
||||
|
||||
pub fn can_meet(&self, needs: &Vec<String>) -> bool {
|
||||
// TODO: Improve the performance of this by reducing the clones
|
||||
let mut needs = needs.clone();
|
||||
needs.sort();
|
||||
|
||||
let mut capabilities: Vec<String> = self
|
||||
.capabilities
|
||||
.iter()
|
||||
.map(|c| c.name.to_lowercase())
|
||||
.collect();
|
||||
capabilities.sort();
|
||||
capabilities == needs
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct AgentConfig {
|
||||
pub url: Url,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct ServerConfig {
|
||||
pub agents: HashMap<String, AgentConfig>,
|
||||
pub projects: HashMap<String, Project>,
|
||||
}
|
||||
|
||||
impl ServerConfig {
|
||||
pub fn has_project(&self, name: &str) -> bool {
|
||||
self.projects.contains_key(name)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ServerConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
agents: HashMap::default(),
|
||||
projects: HashMap::default(),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -5,7 +5,7 @@
|
|||
use chrono::{DateTime, NaiveDateTime, Utc};
|
||||
use serde::Serialize;
|
||||
use sqlx::sqlite::SqliteQueryResult;
|
||||
use sqlx::{Sqlite, SqlitePool, Transaction};
|
||||
use sqlx::SqlitePool;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
#[macro_use]
|
||||
extern crate serde_json;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use async_std::sync::{Arc, RwLock};
|
||||
|
@ -13,13 +12,15 @@ use dotenv::dotenv;
|
|||
use gumdrop::Options;
|
||||
use handlebars::Handlebars;
|
||||
use log::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::SqlitePool;
|
||||
use url::Url;
|
||||
|
||||
mod config;
|
||||
mod dao;
|
||||
mod routes;
|
||||
|
||||
use crate::config::*;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct AppState<'a> {
|
||||
pub db: SqlitePool,
|
||||
|
@ -60,95 +61,6 @@ impl AppState<'_> {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
struct JankyYml {
|
||||
needs: Vec<String>,
|
||||
commands: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
enum Scm {
|
||||
GitHub {
|
||||
owner: String,
|
||||
repo: String,
|
||||
#[serde(rename = "ref")]
|
||||
scm_ref: String,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
struct Project {
|
||||
description: String,
|
||||
filename: String,
|
||||
#[serde(with = "serde_yaml::with::singleton_map")]
|
||||
scm: Scm,
|
||||
}
|
||||
|
||||
/*
|
||||
* Internal representation of an Agent that has been "loaded" by the server
|
||||
*
|
||||
* Loaded meaning the server has pinged the agent and gotten necessary bootstrap
|
||||
* information
|
||||
*/
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub struct Agent {
|
||||
name: String,
|
||||
url: Url,
|
||||
capabilities: Vec<janky::Capability>,
|
||||
}
|
||||
|
||||
impl Agent {
|
||||
pub fn render_compact(&self, state: &AppState<'_>) -> String {
|
||||
"".into()
|
||||
//let data = serde_json::to_str(self).unwrap_or(serde_json::Value::Array);
|
||||
|
||||
//state.render("views/components/agent/compact.hbs",
|
||||
// data: data).await.unwrap_or("".into())
|
||||
}
|
||||
|
||||
pub fn can_meet(&self, needs: &Vec<String>) -> bool {
|
||||
// TODO: Improve the performance of this by reducing the clones
|
||||
let mut needs = needs.clone();
|
||||
needs.sort();
|
||||
|
||||
let mut capabilities: Vec<String> = self
|
||||
.capabilities
|
||||
.iter()
|
||||
.map(|c| c.name.to_lowercase())
|
||||
.collect();
|
||||
capabilities.sort();
|
||||
capabilities == needs
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
struct AgentConfig {
|
||||
url: Url,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct ServerConfig {
|
||||
agents: HashMap<String, AgentConfig>,
|
||||
projects: HashMap<String, Project>,
|
||||
}
|
||||
|
||||
impl ServerConfig {
|
||||
fn has_project(&self, name: &str) -> bool {
|
||||
self.projects.contains_key(name)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ServerConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
agents: HashMap::default(),
|
||||
projects: HashMap::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Options)]
|
||||
struct ServerOptions {
|
||||
#[options(help = "print help message")]
|
||||
|
@ -209,11 +121,11 @@ async fn main() -> Result<(), tide::Error> {
|
|||
.await?
|
||||
.json()
|
||||
.await?;
|
||||
state.agents.push(Agent {
|
||||
name: name.clone(),
|
||||
url: agent.url.clone(),
|
||||
capabilities: response.caps,
|
||||
});
|
||||
state.agents.push(Agent::new(
|
||||
name.to_string(),
|
||||
agent.url.clone(),
|
||||
response.caps,
|
||||
));
|
||||
}
|
||||
|
||||
state
|
||||
|
|
|
@ -11,9 +11,12 @@ use tide::{Body, Request};
|
|||
* GET /
|
||||
*/
|
||||
pub async fn index(req: Request<AppState<'_>>) -> Result<Body, tide::Error> {
|
||||
let agents: Vec<String> = req.state().agents
|
||||
.iter()
|
||||
.map(|a| a.render_compact(req.state())).collect();
|
||||
let agents: Vec<String> = req
|
||||
.state()
|
||||
.agents
|
||||
.iter()
|
||||
.map(|a| a.render_compact(req.state()))
|
||||
.collect();
|
||||
let params = json!({
|
||||
"page": "home",
|
||||
"agents" : agents,
|
||||
|
@ -41,7 +44,8 @@ pub async fn project(req: Request<AppState<'_>>) -> Result<Body, tide::Error> {
|
|||
}
|
||||
|
||||
pub mod api {
|
||||
use crate::{AppState, JankyYml, Scm};
|
||||
use crate::config::{JankyYml, Scm};
|
||||
use crate::AppState;
|
||||
use log::*;
|
||||
use serde::Deserialize;
|
||||
use tide::{Request, Response, StatusCode};
|
||||
|
|
Loading…
Reference in New Issue