Skip to content

Commit

Permalink
Put related things together (in-memory store, errors, traits, types) …
Browse files Browse the repository at this point in the history
…and don't use the internal version types of the in-memory store in public APIs.
  • Loading branch information
ximon18 committed Mar 26, 2024
1 parent c1e5e86 commit 47e003e
Show file tree
Hide file tree
Showing 21 changed files with 788 additions and 672 deletions.
18 changes: 9 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,6 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[[example]]
name = "read-zone"
required-features = ["zonefile"]

[[example]]
name = "query-zone"
required-features = ["unstable-zonetree"]

[[example]]
name = "download-rust-lang"
required-features = ["resolv"]
Expand All @@ -128,9 +120,17 @@ required-features = ["net", "unstable-client-transport"]
name = "server-transports"
required-features = ["net", "unstable-server-transport"]

[[example]]
name = "read-zone"
required-features = ["zonefile"]

[[example]]
name = "query-zone"
required-features = ["zonefile", "unstable-zonetree"]

[[example]]
name = "serve-zone"
required-features = ["net", "unstable-server-transport", "unstable-zonetree"]
required-features = ["zonefile", "net", "unstable-server-transport", "unstable-zonetree"]

# This example is commented out because it is difficult, if not impossible,
# when including the sqlx dependency, to make the dependency tree compatible
Expand Down
17 changes: 6 additions & 11 deletions examples/other/mysql-zone.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! MySQL backed zone serving minimal proof of concept.
//
// This example extends `domain` with a new `ZoneData` impl adding support for
// This example extends `domain` with a new `ZoneStore` impl adding support for
// MySQL backed zones. This demonstration only implements the `ReadableZone`
// trait, it doesn't implement the `WritableZone` trait, so database access is
// read-only. Write access could be implemented, it just isn't in this
Expand Down Expand Up @@ -123,8 +123,8 @@ use domain::base::scan::IterScanner;
use domain::base::{Dname, Rtype, Ttl};
use domain::rdata::ZoneRecordData;
use domain::zonetree::{
Answer, OutOfZone, ReadableZone, Rrset, SharedRrset, StoredDname,
Version, VersionMarker, WalkOp, WriteableZone, Zone, ZoneData, ZoneSet,
Answer, OutOfZone, ReadableZone, Rrset, SharedRrset, StoredDname, WalkOp,
WriteableZone, Zone, ZoneSet, ZoneStore,
};
use parking_lot::RwLock;
use sqlx::Row;
Expand Down Expand Up @@ -195,9 +195,9 @@ impl DatabaseNode {
}
}

//--- impl ZoneData
//--- impl ZoneStore

impl ZoneData for DatabaseNode {
impl ZoneStore for DatabaseNode {
fn class(&self) -> Class {
Class::In
}
Expand All @@ -206,10 +206,7 @@ impl ZoneData for DatabaseNode {
&self.apex_name
}

fn read(
self: Arc<Self>,
_current: (Version, Arc<VersionMarker>),
) -> Box<dyn ReadableZone> {
fn read(self: Arc<Self>) -> Box<dyn ReadableZone> {
Box::new(DatabaseReadZone::new(
self.db_pool.clone(),
self.apex_name.clone(),
Expand All @@ -218,8 +215,6 @@ impl ZoneData for DatabaseNode {

fn write(
self: Arc<Self>,
_version: Version,
_zone_versions: Arc<RwLock<domain::zonetree::ZoneVersions>>,
) -> Pin<Box<dyn Future<Output = Box<dyn WriteableZone>>>> {
todo!()
}
Expand Down
4 changes: 2 additions & 2 deletions examples/query-zone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use domain::base::{ParsedRecord, Record};
use domain::rdata::ZoneRecordData;
use domain::zonefile::inplace;
use domain::zonetree::{Answer, Rrset};
use domain::zonetree::{Zone, ZoneSet};
use domain::zonetree::{Zone, ZoneTree};
use octseq::Parser;
use tracing_subscriber::EnvFilter;

Expand Down Expand Up @@ -52,7 +52,7 @@ fn main() {
});

// Go!
let mut zones = ZoneSet::new();
let mut zones = ZoneTree::new();

for (zone_file_path, mut zone_file) in zone_files {
if verbosity != Verbosity::Quiet {
Expand Down
10 changes: 4 additions & 6 deletions examples/read-zone.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
//! Reads a zone file.

use std::env;
use std::fs::File;
use std::process::exit;
use std::time::SystemTime;

use domain::zonefile::inplace::Entry;
use domain::zonefile::inplace::Zonefile;

fn main() {
use domain::zonefile::inplace::Zonefile;
use std::env;
use std::fs::File;
use std::time::SystemTime;

let mut args = env::args();
let prog_name = args.next().unwrap(); // SAFETY: O/S always passes our name as the first argument.
let zone_files: Vec<_> = args.collect();
Expand Down Expand Up @@ -44,7 +43,6 @@ fn main() {
eprintln!(" Error: {err}");
if let Some(entry) = &last_entry {
if let Entry::Record(record) = &entry {
// let record = record.unwrap().into_record::<ZoneRecordData<_, ParsedDname<_>>>().unwrap().unwrap();
eprintln!(
"\nThe last record read was:\n{record}."
);
Expand Down
10 changes: 5 additions & 5 deletions examples/serve-zone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use domain::net::server::stream::StreamServer;
use domain::net::server::util::{mk_builder_for_target, service_fn};
use domain::zonefile::inplace;
use domain::zonetree::{Answer, Rrset};
use domain::zonetree::{Zone, ZoneSet};
use domain::zonetree::{Zone, ZoneTree};
use octseq::OctetsBuilder;
use std::future::{pending, ready, Future};
use std::io::BufReader;
Expand All @@ -49,7 +49,7 @@ async fn main() {
.ok();

// Populate a zone tree with test data
let mut zones = ZoneSet::new();
let mut zones = ZoneTree::new();
let zone_bytes = include_bytes!("../test-data/zonefiles/nsd-example.txt");
let mut zone_bytes = BufReader::new(&zone_bytes[..]);

Expand Down Expand Up @@ -79,7 +79,7 @@ async fn main() {
#[allow(clippy::type_complexity)]
fn my_service(
msg: Request<Message<Vec<u8>>>,
zones: Arc<ZoneSet>,
zones: Arc<ZoneTree>,
) -> Result<
Transaction<
Vec<u8>,
Expand All @@ -105,7 +105,7 @@ fn my_service(

async fn handle_non_axfr_request(
msg: Request<Message<Vec<u8>>>,
zones: Arc<ZoneSet>,
zones: Arc<ZoneTree>,
) -> Result<CallResult<Vec<u8>, Vec<u8>>, ServiceError> {
let question = msg.message().sole_question().unwrap();
let zone = zones
Expand All @@ -127,7 +127,7 @@ async fn handle_non_axfr_request(

async fn handle_axfr_request(
msg: Request<Message<Vec<u8>>>,
zones: Arc<ZoneSet>,
zones: Arc<ZoneTree>,
) -> TransactionStream<Result<CallResult<Vec<u8>, Vec<u8>>, ServiceError>> {
let mut stream = TransactionStream::default();

Expand Down
53 changes: 53 additions & 0 deletions src/zonefile/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//------------ ZoneCutError --------------------------------------------------

use std::fmt::Display;

#[derive(Clone, Copy, Debug)]
pub enum ZoneCutError {
OutOfZone,
ZoneCutAtApex,
}

impl From<OutOfZone> for ZoneCutError {
fn from(_: OutOfZone) -> ZoneCutError {
ZoneCutError::OutOfZone
}
}

impl Display for ZoneCutError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
ZoneCutError::OutOfZone => write!(f, "Out of zone"),
ZoneCutError::ZoneCutAtApex => write!(f, "Zone cut at apex"),
}
}
}

//----------- CnameError -----------------------------------------------------

#[derive(Clone, Copy, Debug)]
pub enum CnameError {
OutOfZone,
CnameAtApex,
}

impl From<OutOfZone> for CnameError {
fn from(_: OutOfZone) -> CnameError {
CnameError::OutOfZone
}
}

impl Display for CnameError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
CnameError::OutOfZone => write!(f, "Out of zone"),
CnameError::CnameAtApex => write!(f, "CNAME at apex"),
}
}
}

//----------- OutOfZone ------------------------------------------------------

/// A domain name is not under the zone’s apex.
#[derive(Clone, Copy, Debug)]
pub struct OutOfZone;
2 changes: 2 additions & 0 deletions src/zonefile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
#![cfg(feature = "zonefile")]
#![cfg_attr(docsrs, doc(cfg(feature = "zonefile")))]

pub mod error;
pub mod inplace;
#[cfg(feature = "unstable-zonetree")]
pub mod parsed;
20 changes: 10 additions & 10 deletions src/zonefile/parsed.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
//! Importing from and exporting to a zonefiles.

use tracing::trace;

use crate::base::iana::{Class, Rtype};
use crate::base::name::FlattenInto;
use crate::base::ToDname;
use crate::rdata::ZoneRecordData;
use crate::zonetree::{
CnameError, Rrset, SharedRr, StoredDname, StoredRecord, ZoneBuilder,
ZoneCutError,
};
use core::convert::Infallible;
use std::collections::{BTreeMap, HashMap};
use std::fmt::Display;
use std::vec::Vec;

use tracing::trace;

use super::error::{CnameError, ZoneCutError};
use super::inplace::{self, Entry};

use crate::base::iana::{Class, Rtype};
use crate::base::name::FlattenInto;
use crate::base::ToDname;
use crate::rdata::ZoneRecordData;
use crate::zonetree::in_memory::ZoneBuilder;
use crate::zonetree::{Rrset, SharedRr, StoredDname, StoredRecord};

//------------ Zonefile ------------------------------------------------------

/// The content of a zonefile.
Expand Down
Loading

0 comments on commit 47e003e

Please sign in to comment.