Struct hakari::HakariCargoToml [−][src]
pub struct HakariCargoToml { /* fields omitted */ }
Expand description
Support for maintaining Cargo.toml
files that unify features in a workspace.
This struct maintains a context around a Cargo.toml
file. It provides facilities for diffing
the contents of the file, and for writing out new contents.
Structure of the Cargo.toml file
The Cargo.toml
file is treated as partially generated. It is expected to have a section
marked off as, for example:
[package]
...
## BEGIN HAKARI SECTION
[dependencies]
...
[build-dependencies]
...
[dev-dependencies]
...
## END HAKARI SECTION
The part of the Cargo.toml
file between the BEGIN HAKARI SECTION
and END HAKARI SECTION
lines is managed by this struct, and changes to it may not be preserved. The part of the file
outside this section can be edited and its contents will be preserved.
Setting up a new package
For Hakari to manage a package, a bit of initial prep work must be done:
-
Add a new library package in a desired location within your workspace, for example:
cargo new --lib hakari-package
. -
Copy and paste the following lines of code to the end of the package’s
Cargo.toml
file. Be sure to put in a trailing newline.## BEGIN HAKARI SECTION ## END HAKARI SECTION
-
Add an empty
build.rs
file (the exact contents don’t matter, but the presence of this file makes build dependencies work properly).fn main() {}
Implementations
The string "\n### BEGIN HAKARI SECTION\n"
. This string marks the beginning of the
generated section.
The string "\n### END HAKARI SECTION\n"
. This string marks the end of the generated
section.
Creates a new instance of HakariCargoToml
with the Cargo.toml
located at the given path.
Reads the contents of the file off of disk.
If the path is relative, it is evaluated with respect to the current directory.
Returns an error if the file couldn’t be read (other than if the file wasn’t found, which is a case handled by this struct).
pub fn new_relative(
workspace_root: impl Into<Utf8PathBuf>,
crate_dir: impl AsRef<Utf8Path>
) -> Result<Self, CargoTomlError>
pub fn new_relative(
workspace_root: impl Into<Utf8PathBuf>,
crate_dir: impl AsRef<Utf8Path>
) -> Result<Self, CargoTomlError>
Creates a new instance of HakariCargoToml
at the given workspace root and crate
directory. Reads the contents of the file off of disk.
This is a convenience method around appending crate_dir
and Cargo.toml
to
workspace_root
.
If the path is relative, it is evaluated with respect to the current directory.
pub fn new_in_memory(
toml_path: impl Into<Utf8PathBuf>,
contents: String
) -> Result<Self, CargoTomlError>
pub fn new_in_memory(
toml_path: impl Into<Utf8PathBuf>,
contents: String
) -> Result<Self, CargoTomlError>
Creates a new instance of HakariCargoToml
with the given path with the given contents as
read from disk.
This may be useful for test scenarios.
Returns the contents of the file on disk as read at construction time.
Returns the start and end offsets of the part of the file treated as generated.
Returns the part of the file that is treated as generated.
This part of the file will be replaced on write.
Returns true if the contents on disk are different from the provided TOML output.
Computes the diff between the contents on disk and the provided TOML output.
This returns a diffy::Patch
, which can be formatted through methods provided by diffy
.
diffy
is re-exported at the top level of this crate.
Examples
TODO
Writes out the provided TOML to the generated section of the file. The rest of the file is left unmodified.
self
is consumed because the contents of the file are now assumed to be invalid.
Returns true if the contents were different and the file was written out, false if the contents were the same and the file was not written out, and an error if there was an issue while writing the file out.
Writes out the full contents, including the provided TOML, to the given writer.
Writes out the full contents, including the provided TOML, to the given fmt::Write
instance.
std::io::Write
expects bytes to be written to it, so using it with a &mut String
is
inconvenient. This alternative is more convenient, and also works for fmt::Formatter
instances.
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for HakariCargoToml
impl Send for HakariCargoToml
impl Sync for HakariCargoToml
impl Unpin for HakariCargoToml
impl UnwindSafe for HakariCargoToml
Blanket Implementations
Mutably borrows from an owned value. Read more