Struct hakari::HakariCargoToml

source ·
pub struct HakariCargoToml { /* private fields */ }
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:

  1. Add a new library package in a desired location within your workspace, for example: cargo new --lib hakari-package.

  2. 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
    
    
  3. 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).

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.

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 toml path provided at construction time.

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§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Set the foreground color generically Read more
Set the background color generically. Read more
Change the foreground color to black
Change the background color to black
Change the foreground color to red
Change the background color to red
Change the foreground color to green
Change the background color to green
Change the foreground color to yellow
Change the background color to yellow
Change the foreground color to blue
Change the background color to blue
Change the foreground color to magenta
Change the background color to magenta
Change the foreground color to purple
Change the background color to purple
Change the foreground color to cyan
Change the background color to cyan
Change the foreground color to white
Change the background color to white
Change the foreground color to the terminal default
Change the background color to the terminal default
Change the foreground color to bright black
Change the background color to bright black
Change the foreground color to bright red
Change the background color to bright red
Change the foreground color to bright green
Change the background color to bright green
Change the foreground color to bright yellow
Change the background color to bright yellow
Change the foreground color to bright blue
Change the background color to bright blue
Change the foreground color to bright magenta
Change the background color to bright magenta
Change the foreground color to bright purple
Change the background color to bright purple
Change the foreground color to bright cyan
Change the background color to bright cyan
Change the foreground color to bright white
Change the background color to bright white
Make the text bold
Make the text dim
Make the text italicized
Make the text italicized
Make the text blink
Make the text blink (but fast!)
Swap the foreground and background colors
Hide the text
Cross out the text
Set the foreground color at runtime. Only use if you do not know which color will be used at compile-time. If the color is constant, use either OwoColorize::fg or a color-specific method, such as OwoColorize::green, Read more
Set the background color at runtime. Only use if you do not know what color to use at compile-time. If the color is constant, use either OwoColorize::bg or a color-specific method, such as OwoColorize::on_yellow, Read more
Set the foreground color to a specific RGB value.
Set the background color to a specific RGB value.
Sets the foreground color to an RGB value.
Sets the background color to an RGB value.
Apply a runtime-determined style
The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.