Skip to content

Commit

Permalink
feat: add depit update
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Volosatovs <[email protected]>
  • Loading branch information
rvolosatovs committed Apr 11, 2023
1 parent 8d4b929 commit 37d2c0c
Show file tree
Hide file tree
Showing 11 changed files with 248 additions and 62 deletions.
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.2.2] - 2023-04-11

### Added

- `depit update` along with the `depit::update_path` and `depit::update` library API

## [0.2.1] - 2023-04-10

### Fixed
Expand All @@ -27,7 +33,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Initial `depit` library and binary implementations

[unreleased]: https://github.com/rvolosatovs/depit/compare/v0.2.1...HEAD
[0.2.1]: https://github.com/rvolosatovs/depit/releases/tag/v0.2.0
[unreleased]: https://github.com/rvolosatovs/depit/compare/v0.2.2...HEAD
[0.2.2]: https://github.com/rvolosatovs/depit/releases/tag/v0.2.2
[0.2.1]: https://github.com/rvolosatovs/depit/releases/tag/v0.2.1
[0.2.0]: https://github.com/rvolosatovs/depit/releases/tag/v0.2.0
[0.1.0]: https://github.com/rvolosatovs/depit/releases/tag/v0.1.0
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "depit-cli"
version = "0.2.1"
version = "0.2.2"
description = "WIT dependency manager"

authors.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/depit/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "depit"
version = "0.2.1"
version = "0.2.2"
description = "WIT dependency management"
readme = "../../README.md"

Expand Down
120 changes: 111 additions & 9 deletions crates/depit/src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use core::fmt;
use core::ops::{Deref, DerefMut};

use std::ffi::{OsStr, OsString};
use std::path::{Path, PathBuf};

use anyhow::{bail, Context as _};
use async_trait::async_trait;
use directories::ProjectDirs;
use futures::{io::BufReader, AsyncBufRead, AsyncWrite};
use tokio::fs::{self, File, OpenOptions};
use tokio_util::compat::{Compat, TokioAsyncReadCompatExt};
Expand All @@ -22,13 +27,86 @@ pub trait Cache {
async fn insert(&self, url: &Url) -> anyhow::Result<Self::Write>;
}

/// Write-only [Cache] wrapper
pub struct Write<T>(pub T);

impl<T> From<T> for Write<T> {
fn from(cache: T) -> Self {
Self(cache)
}
}

impl<T> Deref for Write<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<T> DerefMut for Write<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

#[async_trait]
impl<T: Cache + Sync + Send> Cache for Write<T> {
type Read = T::Read;
type Write = T::Write;

async fn get(&self, _: &Url) -> anyhow::Result<Option<Self::Read>> {
Ok(None)
}

async fn insert(&self, url: &Url) -> anyhow::Result<Self::Write> {
self.0.insert(url).await
}
}

impl<T> Write<T> {
/// Extracts the inner [Cache]
pub fn into_inner(self) -> T {
self.0
}
}

/// Local caching layer
#[derive(Clone, Debug)]
pub struct Local<'a>(&'a Path);
pub struct Local(PathBuf);

impl fmt::Display for Local {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0.display())
}
}

impl Deref for Local {
type Target = PathBuf;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for Local {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl Local {
/// Returns a [Local] cache located at the default system-specific cache directory if such
/// could be determined.
pub fn cache_dir() -> Option<Self> {
ProjectDirs::from("", "", env!("CARGO_PKG_NAME"))
.as_ref()
.map(ProjectDirs::cache_dir)
.map(Self::from)
}

impl Local<'_> {
fn path(&self, url: &Url) -> impl AsRef<Path> {
let mut path = PathBuf::from(self.0);
let mut path = self.0.clone();
match url.host() {
Some(Host::Ipv4(ip)) => {
path.push(ip.to_string());
Expand All @@ -51,7 +129,7 @@ impl Local<'_> {
}

#[async_trait]
impl Cache for Local<'_> {
impl Cache for Local {
type Read = BufReader<Compat<File>>;
type Write = Compat<File>;

Expand Down Expand Up @@ -80,15 +158,39 @@ impl Cache for Local<'_> {
}
}

impl<'a> From<&'a Path> for Local<'a> {
fn from(path: &'a Path) -> Self {
impl From<PathBuf> for Local {
fn from(path: PathBuf) -> Self {
Self(path)
}
}

impl<'a> From<&'a str> for Local<'a> {
fn from(path: &'a str) -> Self {
Self::from(Path::new(path))
impl From<String> for Local {
fn from(path: String) -> Self {
Self(path.into())
}
}

impl From<OsString> for Local {
fn from(path: OsString) -> Self {
Self(path.into())
}
}

impl From<&Path> for Local {
fn from(path: &Path) -> Self {
Self(path.into())
}
}

impl From<&str> for Local {
fn from(path: &str) -> Self {
Self(path.into())
}
}

impl From<&OsStr> for Local {
fn from(path: &OsStr) -> Self {
Self(path.into())
}
}

Expand Down
Loading

0 comments on commit 37d2c0c

Please sign in to comment.