gix-config-value-0.14.4/.cargo_vcs_info.json0000644000000001560000000000100142700ustar { "git": { "sha1": "eb6aa8f502314f886fc4ea3d52ab220763968208" }, "path_in_vcs": "gix-config-value" }gix-config-value-0.14.4/Cargo.toml0000644000000026410000000000100122670ustar # 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 are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" rust-version = "1.65" name = "gix-config-value" version = "0.14.4" authors = ["Sebastian Thiel "] include = [ "src/**/*", "LICENSE-*", ] description = "A crate of the gitoxide project providing git-config value parsing" license = "MIT OR Apache-2.0" repository = "https://github.com/Byron/gitoxide" [package.metadata.docs.rs] all-features = true features = ["document-features"] [lib] doctest = false [dependencies.bitflags] version = "2" [dependencies.bstr] version = "1.0.1" features = ["std"] default-features = false [dependencies.document-features] version = "0.2.0" optional = true [dependencies.gix-path] version = "^0.10.4" [dependencies.serde] version = "1.0.114" features = ["derive"] optional = true default-features = false [dependencies.thiserror] version = "1.0.32" [features] serde = [ "dep:serde", "bstr/serde", ] [target."cfg(not(windows))".dependencies.libc] version = "0.2" gix-config-value-0.14.4/Cargo.toml.orig000064400000000000000000000017171046102023000157530ustar 00000000000000[package] name = "gix-config-value" version = "0.14.4" repository = "https://github.com/Byron/gitoxide" license = "MIT OR Apache-2.0" description = "A crate of the gitoxide project providing git-config value parsing" authors = ["Sebastian Thiel "] edition = "2021" rust-version = "1.65" include = ["src/**/*", "LICENSE-*"] [lib] doctest = false [features] ## Data structures implement `serde::Serialize` and `serde::Deserialize`. serde = ["dep:serde", "bstr/serde"] [dependencies] gix-path = { version = "^0.10.4", path = "../gix-path" } thiserror = "1.0.32" bstr = { version = "1.0.1", default-features = false, features = ["std"] } serde = { version = "1.0.114", optional = true, default-features = false, features = ["derive"]} bitflags = "2" document-features = { version = "0.2.0", optional = true } [target.'cfg(not(windows))'.dependencies] libc = "0.2" [package.metadata.docs.rs] all-features = true features = ["document-features"] gix-config-value-0.14.4/LICENSE-APACHE000064400000000000000000000247461046102023000150170ustar 00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. gix-config-value-0.14.4/LICENSE-MIT000064400000000000000000000017771046102023000145260ustar 00000000000000Permission 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. gix-config-value-0.14.4/src/boolean.rs000064400000000000000000000054301046102023000156340ustar 00000000000000use std::{borrow::Cow, convert::TryFrom, ffi::OsString, fmt::Display}; use bstr::{BStr, BString, ByteSlice}; use crate::{Boolean, Error}; fn bool_err(input: impl Into) -> Error { Error::new( "Booleans need to be 'no', 'off', 'false', '' or 'yes', 'on', 'true' or any number", input, ) } impl TryFrom for Boolean { type Error = Error; fn try_from(value: OsString) -> Result { let value = gix_path::os_str_into_bstr(&value) .map_err(|_| Error::new("Illformed UTF-8", std::path::Path::new(&value).display().to_string()))?; Self::try_from(value) } } /// # Warning /// /// The direct usage of `try_from("string")` is discouraged as it will produce the wrong result for values /// obtained from `core.bool-implicit-true`, which have no separator and are implicitly true. /// This method chooses to work correctly for `core.bool-empty=`, which is an empty string and resolves /// to being `false`. /// /// Instead of this, obtain booleans with `config.boolean(…)`, which handles the case were no separator is /// present correctly. impl TryFrom<&BStr> for Boolean { type Error = Error; fn try_from(value: &BStr) -> Result { if parse_true(value) { Ok(Boolean(true)) } else if parse_false(value) { Ok(Boolean(false)) } else { use std::str::FromStr; if let Some(integer) = value.to_str().ok().and_then(|s| i64::from_str(s).ok()) { Ok(Boolean(integer != 0)) } else { Err(bool_err(value)) } } } } impl Boolean { /// Return true if the boolean is a true value. /// /// Note that the inner value is accessible directly as well. pub fn is_true(self) -> bool { self.0 } } impl TryFrom> for Boolean { type Error = Error; fn try_from(c: Cow<'_, BStr>) -> Result { Self::try_from(c.as_ref()) } } impl Display for Boolean { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.0.fmt(f) } } impl From for bool { fn from(b: Boolean) -> Self { b.0 } } #[cfg(feature = "serde")] impl serde::Serialize for Boolean { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, { serializer.serialize_bool(self.0) } } fn parse_true(value: &BStr) -> bool { value.eq_ignore_ascii_case(b"yes") || value.eq_ignore_ascii_case(b"on") || value.eq_ignore_ascii_case(b"true") } fn parse_false(value: &BStr) -> bool { value.eq_ignore_ascii_case(b"no") || value.eq_ignore_ascii_case(b"off") || value.eq_ignore_ascii_case(b"false") || value.is_empty() } gix-config-value-0.14.4/src/color.rs000064400000000000000000000262741046102023000153440ustar 00000000000000#![allow(missing_docs)] use std::{borrow::Cow, convert::TryFrom, fmt::Display, str::FromStr}; use bstr::{BStr, BString}; use crate::{Color, Error}; impl Display for Color { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let mut write_space = None; if let Some(fg) = self.foreground { fg.fmt(f)?; write_space = Some(()); } if let Some(bg) = self.background { if write_space.take().is_some() { write!(f, " ")?; } bg.fmt(f)?; write_space = Some(()) } if !self.attributes.is_empty() { if write_space.take().is_some() { write!(f, " ")?; } self.attributes.fmt(f)?; } Ok(()) } } fn color_err(input: impl Into) -> Error { Error::new( "Colors are specific color values and their attributes, like 'brightred', or 'blue'", input, ) } impl TryFrom<&BStr> for Color { type Error = Error; fn try_from(s: &BStr) -> Result { let s = std::str::from_utf8(s).map_err(|err| color_err(s).with_err(err))?; enum ColorItem { Value(Name), Attr(Attribute), } let items = s.split_whitespace().filter_map(|s| { if s.is_empty() { return None; } Some( Name::from_str(s) .map(ColorItem::Value) .or_else(|_| Attribute::from_str(s).map(ColorItem::Attr)), ) }); let mut foreground = None; let mut background = None; let mut attributes = Attribute::empty(); for item in items { match item { Ok(item) => match item { ColorItem::Value(v) => { if foreground.is_none() { foreground = Some(v); } else if background.is_none() { background = Some(v); } else { return Err(color_err(s)); } } ColorItem::Attr(a) => attributes |= a, }, Err(_) => return Err(color_err(s)), } } Ok(Color { foreground, background, attributes, }) } } impl TryFrom> for Color { type Error = Error; fn try_from(c: Cow<'_, BStr>) -> Result { Self::try_from(c.as_ref()) } } /// Discriminating enum for names of [`Color`] values. /// /// `git-config` supports the eight standard colors, their bright variants, an /// ANSI color code, or a 24-bit hex value prefixed with an octothorpe/hash. #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] #[allow(missing_docs)] pub enum Name { Normal, Default, Black, BrightBlack, Red, BrightRed, Green, BrightGreen, Yellow, BrightYellow, Blue, BrightBlue, Magenta, BrightMagenta, Cyan, BrightCyan, White, BrightWhite, Ansi(u8), Rgb(u8, u8, u8), } impl Display for Name { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::Normal => write!(f, "normal"), Self::Default => write!(f, "default"), Self::Black => write!(f, "black"), Self::BrightBlack => write!(f, "brightblack"), Self::Red => write!(f, "red"), Self::BrightRed => write!(f, "brightred"), Self::Green => write!(f, "green"), Self::BrightGreen => write!(f, "brightgreen"), Self::Yellow => write!(f, "yellow"), Self::BrightYellow => write!(f, "brightyellow"), Self::Blue => write!(f, "blue"), Self::BrightBlue => write!(f, "brightblue"), Self::Magenta => write!(f, "magenta"), Self::BrightMagenta => write!(f, "brightmagenta"), Self::Cyan => write!(f, "cyan"), Self::BrightCyan => write!(f, "brightcyan"), Self::White => write!(f, "white"), Self::BrightWhite => write!(f, "brightwhite"), Self::Ansi(num) => num.fmt(f), Self::Rgb(r, g, b) => write!(f, "#{r:02x}{g:02x}{b:02x}"), } } } #[cfg(feature = "serde")] impl serde::Serialize for Name { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, { serializer.serialize_str(&self.to_string()) } } impl FromStr for Name { type Err = Error; fn from_str(mut s: &str) -> Result { let bright = if let Some(rest) = s.strip_prefix("bright") { s = rest; true } else { false }; match s { "normal" if !bright => return Ok(Self::Normal), "-1" if !bright => return Ok(Self::Normal), "normal" if bright => return Err(color_err(s)), "default" if !bright => return Ok(Self::Default), "default" if bright => return Err(color_err(s)), "black" if !bright => return Ok(Self::Black), "black" if bright => return Ok(Self::BrightBlack), "red" if !bright => return Ok(Self::Red), "red" if bright => return Ok(Self::BrightRed), "green" if !bright => return Ok(Self::Green), "green" if bright => return Ok(Self::BrightGreen), "yellow" if !bright => return Ok(Self::Yellow), "yellow" if bright => return Ok(Self::BrightYellow), "blue" if !bright => return Ok(Self::Blue), "blue" if bright => return Ok(Self::BrightBlue), "magenta" if !bright => return Ok(Self::Magenta), "magenta" if bright => return Ok(Self::BrightMagenta), "cyan" if !bright => return Ok(Self::Cyan), "cyan" if bright => return Ok(Self::BrightCyan), "white" if !bright => return Ok(Self::White), "white" if bright => return Ok(Self::BrightWhite), _ => (), } if let Ok(v) = u8::from_str(s) { return Ok(Self::Ansi(v)); } if let Some(s) = s.strip_prefix('#') { if s.len() == 6 && s.is_char_boundary(2) && s.is_char_boundary(4) && s.is_char_boundary(6) { let rgb = ( u8::from_str_radix(&s[..2], 16), u8::from_str_radix(&s[2..4], 16), u8::from_str_radix(&s[4..], 16), ); if let (Ok(r), Ok(g), Ok(b)) = rgb { return Ok(Self::Rgb(r, g, b)); } } } Err(color_err(s)) } } impl TryFrom<&BStr> for Name { type Error = Error; fn try_from(s: &BStr) -> Result { Self::from_str(std::str::from_utf8(s).map_err(|err| color_err(s).with_err(err))?) } } bitflags::bitflags! { /// Discriminating enum for [`Color`] attributes. /// /// `git-config` supports modifiers and their negators. The negating color /// attributes are equivalent to having a `no` or `no-` prefix to the normal /// variant. #[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] pub struct Attribute: u32 { const BOLD = 1 << 1; const DIM = 1 << 2; const ITALIC = 1 << 3; const UL = 1 << 4; const BLINK = 1 << 5; const REVERSE = 1 << 6; const STRIKE = 1 << 7; /// Reset is special as we have to be able to parse it, without git actually doing anything with it const RESET = 1 << 8; const NO_DIM = 1 << 21; const NO_BOLD = 1 << 22; const NO_ITALIC = 1 << 23; const NO_UL = 1 << 24; const NO_BLINK = 1 << 25; const NO_REVERSE = 1 << 26; const NO_STRIKE = 1 << 27; } } impl Display for Attribute { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let mut write_space = None; for bit in 1..std::mem::size_of::() * 8 { let attr = match Attribute::from_bits(1 << bit) { Some(attr) => attr, None => continue, }; if self.contains(attr) { if write_space.take().is_some() { write!(f, " ")? } match attr { Attribute::RESET => write!(f, "reset"), Attribute::BOLD => write!(f, "bold"), Attribute::NO_BOLD => write!(f, "nobold"), Attribute::DIM => write!(f, "dim"), Attribute::NO_DIM => write!(f, "nodim"), Attribute::UL => write!(f, "ul"), Attribute::NO_UL => write!(f, "noul"), Attribute::BLINK => write!(f, "blink"), Attribute::NO_BLINK => write!(f, "noblink"), Attribute::REVERSE => write!(f, "reverse"), Attribute::NO_REVERSE => write!(f, "noreverse"), Attribute::ITALIC => write!(f, "italic"), Attribute::NO_ITALIC => write!(f, "noitalic"), Attribute::STRIKE => write!(f, "strike"), Attribute::NO_STRIKE => write!(f, "nostrike"), _ => unreachable!("BUG: add new attribute flag"), }?; write_space = Some(()); } } Ok(()) } } #[cfg(feature = "serde")] impl serde::Serialize for Attribute { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, { serializer.serialize_str(&self.to_string()) } } impl FromStr for Attribute { type Err = Error; fn from_str(mut s: &str) -> Result { let inverted = if let Some(rest) = s.strip_prefix("no-").or_else(|| s.strip_prefix("no")) { s = rest; true } else { false }; match s { "reset" if !inverted => Ok(Attribute::RESET), "reset" if inverted => Err(color_err(s)), "bold" if !inverted => Ok(Attribute::BOLD), "bold" if inverted => Ok(Attribute::NO_BOLD), "dim" if !inverted => Ok(Attribute::DIM), "dim" if inverted => Ok(Attribute::NO_DIM), "ul" if !inverted => Ok(Attribute::UL), "ul" if inverted => Ok(Attribute::NO_UL), "blink" if !inverted => Ok(Attribute::BLINK), "blink" if inverted => Ok(Attribute::NO_BLINK), "reverse" if !inverted => Ok(Attribute::REVERSE), "reverse" if inverted => Ok(Attribute::NO_REVERSE), "italic" if !inverted => Ok(Attribute::ITALIC), "italic" if inverted => Ok(Attribute::NO_ITALIC), "strike" if !inverted => Ok(Attribute::STRIKE), "strike" if inverted => Ok(Attribute::NO_STRIKE), _ => Err(color_err(s)), } } } impl TryFrom<&BStr> for Attribute { type Error = Error; fn try_from(s: &BStr) -> Result { Self::from_str(std::str::from_utf8(s).map_err(|err| color_err(s).with_err(err))?) } } gix-config-value-0.14.4/src/integer.rs000064400000000000000000000102641046102023000156530ustar 00000000000000use std::{borrow::Cow, convert::TryFrom, fmt::Display, str::FromStr}; use bstr::{BStr, BString}; use crate::{Error, Integer}; impl Integer { /// Canonicalize values as simple decimal numbers. /// An optional suffix of k, m, or g (case-insensitive), will cause the /// value to be multiplied by 1024 (k), 1048576 (m), or 1073741824 (g) respectively. /// /// Returns the result if there is no multiplication overflow. pub fn to_decimal(&self) -> Option { match self.suffix { None => Some(self.value), Some(suffix) => match suffix { Suffix::Kibi => self.value.checked_mul(1024), Suffix::Mebi => self.value.checked_mul(1024 * 1024), Suffix::Gibi => self.value.checked_mul(1024 * 1024 * 1024), }, } } } impl Display for Integer { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.value)?; if let Some(suffix) = self.suffix { write!(f, "{suffix}") } else { Ok(()) } } } #[cfg(feature = "serde")] impl serde::Serialize for Integer { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, { if let Some(suffix) = self.suffix { serializer.serialize_i64(self.value << suffix.bitwise_offset()) } else { serializer.serialize_i64(self.value) } } } fn int_err(input: impl Into) -> Error { Error::new( "Integers needs to be positive or negative numbers which may have a suffix like 1k, 42, or 50G", input, ) } impl TryFrom<&BStr> for Integer { type Error = Error; fn try_from(s: &BStr) -> Result { let s = std::str::from_utf8(s).map_err(|err| int_err(s).with_err(err))?; if let Ok(value) = s.parse() { return Ok(Self { value, suffix: None }); } if s.len() <= 1 { return Err(int_err(s)); } let last_idx = s.len() - 1; if !s.is_char_boundary(last_idx) { return Err(int_err(s)); } let (number, suffix) = s.split_at(s.len() - 1); if let (Ok(value), Ok(suffix)) = (number.parse(), suffix.parse()) { Ok(Self { value, suffix: Some(suffix), }) } else { Err(int_err(s)) } } } impl TryFrom> for Integer { type Error = Error; fn try_from(c: Cow<'_, BStr>) -> Result { Self::try_from(c.as_ref()) } } /// Integer suffixes that are supported by `git-config`. /// /// These values are base-2 unit of measurements, not the base-10 variants. #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] #[allow(missing_docs)] pub enum Suffix { Kibi, Mebi, Gibi, } impl Suffix { /// Returns the number of bits that the suffix shifts left by. #[must_use] pub const fn bitwise_offset(self) -> usize { match self { Self::Kibi => 10, Self::Mebi => 20, Self::Gibi => 30, } } } impl Display for Suffix { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::Kibi => write!(f, "k"), Self::Mebi => write!(f, "m"), Self::Gibi => write!(f, "g"), } } } #[cfg(feature = "serde")] impl serde::Serialize for Suffix { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, { serializer.serialize_str(match self { Self::Kibi => "k", Self::Mebi => "m", Self::Gibi => "g", }) } } impl FromStr for Suffix { type Err = (); fn from_str(s: &str) -> Result { match s { "k" | "K" => Ok(Self::Kibi), "m" | "M" => Ok(Self::Mebi), "g" | "G" => Ok(Self::Gibi), _ => Err(()), } } } impl TryFrom<&BStr> for Suffix { type Error = (); fn try_from(s: &BStr) -> Result { Self::from_str(std::str::from_utf8(s).map_err(|_| ())?) } } gix-config-value-0.14.4/src/lib.rs000064400000000000000000000024531046102023000147650ustar 00000000000000//! Parsing for data types used in `git-config` files to allow their use from environment variables and other sources. //! //! ## Feature Flags #![cfg_attr( all(doc, feature = "document-features"), doc = ::document_features::document_features!() )] #![cfg_attr(all(doc, feature = "document-features"), feature(doc_cfg, doc_auto_cfg))] #![deny(missing_docs, rust_2018_idioms, unsafe_code)] /// The error returned when any config value couldn't be instantiated due to malformed input. #[derive(Debug, thiserror::Error, Eq, PartialEq)] #[allow(missing_docs)] #[error("Could not decode '{input}': {message}")] pub struct Error { pub message: &'static str, pub input: bstr::BString, #[source] pub utf8_err: Option, } impl Error { /// Create a new value error from `message`, with `input` being what's causing the error. pub fn new(message: &'static str, input: impl Into) -> Self { Error { message, input: input.into(), utf8_err: None, } } pub(crate) fn with_err(mut self, err: std::str::Utf8Error) -> Self { self.utf8_err = Some(err); self } } mod boolean; /// pub mod color; /// pub mod integer; /// pub mod path; mod types; pub use types::{Boolean, Color, Integer, Path}; gix-config-value-0.14.4/src/path.rs000064400000000000000000000171521046102023000151550ustar 00000000000000use std::{borrow::Cow, path::PathBuf}; use bstr::BStr; use crate::Path; /// pub mod interpolate { use std::path::PathBuf; /// Options for interpolating paths with [`Path::interpolate()`][crate::Path::interpolate()]. #[derive(Clone, Copy)] pub struct Context<'a> { /// The location where gitoxide or git is installed. If `None`, `%(prefix)` in paths will cause an error. pub git_install_dir: Option<&'a std::path::Path>, /// The home directory of the current user. If `None`, `~/` in paths will cause an error. pub home_dir: Option<&'a std::path::Path>, /// A function returning the home directory of a given user. If `None`, `~name/` in paths will cause an error. pub home_for_user: Option Option>, } impl Default for Context<'_> { fn default() -> Self { Context { git_install_dir: None, home_dir: None, home_for_user: Some(home_for_user), } } } /// The error returned by [`Path::interpolate()`][crate::Path::interpolate()]. #[derive(Debug, thiserror::Error)] #[allow(missing_docs)] pub enum Error { #[error("{} is missing", .what)] Missing { what: &'static str }, #[error("Ill-formed UTF-8 in {}", .what)] Utf8Conversion { what: &'static str, #[source] err: gix_path::Utf8Error, }, #[error("Ill-formed UTF-8 in username")] UsernameConversion(#[from] std::str::Utf8Error), #[error("User interpolation is not available on this platform")] UserInterpolationUnsupported, } /// Obtain the home directory for the given user `name` or return `None` if the user wasn't found /// or any other error occurred. /// It can be used as `home_for_user` parameter in [`Path::interpolate()`][crate::Path::interpolate()]. #[cfg_attr(windows, allow(unused_variables))] pub fn home_for_user(name: &str) -> Option { #[cfg(not(any(target_os = "android", target_os = "windows")))] { let cname = std::ffi::CString::new(name).ok()?; // SAFETY: calling this in a threaded program that modifies the pw database is not actually safe. // TODO: use the `*_r` version, but it's much harder to use. #[allow(unsafe_code)] let pwd = unsafe { libc::getpwnam(cname.as_ptr()) }; if pwd.is_null() { None } else { use std::os::unix::ffi::OsStrExt; // SAFETY: pw_dir is a cstr and it lives as long as… well, we hope nobody changes the pw database while we are at it // from another thread. Otherwise it lives long enough. #[allow(unsafe_code)] let cstr = unsafe { std::ffi::CStr::from_ptr((*pwd).pw_dir) }; Some(std::ffi::OsStr::from_bytes(cstr.to_bytes()).into()) } } #[cfg(any(target_os = "android", target_os = "windows"))] { None } } } impl<'a> std::ops::Deref for Path<'a> { type Target = BStr; fn deref(&self) -> &Self::Target { self.value.as_ref() } } impl<'a> AsRef<[u8]> for Path<'a> { fn as_ref(&self) -> &[u8] { self.value.as_ref() } } impl<'a> AsRef for Path<'a> { fn as_ref(&self) -> &BStr { self.value.as_ref() } } impl<'a> From> for Path<'a> { fn from(value: Cow<'a, BStr>) -> Self { Path { value } } } impl<'a> Path<'a> { /// Interpolates this path into a path usable on the file system. /// /// If this path starts with `~/` or `~user/` or `%(prefix)/` /// - `~/` is expanded to the value of `home_dir`. The caller can use the [dirs](https://crates.io/crates/dirs) crate to obtain it. /// If it is required but not set, an error is produced. /// - `~user/` to the specified user’s home directory, e.g `~alice` might get expanded to `/home/alice` on linux, but requires /// the `home_for_user` function to be provided. /// The interpolation uses `getpwnam` sys call and is therefore not available on windows. /// - `%(prefix)/` is expanded to the location where `gitoxide` is installed. /// This location is not known at compile time and therefore need to be /// optionally provided by the caller through `git_install_dir`. /// /// Any other, non-empty path value is returned unchanged and error is returned in case of an empty path value or if required input /// wasn't provided. pub fn interpolate( self, interpolate::Context { git_install_dir, home_dir, home_for_user, }: interpolate::Context<'_>, ) -> Result, interpolate::Error> { if self.is_empty() { return Err(interpolate::Error::Missing { what: "path" }); } const PREFIX: &[u8] = b"%(prefix)/"; const USER_HOME: &[u8] = b"~/"; if self.starts_with(PREFIX) { let git_install_dir = git_install_dir.ok_or(interpolate::Error::Missing { what: "git install dir", })?; let (_prefix, path_without_trailing_slash) = self.split_at(PREFIX.len()); let path_without_trailing_slash = gix_path::try_from_bstring(path_without_trailing_slash).map_err(|err| { interpolate::Error::Utf8Conversion { what: "path past %(prefix)", err, } })?; Ok(git_install_dir.join(path_without_trailing_slash).into()) } else if self.starts_with(USER_HOME) { let home_path = home_dir.ok_or(interpolate::Error::Missing { what: "home dir" })?; let (_prefix, val) = self.split_at(USER_HOME.len()); let val = gix_path::try_from_byte_slice(val).map_err(|err| interpolate::Error::Utf8Conversion { what: "path past ~/", err, })?; Ok(home_path.join(val).into()) } else if self.starts_with(b"~") && self.contains(&b'/') { self.interpolate_user(home_for_user.ok_or(interpolate::Error::Missing { what: "home for user lookup", })?) } else { Ok(gix_path::from_bstr(self.value)) } } #[cfg(any(target_os = "windows", target_os = "android"))] fn interpolate_user( self, _home_for_user: fn(&str) -> Option, ) -> Result, interpolate::Error> { Err(interpolate::Error::UserInterpolationUnsupported) } #[cfg(not(any(target_os = "windows", target_os = "android")))] fn interpolate_user( self, home_for_user: fn(&str) -> Option, ) -> Result, interpolate::Error> { let (_prefix, val) = self.split_at("/".len()); let i = val .iter() .position(|&e| e == b'/') .ok_or(interpolate::Error::Missing { what: "/" })?; let (username, path_with_leading_slash) = val.split_at(i); let username = std::str::from_utf8(username)?; let home = home_for_user(username).ok_or(interpolate::Error::Missing { what: "pwd user info" })?; let path_past_user_prefix = gix_path::try_from_byte_slice(&path_with_leading_slash["/".len()..]).map_err(|err| { interpolate::Error::Utf8Conversion { what: "path past ~user/", err, } })?; Ok(home.join(path_past_user_prefix).into()) } } gix-config-value-0.14.4/src/types.rs000064400000000000000000000040171046102023000153610ustar 00000000000000use crate::{color, integer}; /// Any value that may contain a foreground color, background color, a /// collection of color (text) modifiers, or a combination of any of the /// aforementioned values, like `red` or `brightgreen`. /// /// Note that `git-config` allows color values to simply be a collection of /// [`color::Attribute`]s, and does not require a [`color::Name`] for either the /// foreground or background color. #[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)] pub struct Color { /// A provided foreground color pub foreground: Option, /// A provided background color pub background: Option, /// A potentially empty set of text attributes pub attributes: color::Attribute, } /// Any value that can be interpreted as an integer. /// /// This supports any numeric value that can fit in a [`i64`], excluding the /// suffix. The suffix is parsed separately from the value itself, so if you /// wish to obtain the true value of the integer, you must account for the /// suffix after fetching the value. [`integer::Suffix`] provides /// [`bitwise_offset()`][integer::Suffix::bitwise_offset] to help with the /// math, or [`to_decimal()`][Integer::to_decimal()] for obtaining a usable value in one step. #[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] pub struct Integer { /// The value, without any suffix modification pub value: i64, /// A provided suffix, if any. pub suffix: Option, } /// Any value that can be interpreted as a boolean. #[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] #[allow(missing_docs)] pub struct Boolean(pub bool); /// Any value that can be interpreted as a path to a resource on disk. /// /// Git represents file paths as byte arrays, modeled here as owned or borrowed byte sequences. #[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] pub struct Path<'a> { /// The path string, un-interpolated pub value: std::borrow::Cow<'a, bstr::BStr>, }