Skip to content

Commit

Permalink
splitting most basic logic into a separate enso-build-base crate
Browse files Browse the repository at this point in the history
  • Loading branch information
mwu-tow committed Oct 13, 2022
1 parent 948e2c2 commit a5a7c29
Show file tree
Hide file tree
Showing 35 changed files with 559 additions and 486 deletions.
28 changes: 21 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions build/base/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "enso-build-base"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.65"
fn-error-context = "0.2.0"
futures-util = "0.3.24"
futures = "0.3.24"
serde = "1.0.145"
serde_json = "1.0.85"
serde_yaml = "0.9.13"
tracing = "0.1.36"
11 changes: 11 additions & 0 deletions build/base/src/extensions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// ==============
// === Export ===
// ==============

pub mod from_string;
pub mod future;
pub mod iterator;
pub mod maps;
pub mod path;
pub mod result;
pub mod str;
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ use std::any::type_name;



/// An equivalent of standard's library `std::str::FromStr` trait, but with nice error messages.
pub trait FromString: Sized {
/// Parse a string into a value of this type. See: `std::str::FromStr::from_str`.
fn from_str(s: &str) -> Result<Self>;

/// Parse a string into a value of this type and then convert it to `R`.
fn parse_into<R>(text: impl AsRef<str>) -> Result<R>
where
Self: TryInto<R>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,6 @@ pub trait TryFutureExt: TryFuture {
impl<T: ?Sized> TryFutureExt for T where T: TryFuture {}


pub fn receiver_to_stream<T>(
mut receiver: tokio::sync::mpsc::Receiver<T>,
) -> impl Stream<Item = T> {
futures::stream::poll_fn(move |ctx| receiver.poll_recv(ctx))
}



pub trait TryStreamExt: TryStream {
fn anyhow_err(self) -> stream::MapErr<Self, fn(Self::Error) -> anyhow::Error>
where
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use serde::de::DeserializeOwned;


pub trait PathExt: AsRef<Path> {
/// Append multiple segments to this path.
fn join_iter<P: AsRef<Path>>(&self, segments: impl IntoIterator<Item = P>) -> PathBuf {
let mut ret = self.as_ref().to_path_buf();
ret.extend(segments);
Expand Down Expand Up @@ -42,18 +43,27 @@ pub trait PathExt: AsRef<Path> {
}
}

/// Parse this file's contents as a JSON-serialized value.
#[context("Failed to deserialize file `{}` as type `{}`.", self.as_ref().display(), std::any::type_name::<T>())]
fn read_to_json<T: DeserializeOwned>(&self) -> Result<T> {
let content = crate::fs::read_to_string(self)?;
serde_json::from_str(&content).anyhow_err()
}

/// Write this file with a JSON-serialized value.
fn write_as_json<T: Serialize>(&self, value: &T) -> Result {
trace!("Writing JSON to {}.", self.as_ref().display());
let file = crate::fs::create(self)?;
serde_json::to_writer(file, value).anyhow_err()
}

/// Parse this file's contents as a YAML-serialized value.
fn read_to_yaml<T: DeserializeOwned>(&self) -> Result<T> {
let content = crate::fs::read_to_string(self)?;
serde_yaml::from_str(&content).anyhow_err()
}

/// Write this file with a YAML-serialized value.
fn write_as_yaml<T: Serialize>(&self, value: &T) -> Result {
trace!("Writing YAML to {}.", self.as_ref().display());
let file = crate::fs::create(self)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ pub trait ResultExt<T, E>: Sized {
E: Into<E2>,
T2: Send + 'a,
E2: Send + 'a;


fn anyhow_err(self) -> Result<T>
where E: Into<anyhow::Error>;

fn flatten_fut(
self,
) -> futures::future::Either<
std::future::Ready<std::result::Result<T::Ok, T::Error>>,
futures::future::IntoFuture<T>,
>
where T: TryFuture<Error: From<E>>;
}

impl<T, E> ResultExt<T, E> for std::result::Result<T, E> {
Expand Down Expand Up @@ -62,4 +74,22 @@ impl<T, E> ResultExt<T, E> for std::result::Result<T, E> {
Err(e) => ready(Err(e.into())).right_future(),
}
}

fn anyhow_err(self) -> Result<T>
where E: Into<anyhow::Error> {
self.map_err(E::into)
}

fn flatten_fut(
self,
) -> futures::future::Either<
std::future::Ready<std::result::Result<T::Ok, T::Error>>,
futures::future::IntoFuture<T>,
>
where T: TryFuture<Error: From<E>> {
match self {
Ok(fut) => fut.into_future().right_future(),
Err(e) => ready(Err(T::Error::from(e))).left_future(),
}
}
}
File renamed without changes.
Loading

0 comments on commit a5a7c29

Please sign in to comment.