Referencing files in a dependency

We have a bunch of utility functions for generating web pages. These are paired with a few javascripts and css files. Our web application servers can be built either to use these static files in the filesystem, or they can be built into the executable.

The way we currently use our "mini framework" is to copy all the files into the relevant crate. As one can imagine, the maintenance burden is substantial when updates to the framework need to be made (as the changes need to be manually merged into all the application projects).

I'd like to find a way to move all the framework stuff into a crate of its own, and then use cargo to make the applications depend on the framework crate. This is trivial with regards to the rust code, but the issue is the static content.

This leaves the question if it's possible to refer to a dependency's source tree (so the application can access the utility crate's javascript and css files)?

Like I noted above, in some build types the static files are included in the binary using include_bytes!(), so I would need to construct something along the line of:

const MAIN_JS: &'static [u8] = include_bytes!(path_to_main_js_in_dependency_source_tree);

Are there any other common patterns to achieve this sort of thing? It feels a little icky referencing other crates in this way because it feels like I'm violating some crate source isolation, but it's becoming a major maintenance burden not to have the framework properly versioned, and I would prefer if it was possible to get this done using cargo itself, rather than write a custom builder which wraps around it.

Basically I'd like to be able to do this in Cargo.toml:

miniframework = { git = "", branch = "0.23.0" }

.. and the application would be able to fetch all the relevant files using some variables set up by cargo itself.

I don't think I'm opposed to having the miniframework crate copy files somewhere else for the application crate to find, but if that level of indirection could be avoided it would be nice.

I would make sure all my application code is within a single directory tree then use the include_dir crate to embed the assets in your final binary.

Git submodules could be helpful if the mini framework is in a different repository.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.