dennisdoomen / dennisdoomen/pathy
[API Proposal]: WithExtension, WithName and WithoutExtension
- Dominant language
- C#
- Stars
- 27
- Forks
- 5
- Avg merge
- 21h 29m
- Merged PRs (30d)
- 10
Description
### Background and motivation
Deriving one path from another by changing its file name or extension is routine: turning a `.cs` file into a `.g.cs` file, turning a project file into its package name, changing `.md` into `.html`. Right now this means going back to strings, taking the `Directory`, and re-chaining, which is verbose and easy to get wrong when the original has no extension.
### API Proposal
```C#
namespace Pathy
{
public readonly struct ChainablePath
{
public ChainablePath WithExtension(string extension);
public ChainablePath WithName(string name);
public ChainablePath WithoutExtension();
}
}
```
`WithExtension` accepts the extension with or without a leading dot, matching the existing tolerance of `HasExtension`.
### API Usage
```C#
var source = ChainablePath.From("c:/work/repo/docs/readme.md");
source.WithExtension(".html"); // c:/work/repo/docs/readme.html
source.WithExtension("html"); // same result
source.WithoutExtension(); // c:/work/repo/docs/readme
source.WithName("index.md"); // c:/work/repo/docs/index.md
```
A realistic use in a build script:
```C#
foreach (var page in (docs).GlobFiles("**/*.md"))
{
Render(page, page.WithExtension(".html"));
}
```
### Alternative Designs
* A single `Rename(Func)` that transforms the name. More flexible but much less readable at the call site.
* Setter-style properties. Not an option on a readonly struct, and mutation is the wrong model here.
### Risks
`WithExtension(string.Empty)` and `WithExtension(null)` need defined behaviour. Behaviour on paths that have no name at all (a root, or `ChainablePath.Null`) needs to be defined as well, most likely by throwing.
Contributor guide
Research direction
Start at the ChainablePath API and inspect the existing HasExtension behavior, especially its tolerance for a leading dot. Define the behavior for empty or null extensions and paths without names, then confirm the proposed methods cover the shown file-name and extension transformations.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Needs clarification
- Newbie friendliness
- 45/100