Skip to main content

chrony_confile/
lib.rs

1#![deny(unsafe_code)]
2#![doc = "A full-featured Rust library for parsing, editing, validating, and serializing [chrony](https://chrony-project.org/) configuration files.\n\
3\n\
4This crate provides complete coverage of all 96 chrony directives with a type-safe API,\n\
5lossless round-trip serialization (comments and formatting preserved), and optional\n\
6`include`/`confdir` expansion.\n\
7\n\
8# Quick Start\n\
9\n\
10```rust\n\
11use chrony_confile::prelude::*;\n\
12\n\
13let config: ChronyConfig = r#\"\n\
14    server ntp1.example.com iburst prefer\n \
15    server ntp2.example.com iburst\n \
16    pool pool.ntp.org maxsources 6\n \
17    driftfile /var/lib/chrony/drift\n \
18    allow 192.168.0.0/16\n \
19\"#.parse()?;\n\
20\n\
21// Iterate over directives with a specific name\n\
22for directive in config.find(\"server\") {\n\
23    if let DirectiveKind::Server(s) = &directive.kind {\n\
24        println!(\"NTP source: {}\", s.hostname);\n\
25    }\n\
26}\n\
27\n\
28// Validate the configuration\n\
29let errors = config.validate();\n\
30\n\
31// Serialize back to string (lossless)\n\
32let output = config.to_string();\n\
33# Ok::<_, chrony_confile::ParseError>(())\n\
34```\n\
35\n\
36# Modules\n\
37\n\
38- **`ast`** -- All AST types representing chrony directives and configuration structure\n\
39- **`parser`** -- Parsing logic for chrony config files and .sources files\n\
40- **`ser`** -- Serialization (`Display` trait impls, optional `serde`)\n\
41- **`validator`** -- Structural and semantic validation\n\
42- **`builder`** -- Builder pattern for programmatic config generation\n\
43- **`expand`** -- Include/confdir expansion\n\
44- **`error`** -- Error types for parsing, validation, and value construction\n\
45- **`span`** -- Source location tracking\n\
46- **`values`** -- Bounded integer newtypes with range checking\n\
47\n\
48# Feature Flags\n\
49\n\
50- `serde` (optional) -- Enables `Serialize` for `ChronyConfig`\n\
51"]
52
53pub mod error;
54pub mod span;
55pub mod values;
56pub mod ast;
57pub mod parser;
58pub mod ser;
59pub mod validator;
60pub mod builder;
61pub mod expand;
62
63// --- Core re-exports ---
64pub use ast::ChronyConfig;
65pub use ast::source_file::SourceFile;
66pub use error::{ParseError, DirectiveError, ValueError};
67pub use span::Span;
68pub use ast::Directive;
69pub use ast::DirectiveKind;
70pub use ast::DirectiveCategory;
71pub use ast::ConfigNode;
72
73// --- Value types ---
74pub use values::{
75    PollInterval, UdpPort, Dscp, Stratum, SamplesCount, PollTarget,
76    NtpVersion, PpsRate, SchedPriority, PtpDomain, ClientLogSize,
77    TxBuffers, NtsProcesses,
78};
79
80// --- Source types ---
81pub use ast::source::{
82    ServerConfig, PoolConfig, PeerConfig, RefClockConfig, RefClockDriverKind,
83    SourceConnectivity, AddressFamily, SelectOptions, AllowDenyConfig,
84    RateLimitConfig, NtsRateLimitConfig, SmoothTimeConfig, BroadcastConfig,
85    LocalConfig, InitStepSlewConfig,
86};
87
88// --- NTP config types ---
89pub use ast::ntp::{
90    BindAddressConfig, BindDeviceConfig, ClientLogLimitConfig,
91    NtpSignDSocketConfig, NtsPortConfig, NtsServerCertConfig,
92    NtsServerKeyConfig, NtsProcessesConfig, MaxNtsConnectionsConfig,
93    NtsNtpServerConfig, NtsRotateConfig, PortConfig, CmdRateLimitConfig,
94};
95
96// --- Command access types ---
97pub use ast::cmd::{
98    BindCmdAddressConfig, BindCmdDeviceConfig, CmdPortConfig,
99    OpenCommandsConfig,
100};
101
102// --- Clock types ---
103pub use ast::clock::{
104    ClockPrecisionConfig, CorrTimeRatioConfig, DriftFileConfig,
105    FallbackDriftConfig, LeapSecTzConfig, LeapSecListConfig,
106    MakeStepConfig, MaxChangeConfig, MaxClockErrorConfig, MaxDriftConfig,
107    MaxUpdateSkewConfig, MaxSlewRateConfig, TempCompConfig,
108    LeapSecMode,
109};
110
111// --- Selection types ---
112pub use ast::selection::{
113    AuthSelectMode, CombineLimitConfig, MaxDistanceConfig, MaxJitterConfig,
114    MinSourcesConfig, ReselectDistConfig, StratumWeightConfig,
115};
116
117// --- Log types ---
118pub use ast::log::{
119    LogConfig, LogBannerConfig, LogChangeConfig, LogDirConfig,
120};
121
122// --- RTC types ---
123pub use ast::rtc::{
124    HwClockFileConfig, RtcAutoTrimConfig, RtcDeviceConfig, RtcFileConfig,
125};
126
127// --- HW timestamp types ---
128pub use ast::hw::{
129    HwTimestampConfig, HwTsTimeoutConfig, MaxTxBuffersConfig,
130    PtpPortConfig, PtpDomainConfig, HwTsRxFilter,
131};
132
133// --- NTS types ---
134pub use ast::nts::NtsAeadsConfig;
135
136// --- Misc types ---
137pub use ast::misc::{
138    ConfDirConfig, IncludeConfig, SourceDirConfig, MailOnChangeConfig,
139    PidFileConfig, SchedPriorityConfig, UserConfig, KeyFileConfig,
140    AcquisitionPortConfig, DscpConfig, DumpDirConfig, NtsDumpDirConfig,
141    NtsRefreshConfig, NtsTrustedCertsConfig, NoCertTimeCheckConfig,
142    RefreshConfig, MaxSamplesConfig, MinSamplesConfig,
143};
144
145// --- Source file types ---
146pub use ast::source_file::{SourceEntry, SourceNode};
147
148// --- Builder types ---
149pub use builder::{ServerBuilder, PoolBuilder};
150
151// --- Expand types ---
152pub use expand::ExpandOptions;
153
154// --- FromStr impl ---
155
156impl std::str::FromStr for ChronyConfig {
157    type Err = ParseError;
158
159    fn from_str(s: &str) -> Result<Self, Self::Err> {
160        Self::parse(s)
161    }
162}
163
164/// Convenience prelude for glob-importing the most common types.
165///
166/// ```rust
167/// use chrony_confile::prelude::*;
168/// ```
169pub mod prelude {
170    pub use crate::ast::*;
171    pub use crate::builder::*;
172    pub use crate::error::*;
173    pub use crate::span::*;
174    pub use crate::values::*;
175    pub use crate::validator::Validate;
176    pub use crate::ChronyConfig;
177    pub use crate::SourceFile;
178    pub use std::str::FromStr;
179}