From 8b752a4b4dc4c457fee4d5255160c109ae48369a Mon Sep 17 00:00:00 2001 From: Nanaloveyuki Date: Wed, 20 May 2026 08:51:48 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Extract=20sink=20models=20?= =?UTF-8?q?into=20utils=20subpackage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sinks.mbt | 87 +++++++-------------------------------- src/utils/sink_models.mbt | 66 +++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 71 deletions(-) create mode 100644 src/utils/sink_models.mbt diff --git a/src/sinks.mbt b/src/sinks.mbt index c355979..2d7090c 100644 --- a/src/sinks.mbt +++ b/src/sinks.mbt @@ -60,66 +60,14 @@ pub struct FileSink { rotation_failures : Ref[Int] } -pub struct FileRotation { - max_bytes : Int - max_backups : Int -} +pub type FileRotation = @utils.FileRotation -pub struct FileSinkState { - path : String - available : Bool - append : Bool - auto_flush : Bool - rotation : FileRotation? - open_failures : Int - write_failures : Int - flush_failures : Int - rotation_failures : Int -} +pub type FileSinkState = @utils.FileSinkState -pub struct FileSinkPolicy { - append : Bool - auto_flush : Bool - rotation : FileRotation? -} - -pub fn FileSinkPolicy::new( - append~ : Bool = true, - auto_flush~ : Bool = true, - rotation~ : FileRotation? = None, -) -> FileSinkPolicy { - { append, auto_flush, rotation } -} - -pub fn FileSinkState::new( - path : String, - available~ : Bool = false, - append~ : Bool = true, - auto_flush~ : Bool = true, - rotation~ : FileRotation? = None, - open_failures~ : Int = 0, - write_failures~ : Int = 0, - flush_failures~ : Int = 0, - rotation_failures~ : Int = 0, -) -> FileSinkState { - { - path, - available, - append, - auto_flush, - rotation, - open_failures, - write_failures, - flush_failures, - rotation_failures, - } -} +pub type FileSinkPolicy = @utils.FileSinkPolicy pub fn file_rotation(max_bytes : Int, max_backups~ : Int = 1) -> FileRotation { - { - max_bytes: if max_bytes <= 0 { 1 } else { max_bytes }, - max_backups: if max_backups <= 0 { 1 } else { max_backups }, - } + @utils.file_rotation(max_bytes, max_backups=max_backups) } pub fn native_files_supported() -> Bool { @@ -285,17 +233,17 @@ fn policy_rotation_equals_internal(left : FileRotation?, right : FileRotation?) } pub fn FileSink::state(self : FileSink) -> FileSinkState { - { - path: self.path, - available: self.is_available(), - append: self.append.val, - auto_flush: self.auto_flush.val, - rotation: self.rotation.val, - open_failures: self.open_failures.val, - write_failures: self.write_failures.val, - flush_failures: self.flush_failures.val, - rotation_failures: self.rotation_failures.val, - } + FileSinkState::new( + self.path, + available=self.is_available(), + append=self.append.val, + auto_flush=self.auto_flush.val, + rotation=self.rotation.val, + open_failures=self.open_failures.val, + write_failures=self.write_failures.val, + flush_failures=self.flush_failures.val, + rotation_failures=self.rotation_failures.val, + ) } pub fn FileSink::reopen(self : FileSink, append~ : Bool? = None) -> Bool { @@ -548,10 +496,7 @@ pub impl[S : Sink] Sink for BufferedSink[S] with write(self, rec) { } } -pub(all) enum QueueOverflowPolicy { - DropNewest - DropOldest -} +pub type QueueOverflowPolicy = @utils.QueueOverflowPolicy pub struct QueuedSink[S] { sink : S diff --git a/src/utils/sink_models.mbt b/src/utils/sink_models.mbt new file mode 100644 index 0000000..f0e9512 --- /dev/null +++ b/src/utils/sink_models.mbt @@ -0,0 +1,66 @@ +pub struct FileRotation { + max_bytes : Int + max_backups : Int +} + +pub struct FileSinkState { + path : String + available : Bool + append : Bool + auto_flush : Bool + rotation : FileRotation? + open_failures : Int + write_failures : Int + flush_failures : Int + rotation_failures : Int +} + +pub struct FileSinkPolicy { + append : Bool + auto_flush : Bool + rotation : FileRotation? +} + +pub fn FileSinkPolicy::new( + append~ : Bool = true, + auto_flush~ : Bool = true, + rotation~ : FileRotation? = None, +) -> FileSinkPolicy { + { append, auto_flush, rotation } +} + +pub fn FileSinkState::new( + path : String, + available~ : Bool = false, + append~ : Bool = true, + auto_flush~ : Bool = true, + rotation~ : FileRotation? = None, + open_failures~ : Int = 0, + write_failures~ : Int = 0, + flush_failures~ : Int = 0, + rotation_failures~ : Int = 0, +) -> FileSinkState { + { + path, + available, + append, + auto_flush, + rotation, + open_failures, + write_failures, + flush_failures, + rotation_failures, + } +} + +pub fn file_rotation(max_bytes : Int, max_backups~ : Int = 1) -> FileRotation { + { + max_bytes: if max_bytes <= 0 { 1 } else { max_bytes }, + max_backups: if max_backups <= 0 { 1 } else { max_backups }, + } +} + +pub(all) enum QueueOverflowPolicy { + DropNewest + DropOldest +}