orphanage

Check-in Differences
Login

Check-in Differences

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Difference From orphanage-0.1.0 To orphanage-0.1.1

2024-10-28
03:42
Add object name spec string. check-in: 7716fd0685 user: jan tags: trunk
2024-10-19
13:53
Change log. check-in: 3a8934a118 user: jan tags: orphanage-0.1.1, trunk
2024-10-07
01:18
Exclude examples from publishing. check-in: d41a277a0d user: jan tags: orphanage-0.1.0, trunk
01:15
Release maintenance. check-in: 2fdfd4c082 user: jan tags: trunk

Changes to Cargo.toml.

1
2
3
4
5
6
7
8
9
10
[package]
name = "orphanage"
version = "0.1.0"
edition = "2021"
license = "0BSD"
# https://crates.io/category_slugs
categories = [ "network-programming" ]
keywords = [ "sqlite", "fs", "path" ]
repository = "https://repos.qrnch.tech/pub/orphanage"
description = "Random collection of stuff that is still searching for a home."


|







1
2
3
4
5
6
7
8
9
10
[package]
name = "orphanage"
version = "0.1.1"
edition = "2021"
license = "0BSD"
# https://crates.io/category_slugs
categories = [ "network-programming" ]
keywords = [ "sqlite", "fs", "path" ]
repository = "https://repos.qrnch.tech/pub/orphanage"
description = "Random collection of stuff that is still searching for a home."

Changes to src/path.rs.

1
2
3
4
5
6
7


8

9
10


















































11
12
13
14
15
16
17
//! Utility functions that extend `std::path`.
//!
//! There's some functionality that overlaps with [`fs`](super::fs), but the
//! main difference is that functions in `fs` assume the file system objects
//! exists, while functions in the `path` submodule (generally) make no such
//! assumptions.



use std::path::{Path, PathBuf};


use crate::err::Error;



















































/// Expand a string path and return it as a `PathBuf`.
///
/// The expansion is done using [`shellexpand::full()`].
///
/// # Errors
/// [`Error::BadFormat`] means the path could not be expanded.







>
>
|
>


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! Utility functions that extend `std::path`.
//!
//! There's some functionality that overlaps with [`fs`](super::fs), but the
//! main difference is that functions in `fs` assume the file system objects
//! exists, while functions in the `path` submodule (generally) make no such
//! assumptions.

use std::{
  fs,
  path::{Path, PathBuf}
};

use crate::err::Error;

/// Path extensions.
#[allow(clippy::module_name_repetitions)]
pub trait PathExt {
  /// Expand a path and return it.
  ///
  /// # Errors
  /// [`Error::BadFormat`] means the input path could not be expanded.
  fn expand(input: &str) -> Result<PathBuf, Error> {
    match shellexpand::full(&input) {
      Ok(value) => Ok(PathBuf::from(value.into_owned())),
      Err(e) => Err(Error::BadFormat(format!("Unable to expand path; {e}")))
    }
  }

  /// Expand a path, run a closure on the expanded path, and then return the
  /// expanded path.
  ///
  /// # Errors
  /// [`Error::BadFormat`] means the input path could not be expanded.
  fn expand_and<F>(input: &str, f: F) -> Result<PathBuf, Error>
  where
    F: FnOnce(&Path) -> Result<(), Error>
  {
    let expanded = PathBuf::expand(input)?;
    f(&expanded)?;
    Ok(expanded)
  }

  /// Expand a path, run a closure on the expanded path, canonicalize the path
  /// and then return it.
  ///
  /// The canonicalization occurs _after_ the closure has been called because
  /// the the canonicalization may not be possible until the closure has run
  /// (for instance, if the closure is meant to create the file system object
  /// that is meant to be canonicalized).
  ///
  /// # Errors
  /// [`Error::BadFormat`] means the input path could not be expanded.
  fn expand_and_canon<F>(input: &str, f: F) -> Result<PathBuf, Error>
  where
    F: FnOnce(&Path) -> Result<(), Error>
  {
    let expanded = PathBuf::expand_and(input, f)?;
    Ok(fs::canonicalize(&expanded)?)
  }
}

impl PathExt for PathBuf {}


/// Expand a string path and return it as a `PathBuf`.
///
/// The expansion is done using [`shellexpand::full()`].
///
/// # Errors
/// [`Error::BadFormat`] means the path could not be expanded.
76
77
78
79
80
81
82

































83
      basedir.join(pth)
    }
  }

  inner(basedir, pth.as_ref())
}


































// vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
      basedir.join(pth)
    }
  }

  inner(basedir, pth.as_ref())
}


/*
/// Expand, perform action on expanded path.
pub fn expand_and<F>(input: impl AsRef<str>, f: F) -> Result<PathBuf, Error>
where
  F: FnOnce(&Path) -> Result<(), Error>
{
  let expanded = match shellexpand::full(&input) {
    Ok(value) => Ok(PathBuf::from(value.into_owned())),
    Err(e) => Err(Error::BadFormat(format!("Unable to expand path; {e}")))
  }?;

  // Call closure to process the expanded path
  f(&expanded)?;

  Ok(expanded)
}

/// Expand, perform action on expanded path and return canonicalized path.
pub fn expand_canonizalize<F>(
  input: impl AsRef<str>,
  f: F
) -> Result<PathBuf, Error>
where
  F: FnOnce(&Path) -> Result<(), Error>
{
  let expanded = expand_and(input, f)?;

  // Canonicalize
  Ok(fs::canonicalize(&expanded)?)
}
*/

// vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :

Changes to www/changelog.md.

1
2
3
4
5
6
7







8
9
10
11
12
13
14
# Change Log

## [Unreleased]

[Details](/vdiff?from=orphanage-0.1.0&to=trunk)

### Added








### Changed

### Removed

---








>
>
>
>
>
>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Change Log

## [Unreleased]

[Details](/vdiff?from=orphanage-0.1.0&to=trunk)

### Added

- Add `path::PathExt`, and implement it for `PathBuf`.
  - `PathBuf::expand()` expands a string to a `PathBuf` and returns it.
  - `PathBuf::expand_and()` works like `PathBuf::expand()` but it runs a
     closure on the expanded pwth before returning it.
  - `PathBuf::expand_and_canon()` works like `PathBuf::expand_and()` but it
    canonicalizes the path just before returning it.

### Changed

### Removed

---