diff options
Diffstat (limited to 'rust/vendor/build_const')
-rw-r--r-- | rust/vendor/build_const/.cargo-checksum.json | 1 | ||||
-rw-r--r-- | rust/vendor/build_const/Cargo.toml | 26 | ||||
-rw-r--r-- | rust/vendor/build_const/LICENSE.txt | 21 | ||||
-rw-r--r-- | rust/vendor/build_const/README.md | 6 | ||||
-rw-r--r-- | rust/vendor/build_const/src/lib.rs | 88 | ||||
-rw-r--r-- | rust/vendor/build_const/src/writer.rs | 216 |
6 files changed, 358 insertions, 0 deletions
diff --git a/rust/vendor/build_const/.cargo-checksum.json b/rust/vendor/build_const/.cargo-checksum.json new file mode 100644 index 0000000..f6e4325 --- /dev/null +++ b/rust/vendor/build_const/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.toml":"152be142e612a228aca0dc288154a596f47306fed4d1ddf1a806b4534ffb12da","LICENSE.txt":"7a31ff1d8a253fdbf2084fb6854375b1e98722d1a4ec043226c7c2f8c91437c1","README.md":"6e2ed8b0c11176190dcbb0ffd57e9e41ce99057d68b146d9a9f0aeb7439bbf7b","src/lib.rs":"642152d33c8d1680ddcb2b515d16982e86da62fbd6e3d7bc0516416fee027cd4","src/writer.rs":"6ce9ec2f39c0d1e6df9b191d80c5573d5b99129017d786f2b057cf3980bf18de"},"package":"b4ae4235e6dac0694637c763029ecea1a2ec9e4e06ec2729bd21ba4d9c863eb7"}
\ No newline at end of file diff --git a/rust/vendor/build_const/Cargo.toml b/rust/vendor/build_const/Cargo.toml new file mode 100644 index 0000000..cc193e7 --- /dev/null +++ b/rust/vendor/build_const/Cargo.toml @@ -0,0 +1,26 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies +# +# If you believe there's an error in this file please file an +# issue against the rust-lang/cargo repository. If you're +# editing this file be aware that the upstream Cargo.toml +# will likely look very different (and much more reasonable) + +[package] +name = "build_const" +version = "0.2.2" +authors = ["Garrett Berg <vitiral@gmail.com>"] +exclude = ["/justfile"] +description = "library for creating importable constants from build.rs or a script" +documentation = "https://docs.rs/build_const" +keywords = ["embedded", "no_std", "build", "const", "static"] +license = "MIT" +repository = "https://github.com/vitiral/build_const" + +[features] +default = ["std"] +std = [] diff --git a/rust/vendor/build_const/LICENSE.txt b/rust/vendor/build_const/LICENSE.txt new file mode 100644 index 0000000..a3fe395 --- /dev/null +++ b/rust/vendor/build_const/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2017 Garrett Berg, vitiral@gmail.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/rust/vendor/build_const/README.md b/rust/vendor/build_const/README.md new file mode 100644 index 0000000..486eb8d --- /dev/null +++ b/rust/vendor/build_const/README.md @@ -0,0 +1,6 @@ +# `build_const`: crate for creating constants in your build script + +Rust library for creating importable constants from build.rs or a script + +See the [crate documentation](https://docs.rs/build_const) +and the crate on [crates.io](https://crates.io/crates/build_const) diff --git a/rust/vendor/build_const/src/lib.rs b/rust/vendor/build_const/src/lib.rs new file mode 100644 index 0000000..f2158f5 --- /dev/null +++ b/rust/vendor/build_const/src/lib.rs @@ -0,0 +1,88 @@ +//! `build_const`: crate for creating constants in your build script +//! +//! The build_const crate exists to help create rust constant files at compile time or in a +//! generating script. It is ultra simple and lightweight, making constant creation a simple +//! matter. +//! +//! Recommended use: when developing make your constants in `build.rs`. Once your constants are +//! fairly stable create a script instead and have your constants file be generated in either a +//! single file or an external crate that you can bring in as a dependency. +//! +//! # Example +//! +//! Include `build_const = VERSION` in your `Cargo.toml` file. For `no_std` support (macros only) +//! use `default-features = false`. +//! +//! See `ConstWriter` for how to use in a build.rs or script. To then import a "constants.rs" file +//! created in `build.rs` use: +//! +//! ```c +//! #[macro_use] +//! extern crate build_const; +//! +//! build_const!("constants"); +//! println!("VALUE: {}", VALUE); +//! println!("VALUE: {}", ARRAY); +//! ``` +//! +//! For writing constants in a script, the macro `src_file!` is also provided. +//! ```c +//! // will write files to `/src/constants.rs` +//! let mut consts = ConstWriter::from_path(&Path::from(src_file!("constants.rs"))).unwrap(); +//! // ... use consts +//! ``` + +#![cfg_attr(not(feature = "std"), no_std)] + +#[cfg(feature = "std")] +mod writer; + +#[cfg(feature = "std")] +pub use writer::{ + ConstWriter, + ConstValueWriter, + write_array, + write_array_raw, +}; + +/// Shortcut macro which expands to the same module path used in +/// `ConstWriter::for_build(mod_name)`. +/// +/// If you don't want to include macros, this is simply a one liner: +/// ```ignore +/// include!(concat!(env!("OUT_DIR"), concat!("/", $mod_name))); +/// ``` +#[macro_export] +macro_rules! build_const { + ( $mod_name:expr ) => { + include!( + concat!( + env!("OUT_DIR"), + concat!("/", concat!($mod_name, ".rs")) + ) + ); + }; +} + +/// Macro which returns the path to file in your `src/` directory. +/// +/// Example: +/// ```ignore +/// src_file!("constants.rs"); +/// ``` +/// returns `/path/to/project/src/constants.rs` +/// +/// If you need a more custom path, the basic implementation is: +/// ```ignore +/// concat!(env!("CARGO_MANIFEST_DIR"), "/src/path/to/file") +/// ``` +#[macro_export] +macro_rules! src_file { + ( $file_name:expr) => { + concat!( + env!("CARGO_MANIFEST_DIR"), + concat!("/", concat!("src", concat!("/", $file_name))) + ) + }; +} + diff --git a/rust/vendor/build_const/src/writer.rs b/rust/vendor/build_const/src/writer.rs new file mode 100644 index 0000000..c4624b7 --- /dev/null +++ b/rust/vendor/build_const/src/writer.rs @@ -0,0 +1,216 @@ +use std::env; +use std::fs; +use std::fmt::{Debug, Display}; +use std::io; +use std::io::Write; +use std::path::Path; +use std::str; + + +/// Primary object used to write constant files. +/// +/// # Example +/// ```no_run +/// # use std::path::Path; +/// # #[derive(Debug)] +/// # struct Point { x: u8, y: u8 } +/// use build_const::ConstWriter; +/// +/// // use `for_build` in `build.rs` +/// let mut consts = ConstWriter::from_path( +/// &Path::new("/tmp/constants.rs") +/// ).unwrap(); +/// +/// // add an external dependency (`use xyz::Point`) +/// consts.add_dependency("xyz::Point"); +/// +/// // finish dependencies and starting writing constants +/// let mut consts = consts.finish_dependencies(); +/// +/// // add an array of values +/// let values: Vec<u8> = vec![1, 2, 3, 36]; +/// consts.add_array("ARRAY", "u8", &values); +/// +/// // Add a value that is a result of "complex" calculations +/// consts.add_value("VALUE", "u8", values.iter().sum::<u8>()); +/// +/// // Add a value from an external crate (must implement `Debug`) +/// consts.add_value("VALUE", "Point", &Point { x: 3, y: 7}); +/// ``` +pub struct ConstWriter { + f: fs::File, +} + +/// Created from `ConstWriter::finish_dependencies`. See +/// documentation for `ConstWriter`. +pub struct ConstValueWriter { + f: fs::File, +} + +impl ConstWriter { + /// Create a ConstWriter to be used for your crate's `build.rs` + pub fn for_build(mod_name: &str) -> io::Result<ConstWriter> { + let out_dir = env::var("OUT_DIR").unwrap(); + let mod_name = format!("{}.rs", mod_name); + let dest_path = Path::new(&out_dir).join(mod_name); + + Ok(ConstWriter { + f: fs::File::create(&dest_path)? + }) + } + + /// Create a new ConstWriter to write to an path. If a file + /// already exists at the path then it will be deleted. + pub fn from_path(path: &Path) -> io::Result<ConstWriter> { + let f = fs::OpenOptions::new() + .write(true) + .truncate(true) + .open(path)?; + Ok(ConstWriter { + f: f, + }) + } + + /// finish writing dependencies and start writing constants + pub fn finish_dependencies(self) -> ConstValueWriter { + ConstValueWriter { f: self.f } + } + + /// Add a dependency to your constants file. + pub fn add_dependency(&mut self, lib: &str) { + write!(self.f, "pub use {};\n", lib).unwrap(); + } + + /// Add a raw string to the constants file. + /// + /// This method only changes `raw` by adding a `\n` at the end. + pub fn add_raw(&mut self, raw: &str) { + write!(self.f, "{}\n", raw).unwrap(); + } + +} + +impl ConstValueWriter { + /// Add a value to the constants file. + /// + /// You have to manually specify the `name`, type (`ty`) and `value` + /// of the constant you want to add. + /// + /// The `value` uses the `Debug` trait to determine the formating of + /// the value being added. If `Debug` is not accurate or will not work, + /// you must use `add_value_raw` instead and format it yourself. + pub fn add_value<T: Debug>(&mut self, name: &str, ty: &str, value: T) { + self.add_value_raw(name, ty, &format!("{:?}", value)); + } + + /// Add a pre-formatted value to the constants file. + /// + /// `add_value` depends on `Debug` being implemented in such a way + /// that it accurately represents the type's creation. Sometimes that + /// cannot be relied on and `add_value_raw` has to be used instead. + pub fn add_value_raw(&mut self, name: &str, ty: &str, raw_value: &str) { + write!( + self.f, "pub const {}: {} = {};\n", + name, + ty, + raw_value, + ).unwrap(); + } + + /// Add an array of len > 0 to the constants + /// + /// You have to manually specify the `name`, type (`ty`) of the **items** and + /// `values` of the array constant you want to add. The length of the array + /// is determined automatically. + /// + /// Example: `const.add_array("foo", "u16", &[1,2,3])` + /// + /// The `value` of each item uses the `Debug` trait to determine the + /// formatting of the value being added. If `Debug` is not accurate or will + /// not work, you must use `add_array_raw` instead and format it yourself. + pub fn add_array<T: Debug>(&mut self, name: &str, ty: &str, values: &[T]) { + write_array(&mut self.f, name, ty, values); + } + + /// Add an array of pre-formatted values to the constants file. The length of the array is + /// determined automatically. + /// + /// `add_array` depends on `Debug` being implemented for each item in such a way that it + /// accurately represents the item's creation. Sometimes that cannot be relied on and + /// `add_array_raw` has to be used instead. + pub fn add_array_raw<S: AsRef<str> + Display>(&mut self, name: &str, ty: &str, raw_values: &[S]) { + write_array_raw(&mut self.f, name, ty, raw_values); + } + + /// Add a raw string to the constants file. + /// + /// This method only changes `raw` by adding a `\n` at the end. + pub fn add_raw(&mut self, raw: &str) { + write!(self.f, "{}\n", raw).unwrap(); + } + + /// Finish writing to the constants file and consume self. + pub fn finish(&mut self) { + self.f.flush().unwrap(); + } + +} + +// Public Functions + +/// Write an array and return the array's full type representation. +/// +/// This can be used to create nested array constant types. +pub fn write_array<T: Debug, W: Write>(w: &mut W, name: &str, ty: &str, values: &[T]) + -> String +{ + assert!( + !values.is_empty(), + "attempting to add an array of len zero. If this is intentional, use \ + add_value_raw instead." + ); + let full_ty = write_array_header(w, name, ty, values.len()); + for v in values.iter() { + write_array_item_raw(w, &format!("{:?}", v)); + } + write_array_end(w); + full_ty +} + +/// Write an array of raw values and return the array's full type representation. +/// +/// This can be used to create nested array constant types. +pub fn write_array_raw<W: Write, S: AsRef<str> + Display>( + w: &mut W, name: &str, ty: &str, raw_values: &[S] + ) + -> String +{ + assert!( + !raw_values.is_empty(), + "attempting to add an array of len zero. If this is intentional, use \ + add_value_raw instead." + ); + let full_ty = write_array_header(w, name, ty, raw_values.len()); + for v in raw_values { + write_array_item_raw(w, v); + } + write_array_end(w); + full_ty +} + +// Helpers + +/// Write the array header and return the array's full type. +fn write_array_header<W: Write>(w: &mut W, name: &str, ty: &str, len: usize) -> String { + let full_ty = format!("[{}; {}]", ty, len); + write!(w, "pub const {}: {} = [\n", name, &full_ty).unwrap(); + full_ty +} + +fn write_array_item_raw<W: Write, S: AsRef<str> + Display>(w: &mut W, raw_item: S) { + write!(w, " {},\n", raw_item).unwrap() +} + +fn write_array_end<W: Write>(w: &mut W) { + write!(w, "];\n").unwrap(); +} |