convert-case icon indicating copy to clipboard operation
convert-case copied to clipboard

To Path

Open xenoterracide opened this issue 1 year ago • 2 comments

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

xenoterracide avatar Dec 29 '23 01:12 xenoterracide

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.

rutrum avatar Dec 29 '23 12:12 rutrum

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.

xenoterracide avatar Dec 29 '23 13:12 xenoterracide