Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for fallible sanitizers ? #185

Open
vic1707 opened this issue Sep 20, 2024 · 5 comments
Open

Add support for fallible sanitizers ? #185

vic1707 opened this issue Sep 20, 2024 · 5 comments

Comments

@vic1707
Copy link
Contributor

vic1707 commented Sep 20, 2024

I have a new type wrapping a std:::fs::PathBuf, I'd like for it to always be an absolute path.
std::fs::canonicalize does exactly that but is fallible (io::Result<Pathbuf>).
Is it feasible to add support for fallible sanitizers? Currently I can work around that limitation by unwraping it but I'm not a fan 😓.

use derive_more::derive::Display;
use nutype::nutype;
use serde::{Deserialize, Serialize};
use std::{fs, io, path::{Path, PathBuf}};

#[nutype(
    derive(
        Debug, Clone, PartialEq, Eq, PartialOrd, Ord,
        Serialize, Deserialize
    ),
    sanitize(with = fs::canonicalize), // Errors since it returns a Result
    validate(with = is_file_and_exists, error = Error)
)]
struct File(PathBuf);

fn is_file_and_exists<P: AsRef<Path>>(path: P) -> Result<(), Error> {
    let path = path.as_ref();
    if !path.exists() {
        return Err(Error::IoError(io::ErrorKind::NotFound.into()));
    }
    if !path.is_file() {
        return Err(Error::NotAFile);
    }
    Ok(())
}

#[derive(Debug, Display, thiserror::Error)]
enum Error {
	#[display("{_0}")]
	IoError(#[from] io::Error),
	#[display("Provided path isn't a file.")]
	NotAFile,
}
@vic1707 vic1707 changed the title Add support for fallible sanitizer ? Add support for fallible sanitizers ? Sep 20, 2024
@greyblake
Copy link
Owner

Uh.
The short answer is no, there is no way to support fallible sanitizers, and I don't think it will be supported.
What is more realistically is to support validators that would return a new value in Ok().

But to be honest, I have never thought of the use case like this before.

@vic1707
Copy link
Contributor Author

vic1707 commented Sep 21, 2024

Understandable 👌

Would you be open to proposals, I'd like to give it a try to see what it would require ? 🙂

(I have no clue if it's even possible I'm just curious 😁)

@greyblake
Copy link
Owner

Would you be open to proposals

I am not sure yet. The problem with that is that it will break the conceptual model that exists at the moment.
It will also will require a lot of code changes and breaking the existing API.

Though, I don't wanna stop you from experimenting if you want to, I just don't feel committed to this at the moment.

@greyblake
Copy link
Owner

Do you have any other potential sanitization functions that could fail?

@vic1707
Copy link
Contributor Author

vic1707 commented Sep 21, 2024

Ok no problem 👌

I don't have any other fallible sanitization function in mind at the moment, if I find some I'll tell you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants