-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alexis-Bernard <[email protected]> fix: deleted files are back Signed-off-by: WoodenMaiden <[email protected]>
- Loading branch information
1 parent
8609280
commit 1a0fcaf
Showing
7 changed files
with
246 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use anyhow::Result; | ||
use serde::{Deserialize, Serialize}; | ||
use std::{ | ||
fs::File, | ||
io::{self, BufReader}, | ||
}; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum AgentConfigError { | ||
#[error("cannot load config file")] | ||
Load(#[from] io::Error), | ||
#[error("cannot parse config file")] | ||
Parse(#[from] serde_yaml::Error), | ||
#[error("unsupported config kind")] | ||
KindNotSupported, | ||
#[error("unsupported config api version")] | ||
VersionNotSupported, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, PartialEq, Debug)] | ||
#[allow(non_snake_case)] | ||
pub struct AgentConfig { | ||
/// The api version of the agent config file | ||
pub apiVersion: String, | ||
/// The kind of the agent config file | ||
pub kind: String, | ||
/// The serial configuration | ||
pub serial: SerialConfig, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, PartialEq, Debug)] | ||
pub struct SerialConfig { | ||
/// The path to the serial port | ||
pub path: String, | ||
/// The baud rate to use for the serial port | ||
pub baud_rate: u32, | ||
} | ||
|
||
impl AgentConfig { | ||
/// Load a AgentConfig from a file. | ||
/// | ||
/// Arguments: | ||
/// | ||
/// * `path`: The path to the config file. | ||
/// | ||
/// Returns: | ||
/// | ||
/// A Result<AgentConfig> | ||
pub fn load(path: &str) -> Result<Self> { | ||
let file = File::open(path).map_err(AgentConfigError::Load)?; | ||
let reader = BufReader::new(file); | ||
let config: AgentConfig = | ||
serde_yaml::from_reader(reader).map_err(AgentConfigError::Parse)?; | ||
|
||
if config.kind != "AgentConfig" { | ||
return Err(AgentConfigError::KindNotSupported.into()); | ||
} | ||
|
||
if config.apiVersion != "lambdo.io/v1alpha1" { | ||
return Err(AgentConfigError::VersionNotSupported.into()); | ||
} | ||
|
||
Ok(config) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// This message is sent to the API server to indicate wether | ||
// the agent is ready or not to receive messages. | ||
|
||
pub const MESSAGE_SIZE_NB_BYTES: usize = 8; | ||
|
||
pub struct Message { | ||
pub message_size: [u8; MESSAGE_SIZE_NB_BYTES], // These are characters e.g. 00002048 | ||
pub message: Vec<u8> // stringified json, vec because size is unknown | ||
} | ||
|
||
impl Message { | ||
pub fn new(message_to_send: String) -> Self { | ||
let mut message_size = [0; MESSAGE_SIZE_NB_BYTES]; | ||
let message = message_to_send.as_bytes().to_vec(); | ||
|
||
let string_size = format!("{:0>8}", message.len()); | ||
//We can't call directly as bytes as both &str and String sizes are not known at | ||
//compile time unlike message_size | ||
|
||
for (i, c) in string_size.chars().enumerate() { | ||
message_size[i] = c as u8; | ||
} | ||
|
||
Self { | ||
message_size, | ||
message | ||
} | ||
} | ||
|
||
pub fn to_bytes(&self) -> Vec<u8> { | ||
let mut bytes = Vec::new(); | ||
bytes.extend_from_slice(&self.message_size); | ||
bytes.extend_from_slice(&self.message); | ||
bytes | ||
} | ||
|
||
|
||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn new_message_well_encoded() { | ||
let message_data = "Hello world".to_string(); | ||
let message = Message::new(message_data); | ||
assert_eq!(message.message, [72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]); | ||
assert_eq!(message.message_size, [48, 48, 48, 48, 48, 48, 49, 49]); | ||
|
||
assert_eq!(message.to_bytes(), [48, 48, 48, 48, 48, 48, 49, 49, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]); | ||
} | ||
|
||
|
||
#[test] | ||
fn message_size_badly_encoded() { | ||
let message_data = "Hello world".to_string(); | ||
let message = Message::new(message_data); | ||
assert_eq!(message.message, [72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]); | ||
assert_ne!(message.message_size, [48, 48, 48, 48, 48, 48, 49, 50]); // should be 11, is 12 | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod model; | ||
pub mod service; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.