Rewrite rust (#38)
Co-authored-by: kjuulh <contact@kjuulh.io> Reviewed-on: https://git.front.kjuulh.io/kjuulh/octopush/pulls/38
This commit is contained in:
2
crates/octopush_core/src/schema/mod.rs
Normal file
2
crates/octopush_core/src/schema/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod models;
|
||||
pub mod parser;
|
||||
73
crates/octopush_core/src/schema/models.rs
Normal file
73
crates/octopush_core/src/schema/models.rs
Normal file
@@ -0,0 +1,73 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub type Repository = String;
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
|
||||
pub struct GitPushBranch {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
|
||||
pub struct GitPushPullRequest {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
|
||||
pub struct GitPush {
|
||||
pub branch: GitPushBranch,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
|
||||
pub struct Git {
|
||||
pub push: Option<GitPush>,
|
||||
pub repositories: Vec<Repository>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
|
||||
pub struct GitHubPush {
|
||||
#[serde(rename = "pull-request")]
|
||||
pub pull_request: GitPushPullRequest,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
|
||||
pub struct GiteaPush {
|
||||
#[serde(rename = "pull-request")]
|
||||
pub pull_request: GitPushPullRequest,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
|
||||
pub struct GitHub {
|
||||
pub push: Option<GitHubPush>,
|
||||
pub repositories: Vec<Repository>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
|
||||
pub struct Gitea {
|
||||
pub push: Option<GiteaPush>,
|
||||
pub repositories: Vec<Repository>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
|
||||
pub struct SelectAction {
|
||||
pub git: Option<Git>,
|
||||
pub github: Option<GitHub>,
|
||||
pub gitea: Option<Gitea>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum Action {
|
||||
#[serde(rename = "go")]
|
||||
Go { entry: String },
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
|
||||
#[serde(tag = "apiVersion")]
|
||||
pub enum Schema {
|
||||
#[serde(rename = "action")]
|
||||
Action {
|
||||
name: String,
|
||||
select: SelectAction,
|
||||
action: Action,
|
||||
},
|
||||
}
|
||||
36
crates/octopush_core/src/schema/parser.rs
Normal file
36
crates/octopush_core/src/schema/parser.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
use std::{path::PathBuf, sync::Arc};
|
||||
|
||||
use async_trait::async_trait;
|
||||
|
||||
use super::models::Schema;
|
||||
|
||||
#[async_trait]
|
||||
pub trait SchemaParser {
|
||||
async fn parse_file(&self, file: PathBuf) -> eyre::Result<Schema>;
|
||||
}
|
||||
|
||||
pub type DynSchemaParser = Arc<dyn SchemaParser + Send + Sync>;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DefaultSchemaParser {}
|
||||
|
||||
#[async_trait]
|
||||
impl SchemaParser for DefaultSchemaParser {
|
||||
async fn parse_file(&self, file: PathBuf) -> eyre::Result<Schema> {
|
||||
let file = tokio::fs::read(file).await?;
|
||||
|
||||
self.parse(file)
|
||||
}
|
||||
}
|
||||
|
||||
impl DefaultSchemaParser {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
|
||||
pub fn parse(&self, contents: Vec<u8>) -> eyre::Result<Schema> {
|
||||
let schema = serde_yaml::from_slice(contents.as_slice())?;
|
||||
|
||||
Ok(schema)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user