Skip to content

Commit

Permalink
Merge pull request #1089 from rainlanguage/2024-12-18-dotrain-yaml
Browse files Browse the repository at this point in the history
Creating DotrainYaml struct
  • Loading branch information
hardyjosh authored Dec 20, 2024
2 parents 212f110 + d0e7e35 commit cc42e57
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
22 changes: 22 additions & 0 deletions crates/settings/src/yaml/dotrain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use super::*;
use std::sync::{Arc, RwLock};

#[derive(Debug, Clone)]
pub struct DotrainYaml {
pub document: Arc<RwLock<StrictYaml>>,
}

impl YamlParsable for DotrainYaml {
fn new(source: String, _validate: bool) -> Result<Self, YamlError> {
let docs = StrictYamlLoader::load_from_str(&source)?;
if docs.is_empty() {
return Err(YamlError::EmptyFile);
}
let doc = docs[0].clone();
let document = Arc::new(RwLock::new(doc));

Ok(DotrainYaml { document })
}
}

impl DotrainYaml {}
14 changes: 14 additions & 0 deletions crates/settings/src/yaml/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod dotrain;
pub mod orderbook;

use crate::{
Expand All @@ -7,13 +8,26 @@ use crate::{
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::sync::{PoisonError, RwLockReadGuard, RwLockWriteGuard};
use strict_yaml_rust::StrictYamlEmitter;
use strict_yaml_rust::{
strict_yaml::{Array, Hash},
EmitError, ScanError, StrictYaml, StrictYamlLoader,
};
use thiserror::Error;
use url::ParseError as UrlParseError;

pub trait YamlParsable: Sized {
fn new(source: String, validate: bool) -> Result<Self, YamlError>;

fn get_yaml_string(document: Arc<RwLock<StrictYaml>>) -> Result<String, YamlError> {
let document = document.read().unwrap();
let mut out_str = String::new();
let mut emitter = StrictYamlEmitter::new(&mut out_str);
emitter.dump(&document)?;
Ok(out_str)
}
}

pub trait YamlParsableHash: Sized + Clone {
fn parse_all_from_yaml(
document: Arc<RwLock<StrictYaml>>,
Expand Down
15 changes: 4 additions & 11 deletions crates/settings/src/yaml/orderbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ use crate::{
metaboard::Metaboard, sentry::Sentry, subgraph::Subgraph, Deployer, Network, Orderbook, Token,
};
use std::sync::{Arc, RwLock};
use strict_yaml_rust::StrictYamlEmitter;

#[derive(Debug, Clone)]
pub struct OrderbookYaml {
pub document: Arc<RwLock<StrictYaml>>,
}

impl OrderbookYaml {
pub fn new(source: String, validate: bool) -> Result<Self, YamlError> {
impl YamlParsable for OrderbookYaml {
fn new(source: String, validate: bool) -> Result<Self, YamlError> {
let docs = StrictYamlLoader::load_from_str(&source)?;
if docs.is_empty() {
return Err(YamlError::EmptyFile);
Expand All @@ -24,15 +23,9 @@ impl OrderbookYaml {
}
Ok(OrderbookYaml { document })
}
}

pub fn get_yaml_string(&self) -> Result<String, YamlError> {
let document = self.document.read().unwrap();
let mut out_str = String::new();
let mut emitter = StrictYamlEmitter::new(&mut out_str);
emitter.dump(&document)?;
Ok(out_str)
}

impl OrderbookYaml {
pub fn get_network_keys(&self) -> Result<Vec<String>, YamlError> {
let networks = Network::parse_all_from_yaml(self.document.clone())?;
Ok(networks.keys().cloned().collect())
Expand Down

0 comments on commit cc42e57

Please sign in to comment.