Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ rust-version = "1.89"
license = "MIT OR Apache-2.0"
repository = "https://github.com/DreamLab-AI/dream-engine"
authors = ["DreamLab AI"]
version = "0.1.0"
version = "0.1.1"
4 changes: 2 additions & 2 deletions crates/dream-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ name = "dream_cli"
path = "src/lib.rs"

[dependencies]
dream-gate = { path = "../dream-gate", version = "0.1.0" }
dream-core = { path = "../dream-core", version = "0.1.0" }
dream-gate = { path = "../dream-gate", version = "0.1.1" }
dream-core = { path = "../dream-core", version = "0.1.1" }
clap = { version = "4", features = ["derive"] }
serde_json = "1"
thiserror = "2"
Expand Down
2 changes: 1 addition & 1 deletion crates/dream-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ categories = ["development-tools", "development-tools::testing"]
authors.workspace = true

[dependencies]
dream-gate = { path = "../dream-gate", version = "0.1.0" }
dream-gate = { path = "../dream-gate", version = "0.1.1" }
failure-fingerprint = { path = "../failure-fingerprint", version = "0.1.0" }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
Expand Down
2 changes: 1 addition & 1 deletion crates/dream-core/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ pub fn compile(
.collect::<Vec<_>>()
.join("\n");

let adr_example = format!("ADR-{:0>width$}", 1, width = cfg.adr_convention.pad());
let adr_example = cfg.adr_convention.example(1);
let labels = if cfg.labels.is_empty() {
"none".to_string()
} else {
Expand Down
43 changes: 43 additions & 0 deletions crates/dream-gate/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,25 @@ pub enum AdrConvention {
},
}

/// Byte offset and length of the first longest run of `N` in a template.
fn longest_n_run(t: &str) -> Option<(usize, usize)> {
let (mut best, mut run_start, mut run) = (None::<(usize, usize)>, 0usize, 0usize);
for (i, c) in t.char_indices() {
if c == 'N' {
if run == 0 {
run_start = i;
}
run += 1;
if best.is_none_or(|(_, l)| run > l) {
best = Some((run_start, run));
}
} else {
run = 0;
}
}
best
}

/// Width of the longest run of `N` in a numbering template, or `None` when the
/// text has no `N` at all and so is not a template.
fn template_pad(t: &str) -> Option<usize> {
Expand Down Expand Up @@ -143,6 +162,30 @@ impl AdrConvention {
}
}

/// The identifier for ADR number `n` under this convention.
///
/// A free-form template keeps everything around its numbering run, so
/// `ADR-OM-NNN` gives `ADR-OM-006` for 6; the other forms give `ADR-` plus
/// the zero-padded number.
///
/// ```
/// use dream_gate::config::AdrConvention;
/// let t: AdrConvention = serde_json::from_str("\"ADR-OM-NNN\"").unwrap();
/// assert_eq!(t.example(6), "ADR-OM-006");
/// assert_eq!(AdrConvention::ThreeDigit.example(1), "ADR-001");
/// ```
pub fn example(&self, n: u32) -> String {
let width = self.pad();
let number = format!("{n:0>width$}");
match self {
AdrConvention::Template(t) => match longest_n_run(t) {
Some((start, len)) => format!("{}{number}{}", &t[..start], &t[start + len..]),
None => format!("ADR-{number}"),
},
_ => format!("ADR-{number}"),
}
}

/// The configured directory, for the custom form only.
pub fn dir(&self) -> Option<&str> {
match self {
Expand Down
15 changes: 15 additions & 0 deletions crates/dream-gate/tests/adr_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,18 @@ fn named_forms_are_unchanged() {
assert_eq!(parse("\"3-digit\"").unwrap(), AdrConvention::ThreeDigit);
assert_eq!(parse("\"4-digit\"").unwrap(), AdrConvention::FourDigit);
}

#[test]
fn example_keeps_the_template_prefix() {
assert_eq!(parse("\"ADR-OM-NNN\"").unwrap().example(6), "ADR-OM-006");
assert_eq!(parse("\"ADR-NNNNN\"").unwrap().example(1), "ADR-00001");
assert_eq!(parse("\"N-ADR-NNNN\"").unwrap().example(12), "N-ADR-0012");
assert_eq!(parse("\"ADR-NN-NNN\"").unwrap().example(2), "ADR-NN-002");
}

#[test]
fn example_for_named_forms_is_plain_adr() {
assert_eq!(parse("\"3-digit\"").unwrap().example(1), "ADR-001");
assert_eq!(parse("\"4-digit\"").unwrap().example(1), "ADR-0001");
assert_eq!(AdrConvention::Unset.example(7), "ADR-0007");
}
Loading