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

To Path #17

Open
xenoterracide opened this issue Dec 29, 2023 · 2 comments
Open

To Path #17

xenoterracide opened this issue Dec 29, 2023 · 2 comments

Comments

@xenoterracide
Copy link

ok, I hardly know rust at all

#[derive(Clone, Copy)]
pub struct ToJavaPackagePathHelper;

impl HelperDef for ToJavaPackagePathHelper {
    fn call<'reg: 'rc, 'rc>(
        &self,
        h: &Helper,
        _: &Handlebars,
        _: &Context,
        _rc: &mut RenderContext,
        out: &mut dyn Output,
    ) -> HelperResult {
        let param = h.param(0).ok_or(RenderError::new(
            "this function requires an argument to process",
        ))?;
        let rendered = param.value().render();

        let conv = Converter::new()
            .set_pattern(Pattern::Camel)
            .remove_boundaries(&[
                Boundary::UpperDigit,
                Boundary::LowerDigit,
                Boundary::DigitUpper,
                Boundary::DigitLower,
            ])
            .set_delim("/");

        let path = &conv.convert(rendered).to_lowercase();
        debug!("path: '{}'", path  );

        out.write(path)?;
        Ok(())
    }
}

the goal is (hopefully) to obviously convert a java package to a path. java packages (by convention) are all prefixed with a reverse domain. So my domain com.xenoterracide input, should end up as com/xenoterracide which will then be passed to mkdir, this sadly doesn't seem to work. If I don't input a dot it usually seems to work as expected. Probably just going to do a . replace, but seems like maybe a good feature (PathCase), but in general not the behavior I would expect from Pattern::Camel.

https://github.com/xenoterracide/brix/blob/main/crates/brix_processor/src/helpers/basic.rs#L119

@rutrum
Copy link
Owner

rutrum commented Dec 29, 2023

At this time there is, unfortunately, no way to manually split text based on a character that isn't space, hyphen, or underscore. You can produce text with whatever delimiter you want, as you've done here. This is a good feature idea. It will require extending boundary to include an option like "character(char)", which can include space, hyphen, underscore, periods, whatever. If this were the case, then you could create a converter, set the boundaries to include exactly the dot character, and then convert by delimiting with slash.

For now, my best suggestion is if you are just splitting on dots as you would in a domain, then don't use my library for that part.

let path = rendered.split(".").collect::<Vec<&str>>().join("/");

When you set the pattern with converter, it describes how the text at the end will be formatted. By setting the pattern to Camel, you would get com/Xenoterracide and not com/xenoterracide. I see you manually make it lowercase after converting. You can just use the Pattern::Lower pattern instead.

I would like to keep this issue open and close when the feature to split on any character has been fully investigated.

@xenoterracide
Copy link
Author

xenoterracide commented Dec 29, 2023

I'm doing a lot more than splitting on dots. _ - etc, brix is a generic scaffolding tool. Splitting on a . was just where I hit the wall.

I think I didn't use the pattern to lowercase because it didn't split the words where I wanted it to or something. I could give it a go again just to see why. Probably had something to do with either where numbers split or where words disappeared... Although, I might be missing exactly what you're saying there.

You might want to consider being able to do multiple characters. I know Perl5 (which I don't do anymore), uses :: for its namespaces.

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