Skip to main content

gix_config_value/
lib.rs

1//! Parsing for data types used in `git-config` files to allow their use from environment variables and other sources.
2//!
3//! ## Examples
4//!
5//! ```
6//! use std::borrow::Cow;
7//!
8//! use bstr::ByteSlice;
9//! use gix_config_value::{Boolean, Integer, Path};
10//!
11//! let auto_crlf: bool = Boolean::try_from("true".as_bytes().as_bstr()).unwrap().into();
12//! assert!(auto_crlf);
13//!
14//! let packed_limit = Integer::try_from("10m".as_bytes().as_bstr()).unwrap();
15//! assert_eq!(packed_limit.to_decimal(), Some(10 * 1024 * 1024));
16//!
17//! let ignore_revs = Path::from(Cow::Borrowed(b":(optional)~/.git-blame-ignore-revs".as_bstr()));
18//! assert!(ignore_revs.is_optional);
19//! assert_eq!(ignore_revs.value.as_ref(), b"~/.git-blame-ignore-revs".as_bstr());
20//! ```
21//!
22//! ## Feature Flags
23#![cfg_attr(
24    all(doc, feature = "document-features"),
25    doc = ::document_features::document_features!()
26)]
27#![cfg_attr(all(doc, feature = "document-features"), feature(doc_cfg))]
28#![deny(missing_docs, rust_2018_idioms, unsafe_code)]
29
30/// The error returned when any config value couldn't be instantiated due to malformed input.
31#[derive(Debug, thiserror::Error, Eq, PartialEq)]
32#[allow(missing_docs)]
33#[error("Could not decode '{input}': {message}")]
34pub struct Error {
35    pub message: &'static str,
36    pub input: bstr::BString,
37    #[source]
38    pub utf8_err: Option<std::str::Utf8Error>,
39}
40
41impl Error {
42    /// Create a new value error from `message`, with `input` being what's causing the error.
43    pub fn new(message: &'static str, input: impl Into<bstr::BString>) -> Self {
44        Error {
45            message,
46            input: input.into(),
47            utf8_err: None,
48        }
49    }
50
51    pub(crate) fn with_err(mut self, err: std::str::Utf8Error) -> Self {
52        self.utf8_err = Some(err);
53        self
54    }
55}
56
57mod boolean;
58///
59pub mod color;
60///
61pub mod integer;
62///
63pub mod path;
64
65mod types;
66pub use types::{Boolean, Color, Integer, Path};