File size: 1,563 Bytes
afa0cbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use codex_code_mode_protocol::grpc as proto;
use tonic::Status;
use uuid::Uuid;

pub(super) use codex_code_mode_protocol::grpc::MAX_IDENTIFIER_BYTES;
pub(super) const MAX_TOOL_FILTERS: usize = 64;

pub(super) fn identifier(value: &str, field: &str) -> Result<(), Status> {
    if value.is_empty() {
        return Err(Status::invalid_argument(format!(
            "code-mode {field} must not be empty"
        )));
    }
    bounded(value, MAX_IDENTIFIER_BYTES, field)
}

pub(super) fn uuid(value: &str, field: &str) -> Result<Uuid, Status> {
    identifier(value, field)?;
    Uuid::parse_str(value)
        .map_err(|_| Status::invalid_argument(format!("code-mode {field} must be a UUID")))
}

pub(super) fn bounded(value: &str, maximum: usize, field: &str) -> Result<(), Status> {
    if value.len() > maximum {
        return Err(Status::invalid_argument(format!(
            "code-mode {field} exceeds {maximum} bytes"
        )));
    }
    Ok(())
}

pub(super) fn tool_name(name: &proto::ToolName) -> Result<(), Status> {
    identifier(&name.name, "tool name")?;
    if let Some(namespace) = name.namespace.as_deref() {
        bounded(namespace, MAX_IDENTIFIER_BYTES, "tool namespace")?;
    }
    Ok(())
}

pub(super) fn tool_filters(filters: &[proto::ToolName]) -> Result<(), Status> {
    if filters.len() > MAX_TOOL_FILTERS {
        return Err(Status::invalid_argument(format!(
            "code-mode tool subscription exceeds {MAX_TOOL_FILTERS} filters"
        )));
    }
    for filter in filters {
        tool_name(filter)?;
    }
    Ok(())
}