Skip to content

Commit

Permalink
Make de/serialized structs compliant with Rust naming practices (#1271)
Browse files Browse the repository at this point in the history
Rather than using lowercase names for types, we can
annotate structs and enums with `serde(rename_all = "snake_case")`
to have serde rename things during de/serialization. That
way, the code has Rust-style names in the source tree,
but the serialized variants remain snake_cased.
  • Loading branch information
caass authored Aug 23, 2024
1 parent 5903a8e commit a174fbf
Show file tree
Hide file tree
Showing 30 changed files with 233 additions and 231 deletions.
42 changes: 21 additions & 21 deletions nativelink-config/src/cas_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ pub type SchedulerRefName = String;
/// Used when the config references `instance_name` in the protocol.
pub type InstanceName = String;

#[allow(non_camel_case_types)]
#[derive(Deserialize, Debug, Default, Clone, Copy)]
#[serde(rename_all = "snake_case")]
pub enum HttpCompressionAlgorithm {
/// No compression.
#[default]
none,
None,

/// Zlib compression.
gzip,
Gzip,
}

/// Note: Compressing data in the cloud rarely has a benefit, since most
Expand Down Expand Up @@ -360,11 +360,11 @@ pub struct HttpServerConfig {
pub experimental_http2_max_header_list_size: Option<u32>,
}

#[allow(non_camel_case_types)]
#[derive(Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
pub enum ListenerConfig {
/// Listener for HTTP/HTTPS/HTTP2 sockets.
http(HttpListener),
Http(HttpListener),
}

#[derive(Deserialize, Debug)]
Expand Down Expand Up @@ -408,18 +408,18 @@ pub struct ServerConfig {
pub services: Option<ServicesConfig>,
}

#[allow(non_camel_case_types)]
#[derive(Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
pub enum WorkerProperty {
/// List of static values.
/// Note: Generally there should only ever be 1 value, but if the platform
/// property key is PropertyType::Priority it may have more than one value.
#[serde(deserialize_with = "convert_vec_string_with_shellexpand")]
values(Vec<String>),
Values(Vec<String>),

/// A dynamic configuration. The string will be executed as a command
/// (not sell) and will be split by "\n" (new line character).
query_cmd(String),
QueryCmd(String),
}

/// Generic config for an endpoint and associated configs.
Expand All @@ -438,35 +438,35 @@ pub struct EndpointConfig {
pub tls_config: Option<ClientTlsConfig>,
}

#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Deserialize, Debug, Default)]
#[serde(rename_all = "snake_case")]
pub enum UploadCacheResultsStrategy {
/// Only upload action results with an exit code of 0.
#[default]
success_only,
SuccessOnly,

/// Don't upload any action results.
never,
Never,

/// Upload all action results that complete.
everything,
Everything,

/// Only upload action results that fail.
failures_only,
FailuresOnly,
}

#[allow(non_camel_case_types)]
#[derive(Clone, Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
pub enum EnvironmentSource {
/// The name of the platform property in the action to get the value from.
property(String),
Property(String),

/// The raw value to set.
value(#[serde(deserialize_with = "convert_string_with_shellexpand")] String),
Value(#[serde(deserialize_with = "convert_string_with_shellexpand")] String),

/// The max amount of time in milliseconds the command is allowed to run
/// (requested by the client).
timeout_millis,
TimeoutMillis,

/// A special file path will be provided that can be used to comminicate
/// with the parent process about out-of-band information. This file
Expand All @@ -484,7 +484,7 @@ pub enum EnvironmentSource {
///
/// All fields are optional, file does not need to be created and may be
/// empty.
side_channel_file,
SideChannelFile,

/// A "root" directory for the action. This directory can be used to
/// store temporary files that are not needed after the action has
Expand All @@ -499,7 +499,7 @@ pub enum EnvironmentSource {
/// variable, `mkdir $ENV_VAR_NAME/tmp` and `export TMPDIR=$ENV_VAR_NAME/tmp`.
/// Another example might be to bind-mount the `/tmp` path in a container to
/// this path in `entrypoint`.
action_directory,
ActionDirectory,
}

#[derive(Deserialize, Debug, Default)]
Expand Down Expand Up @@ -653,11 +653,11 @@ pub struct LocalWorkerConfig {
pub additional_environment: Option<HashMap<String, EnvironmentSource>>,
}

#[allow(non_camel_case_types)]
#[derive(Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
pub enum WorkerConfig {
/// A worker type that executes jobs locally on this machine.
local(LocalWorkerConfig),
Local(LocalWorkerConfig),
}

#[derive(Deserialize, Debug, Clone, Copy)]
Expand Down
30 changes: 15 additions & 15 deletions nativelink-config/src/schedulers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,50 @@ use serde::Deserialize;
use crate::serde_utils::{convert_duration_with_shellexpand, convert_numeric_with_shellexpand};
use crate::stores::{GrpcEndpoint, Retry, StoreRefName};

#[allow(non_camel_case_types)]
#[derive(Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
pub enum SchedulerConfig {
simple(SimpleScheduler),
grpc(GrpcScheduler),
cache_lookup(CacheLookupScheduler),
property_modifier(PropertyModifierScheduler),
Simple(SimpleScheduler),
Grpc(GrpcScheduler),
CacheLookup(CacheLookupScheduler),
PropertyModifier(PropertyModifierScheduler),
}

/// When the scheduler matches tasks to workers that are capable of running
/// the task, this value will be used to determine how the property is treated.
#[allow(non_camel_case_types)]
#[derive(Deserialize, Debug, Clone, Copy, Hash, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum PropertyType {
/// Requires the platform property to be a u64 and when the scheduler looks
/// for appropriate worker nodes that are capable of executing the task,
/// the task will not run on a node that has less than this value.
minimum,
Minimum,

/// Requires the platform property to be a string and when the scheduler
/// looks for appropriate worker nodes that are capable of executing the
/// task, the task will not run on a node that does not have this property
/// set to the value with exact string match.
exact,
Exact,

/// Does not restrict on this value and instead will be passed to the worker
/// as an informational piece.
/// TODO(allada) In the future this will be used by the scheduler and worker
/// to cause the scheduler to prefer certain workers over others, but not
/// restrict them based on these values.
priority,
Priority,
}

/// When a worker is being searched for to run a job, this will be used
/// on how to choose which worker should run the job when multiple
/// workers are able to run the task.
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Deserialize, Debug, Default)]
#[serde(rename_all = "snake_case")]
pub enum WorkerAllocationStrategy {
/// Prefer workers that have been least recently used to run a job.
#[default]
least_recently_used,
LeastRecentlyUsed,
/// Prefer workers that have been most recently used to run a job.
most_recently_used,
MostRecentlyUsed,
}

#[derive(Deserialize, Debug, Default)]
Expand Down Expand Up @@ -168,13 +168,13 @@ pub struct PlatformPropertyAddition {
pub value: String,
}

#[allow(non_camel_case_types)]
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
pub enum PropertyModification {
/// Add a property to the action properties.
add(PlatformPropertyAddition),
Add(PlatformPropertyAddition),
/// Remove a named property from the action.
remove(String),
Remove(String),
}

#[derive(Deserialize, Debug)]
Expand Down
Loading

0 comments on commit a174fbf

Please sign in to comment.