2024-03-15 20:48:13 +00:00
|
|
|
// Copyright 2024 The Jujutsu Authors
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
// https://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.
|
|
|
|
|
|
|
|
use jj_cli::cli_util::CliRunner;
|
|
|
|
use jj_cli::operation_templater::{
|
2024-03-19 15:01:06 +00:00
|
|
|
OperationTemplateBuildFnTable, OperationTemplateLanguage, OperationTemplateLanguageExtension,
|
2024-03-15 20:48:13 +00:00
|
|
|
};
|
|
|
|
use jj_cli::template_builder::TemplateLanguage;
|
|
|
|
use jj_cli::template_parser::{self, TemplateParseError};
|
2024-03-21 02:38:23 +00:00
|
|
|
use jj_cli::templater::TemplatePropertyExt as _;
|
2024-03-15 20:48:13 +00:00
|
|
|
use jj_lib::extensions_map::ExtensionsMap;
|
|
|
|
use jj_lib::object_id::ObjectId;
|
|
|
|
use jj_lib::op_store::OperationId;
|
|
|
|
use jj_lib::operation::Operation;
|
|
|
|
|
|
|
|
struct HexCounter;
|
|
|
|
|
|
|
|
fn num_digits_in_id(id: &OperationId) -> i64 {
|
|
|
|
let mut count = 0;
|
|
|
|
for ch in id.hex().chars() {
|
|
|
|
if ch.is_ascii_digit() {
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
count
|
|
|
|
}
|
|
|
|
|
2024-03-21 02:38:23 +00:00
|
|
|
fn num_char_in_id(operation: Operation, ch_match: char) -> i64 {
|
2024-03-15 20:48:13 +00:00
|
|
|
let mut count = 0;
|
|
|
|
for ch in operation.id().hex().chars() {
|
|
|
|
if ch == ch_match {
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
}
|
2024-03-21 02:38:23 +00:00
|
|
|
count
|
2024-03-15 20:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl OperationTemplateLanguageExtension for HexCounter {
|
|
|
|
fn build_fn_table(&self) -> OperationTemplateBuildFnTable {
|
2024-03-19 15:01:06 +00:00
|
|
|
type L = OperationTemplateLanguage;
|
2024-03-15 20:48:13 +00:00
|
|
|
let mut table = OperationTemplateBuildFnTable::empty();
|
|
|
|
table.operation_methods.insert(
|
|
|
|
"num_digits_in_id",
|
2024-03-19 15:01:06 +00:00
|
|
|
|_language, _build_context, property, call| {
|
2024-03-15 20:48:13 +00:00
|
|
|
template_parser::expect_no_arguments(call)?;
|
2024-03-21 02:38:23 +00:00
|
|
|
Ok(L::wrap_integer(
|
|
|
|
property.map(|operation| num_digits_in_id(operation.id())),
|
|
|
|
))
|
2024-03-15 20:48:13 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
table.operation_methods.insert(
|
|
|
|
"num_char_in_id",
|
2024-03-19 15:01:06 +00:00
|
|
|
|_language, _build_context, property, call| {
|
2024-03-15 20:48:13 +00:00
|
|
|
let [string_arg] = template_parser::expect_exact_arguments(call)?;
|
|
|
|
let char_arg =
|
|
|
|
template_parser::expect_string_literal_with(string_arg, |string, span| {
|
|
|
|
let chars: Vec<_> = string.chars().collect();
|
|
|
|
match chars[..] {
|
|
|
|
[ch] => Ok(ch),
|
|
|
|
_ => Err(TemplateParseError::unexpected_expression(
|
|
|
|
"Expected singular character argument",
|
|
|
|
span,
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
})?;
|
|
|
|
|
2024-03-21 02:38:23 +00:00
|
|
|
Ok(L::wrap_integer(
|
|
|
|
property.map(move |operation| num_char_in_id(operation, char_arg)),
|
|
|
|
))
|
2024-03-15 20:48:13 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
table
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_cache_extensions(&self, _extensions: &mut ExtensionsMap) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() -> std::process::ExitCode {
|
|
|
|
CliRunner::init()
|
|
|
|
.set_operation_template_extension(Box::new(HexCounter))
|
|
|
|
.run()
|
|
|
|
}
|