Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
split the project into crates
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Aug 19, 2024
1 parent 26a3578 commit 8125b16
Show file tree
Hide file tree
Showing 46 changed files with 639 additions and 500 deletions.
387 changes: 229 additions & 158 deletions Cargo.lock

Large diffs are not rendered by default.

53 changes: 28 additions & 25 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
[package]
name = "openadr-rs"
[workspace]
members = [
"openadr-vtn",
"openadr-client",
"openadr-wire"
]
exclude = [ ]

resolver = "2"

[workspace.package]
version = "0.1.0"
edition = "2021"
rust-version = "1.76" # MSRV
Expand All @@ -9,42 +18,36 @@ homepage = "https://github.com/tweedegolf/openadr-rs"
publish = true
description = "An OpenADR 3.0 VTN/VEN implementation"

[dependencies]
reqwest = { version = "0.12.4", default-features = false, features = ["http2", "charset", "rustls-tls-native-roots", "json"] }
[workspace.dependencies]
openadr-wire = { path = "openadr-wire" }
openadr-vtn = { path = "openadr-vtn" }
openadr-client = { path = "openadr-client" }

serde = { version = "1.0.203", features = ["derive"] }
serde_json = "1.0.117"
serde_with = { version = "3.8.1", features = ["macros"] }
tokio = { version = "1.37.0", features = ["full"] }

reqwest = { version = "0.12.4", default-features = false, features = ["http2", "charset", "rustls-tls-native-roots", "json"] }
tokio = { version = "1.37.0", features = ["full", "test-util"] }
axum = { version = "0.7.5", features = ["macros"] }
axum-extra = { version = "0.9.3", features = ["query", "typed-header"] }
tower = { version = "0.4", features = ["util"] }

tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }

chrono = "0.4.38"
iso8601-duration = { version = "0.2.0", features = ["chrono"] }
rangemap = "1.5.1"

thiserror = "1.0.61"
validator = {version = "0.18.1", features = ["derive"] }
uuid = { version = "1.8.0", features = ["v4"] }
regex = "1.10.4"
chrono = "0.4.38"
url = "2.5.0"
mime = "0.3"
tower-http = { version = "0.5.2" , features = ["trace"]}
iso8601-duration = { version = "0.2.0", features = ["chrono"] }
rangemap = "1.5.1"
http-body-util = "0.1.0"
jsonwebtoken = "9.3.0"
async-trait = "0.1.81"
tower = { version = "0.4", features = ["util"] }
http-body-util = "0.1.0"

[dev-dependencies]
quickcheck = "1.0.3"
mime = "0.3"
tokio = { version = "1.37.0", features = ["full", "test-util"] }

[lib]
name = "openadr"

[[bin]]
name = "vtn"
path = "src/vtn/main.rs"

[[bin]]
name = "openadr"
path = "src/cli/main.rs"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
33 changes: 33 additions & 0 deletions openadr-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[package]
name = "openadr-client"
description = "openadr client"
readme = "README.md"
version.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
homepage.workspace = true
publish.workspace = true
rust-version.workspace = true

[dependencies]
openadr-wire.workspace = true
openadr-vtn.workspace = true

serde.workspace = true
serde_json.workspace = true

reqwest.workspace = true
axum.workspace = true
tokio = { workspace = true, features = ["full"] }
tracing.workspace = true
http-body-util.workspace = true
tower.workspace = true

url.workspace = true
chrono.workspace = true
rangemap.workspace = true
uuid.workspace = true

[dev-dependencies]
tokio = { workspace = true, features = ["full", "test-util"] }
5 changes: 3 additions & 2 deletions src/cli/main.rs → openadr-client/src/bin/cli.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use openadr::{ClientCredentials, ProgramContent, Target};
use openadr_client::{ClientCredentials, Target};
use openadr_wire::program::ProgramContent;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = openadr::Client::with_url(
let client = openadr_client::Client::with_url(
"http://localhost:3000/".try_into()?,
Some(ClientCredentials::new(
"admin".to_string(),
Expand Down
21 changes: 10 additions & 11 deletions src/bin/everest.rs → openadr-client/src/bin/everest.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use chrono::{DateTime, Utc};
use openadr::{
wire::{
event::{EventType, EventValuesMap},
values_map::Value,
},
ClientRef, ProgramClient, Target, Timeline,
use openadr_wire::{
event::{EventType, EventValuesMap},
values_map::Value,
};

use openadr_client::{ClientRef, ProgramClient, Target, Timeline};
use std::{error::Error, time::Duration};
use tokio::sync::mpsc::{Receiver, Sender};
use tokio::{select, sync::mpsc};
Expand Down Expand Up @@ -39,7 +38,7 @@ impl Clock for ChronoClock {

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let client = openadr::Client::with_url("http://localhost:3000/".try_into()?, None);
let client = openadr_client::Client::with_url("http://localhost:3000/".try_into()?, None);
let program = client.get_program(Target::Program("name")).await?;

// channel used to send new timelines
Expand All @@ -63,7 +62,7 @@ async fn poll_timeline(
mut program: ProgramClient<impl ClientRef>,
poll_interval: std::time::Duration,
sender: mpsc::Sender<Timeline>,
) -> Result<(), openadr::Error> {
) -> Result<(), openadr_client::Error> {
loop {
tokio::time::sleep(poll_interval).await;

Expand Down Expand Up @@ -187,9 +186,9 @@ impl LimitsRes {
#[cfg(test)]
mod test {
use super::*;
use openadr::wire::event::EventInterval;
use openadr::wire::interval::IntervalPeriod;
use openadr::{EventContent, ProgramContent, ProgramId};
use openadr_wire::event::{EventContent, EventInterval};
use openadr_wire::interval::IntervalPeriod;
use openadr_wire::program::{ProgramContent, ProgramId};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;

Expand Down
12 changes: 6 additions & 6 deletions src/error.rs → openadr-client/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ pub enum Error {
Reqwest(reqwest::Error),
Serde(serde_json::Error),
UrlParseError(url::ParseError),
Problem(crate::wire::Problem),
AuthProblem(crate::wire::oauth::OAuthError),
Problem(openadr_vtn::Problem),
AuthProblem(openadr_wire::oauth::OAuthError),
OAuthTokenNotBearer,
ObjectNotFound,
DuplicateObject,
Expand All @@ -31,14 +31,14 @@ impl From<url::ParseError> for Error {
}
}

impl From<crate::wire::Problem> for Error {
fn from(err: crate::wire::Problem) -> Self {
impl From<openadr_vtn::Problem> for Error {
fn from(err: openadr_vtn::Problem) -> Self {
Error::Problem(err)
}
}

impl From<crate::wire::oauth::OAuthError> for Error {
fn from(err: crate::wire::oauth::OAuthError) -> Self {
impl From<openadr_wire::oauth::OAuthError> for Error {
fn from(err: openadr_wire::oauth::OAuthError) -> Self {
Error::AuthProblem(err)
}
}
Expand Down
19 changes: 10 additions & 9 deletions src/client/event.rs → openadr-client/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::sync::Arc;

use crate::{
client::ClientRef,
wire::{
report::{ReportContent, ReportObjectType},
Event, Report,
},
EventContent, ReportClient, Result,
error::{Error, Result},
ClientRef, ReportClient,
};
use openadr_wire::{
event::EventContent,
report::{ReportContent, ReportObjectType},
Event, Report,
};

#[derive(Debug)]
Expand All @@ -23,7 +24,7 @@ impl<C: ClientRef> EventClient<C> {
}
}

pub fn id(&self) -> &crate::wire::event::EventId {
pub fn id(&self) -> &openadr_wire::event::EventId {
&self.data.id
}

Expand Down Expand Up @@ -76,11 +77,11 @@ impl<C: ClientRef> EventClient<C> {
/// Create a new report for the event
pub async fn create_report(&self, report_data: ReportContent) -> Result<ReportClient<C>> {
if report_data.program_id != self.data().program_id {
return Err(crate::Error::InvalidParentObject);
return Err(Error::InvalidParentObject);
}

if &report_data.event_id != self.id() {
return Err(crate::Error::InvalidParentObject);
return Err(Error::InvalidParentObject);
}

let report = self.client.post("events", &report_data, &[]).await?;
Expand Down
17 changes: 10 additions & 7 deletions src/client/mod.rs → openadr-client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod error;
mod event;
mod program;
mod report;
Expand All @@ -17,18 +18,20 @@ use http_body_util::BodyExt;
use tower::{Service, ServiceExt};
use url::Url;

pub use error::*;
pub use event::*;
pub use program::*;
pub use report::*;
pub use target::*;
pub use timeline::*;

pub use crate::wire::{
use crate::error::Result;
pub(crate) use openadr_wire::{
event::EventContent,
program::{ProgramContent, ProgramId},
target::TargetLabel,
Program,
};
use crate::wire::{target::TargetLabel, Program};
use crate::Result;

/// Client used for interaction with a VTN.
///
Expand Down Expand Up @@ -120,7 +123,7 @@ impl ReqwestClientRef {
let since = Instant::now();
let res = request.send().await?;
if !res.status().is_success() {
let problem = res.json::<crate::wire::oauth::OAuthError>().await?;
let problem = res.json::<openadr_wire::oauth::OAuthError>().await?;
return Err(crate::Error::AuthProblem(problem));
}

Expand Down Expand Up @@ -213,8 +216,8 @@ impl ReqwestClientRef {

// handle any errors returned by the server
if !res.status().is_success() {
let problem = res.json::<crate::wire::Problem>().await?;
return Err(crate::Error::from(problem));
let problem = res.json::<openadr_vtn::Problem>().await?;
return Err(crate::error::Error::from(problem));
}

Ok(res.json().await?)
Expand Down Expand Up @@ -506,7 +509,7 @@ impl<C: ClientRef> Client<C> {
pub async fn get_program_by_id(&self, id: &ProgramId) -> Result<ProgramClient<C>> {
let program = self
.client_ref
.get(&format!("programs/{}", id.0), &[])
.get(&format!("programs/{}", id.as_str()), &[])
.await?;

Ok(ProgramClient::from_program(
Expand Down
15 changes: 8 additions & 7 deletions src/client/program.rs → openadr-client/src/program.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::sync::Arc;

use openadr_wire::{
event::{EventObjectType, Priority},
target::TargetLabel,
Event, Program,
};

use crate::{
client::ClientRef,
wire::{
event::{EventObjectType, Priority},
target::TargetLabel,
Event, Program,
},
Error, EventClient, EventContent, ProgramContent, ProgramId, Result, Target, Timeline,
error::{Error, Result},
ClientRef, EventClient, EventContent, ProgramContent, ProgramId, Target, Timeline,
};

/// A client for interacting with the data in a specific program and the events
Expand Down
11 changes: 5 additions & 6 deletions src/client/report.rs → openadr-client/src/report.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::sync::Arc;

use crate::{
client::ClientRef,
wire::{report::ReportContent, Report},
Result,
};
use openadr_wire::{report::ReportContent, Report};

use crate::error::Result;
use crate::ClientRef;

#[derive(Debug)]
pub struct ReportClient<C> {
Expand All @@ -20,7 +19,7 @@ impl<C: ClientRef> ReportClient<C> {
}
}

pub fn id(&self) -> &crate::wire::report::ReportId {
pub fn id(&self) -> &openadr_wire::report::ReportId {
&self.data.id
}

Expand Down
2 changes: 1 addition & 1 deletion src/client/target.rs → openadr-client/src/target.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::wire::target::TargetLabel;
use openadr_wire::target::TargetLabel;

/// Target for a query to the VTN
#[derive(Copy, Clone, Debug)]
Expand Down
Loading

0 comments on commit 8125b16

Please sign in to comment.