ec4rs/
lib.rs

1#![doc = include_str!("../rustdoc.md")]
2#![deny(clippy::as_conversions)]
3#![deny(clippy::enum_glob_use)]
4#![deny(clippy::wildcard_imports)]
5#![deny(missing_docs)]
6#![deny(unsafe_code)]
7#![deny(rustdoc::bare_urls)]
8#![deny(rustdoc::broken_intra_doc_links)]
9#![deny(rustdoc::invalid_codeblock_attributes)]
10#![deny(rustdoc::invalid_html_tags)]
11#![deny(rustdoc::invalid_rust_codeblocks)]
12#![deny(rustdoc::private_intra_doc_links)]
13#![warn(clippy::if_then_some_else_none)]
14#![warn(clippy::pedantic)]
15#![allow(clippy::doc_markdown)] // reason = "False positives on EditorConfig".
16#![allow(clippy::module_name_repetitions)] // reason = "Affects re-exports from private modules."
17#![allow(clippy::must_use_candidate)] // reason = "Too pedantic."
18#![allow(clippy::semicolon_if_nothing_returned)] // reason = "Too pedantic."
19#![allow(clippy::let_underscore_untyped)] // reason = "Too pedantic."
20#![allow(clippy::missing_errors_doc)] // reason = "TODO: Fix."
21#![cfg_attr(doc_unstable, feature(doc_auto_cfg))]
22
23mod error;
24mod fallback;
25mod file;
26mod glob;
27mod linereader;
28mod parser;
29mod properties;
30pub mod property;
31pub mod rawvalue;
32mod section;
33#[cfg(test)]
34mod tests;
35mod traits;
36pub mod version;
37
38pub use error::{Error, ParseError};
39pub use file::{ConfigFile, ConfigFiles};
40pub use parser::ConfigParser;
41pub use properties::{Properties, PropertiesSource};
42pub use section::Section;
43pub use traits::*;
44
45#[cfg(feature = "language-tags")]
46pub mod language_tags {
47    //! Re-export of the `language-tags` crate.
48    pub use ::language_tags::*;
49}
50
51/// Retrieves the [`Properties`] for a file at the given path.
52///
53/// This is the simplest way to use this library in an EditorConfig integration or plugin.
54///
55/// This function does not canonicalize the path,
56/// but will join relative paths onto the current working directory.
57///
58/// EditorConfig files are assumed to be named `.editorconfig`.
59/// If not, use [`properties_from_config_of`]
60pub fn properties_of(path: impl AsRef<std::path::Path>) -> Result<Properties, Error> {
61    properties_from_config_of(path, Option::<&std::path::Path>::None)
62}
63
64/// Retrieves the [`Properties`] for a file at the given path,
65/// expecting EditorConfig files to be named matching `config_path_override`.
66///
67/// This function does not canonicalize the path,
68/// but will join relative paths onto the current working directory.
69///
70/// If the provided config path is absolute, uses the EditorConfig file at that path.
71/// If it's relative, joins it onto every ancestor of the target file,
72/// and looks for config files at those paths.
73/// If it's `None`, EditorConfig files are assumed to be named `.editorconfig`.
74pub fn properties_from_config_of(
75    target_path: impl AsRef<std::path::Path>,
76    config_path_override: Option<impl AsRef<std::path::Path>>,
77) -> Result<Properties, Error> {
78    let mut retval = Properties::new();
79    ConfigFiles::open(&target_path, config_path_override)?.apply_to(&mut retval, &target_path)?;
80    Ok(retval)
81}