Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
elmerbulthuis committed Feb 20, 2024
1 parent 3e17014 commit c96cd7e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
8 changes: 5 additions & 3 deletions packages/cargo/jns42-generator/src/utils/read_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ use url::Url;

pub type ReadUrlItem = Result<Vec<u8>, Box<dyn Error>>;

pub async fn read_url(url: &Url) -> Result<Box<dyn Stream<Item = ReadUrlItem>>, Box<dyn Error>> {
pub async fn read_url(
url: &Url,
) -> Result<Box<dyn Stream<Item = ReadUrlItem> + Unpin>, Box<dyn Error>> {
let scheme = url.scheme();
match scheme {
"file:" => {
"file" => {
let file = File::open(url.to_file_path().unwrap()).await?;
let stream = ReadStream::new(file);
let stream = stream.map(|item| item.map_err(|error| Box::new(error) as Box<dyn Error>));

Ok(Box::new(stream))
}
"http:" | "https:" => {
"http" | "https" => {
let response = reqwest::get(url.as_str()).await?.error_for_status()?;
let stream = response.bytes_stream();
let stream = stream.map(|item| {
Expand Down
11 changes: 4 additions & 7 deletions packages/cargo/jns42-generator/src/utils/yaml.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
use crate::utils::yaml_deserializer::YamlDeserializer;
use futures_util::StreamExt;
use futures_util::TryStreamExt;
use std::error::Error;
use url::Url;

use super::read_url::read_url;

pub async fn load_yaml(url: &Url) -> Result<Option<serde_json::Value>, Box<dyn std::error::Error>> {
let response = reqwest::get(url.as_str()).await?.error_for_status()?;
let body = response
.bytes_stream()
.map_err(|error| Box::new(error) as Box<dyn Error>);
let stream = read_url(url).await?;

let mut deserializer = YamlDeserializer::new(body);
let mut deserializer = YamlDeserializer::new(stream);
if let Some(item) = deserializer.next().await {
let item = item?;

Expand Down

0 comments on commit c96cd7e

Please sign in to comment.