Repository: risingwavelabs/risingwave
File: src/meta/src/manager/diagnose.rs (Rust)

Code before:

    fn write_license(&self, s: &mut String) {
        use comfy_table::presets::ASCII_BORDERS_ONLY;
        use comfy_table::{ContentArrangement, Row, Table};

        let mut table = Table::new();
        table.load_preset(ASCII_BORDERS_ONLY);
        table.set_content_arrangement(ContentArrangement::Dynamic);
        table.set_header({
            let mut row = Row::new();
            row.add_cell("field".into());
            row.add_cell("value".into());
            row
        });

        match LicenseManager::get().license() {
            Ok(license) => {
                let fmt_option = |value: Option<u64>| match value {
                    Some(v) => v.to_string(),
                    None => "unlimited".to_owned(),
                };

                let expires_at = if license.exp == u64::MAX {
                    "never".to_owned()
                } else if license.exp > i64::MAX as u64 {
                    format!("invalid ({})", license.exp)
                } else {
                    let exp_i64 = license.exp as i64;
                    chrono::DateTime::<chrono::Utc>::from_timestamp(exp_i64, 0)
                        .map(|ts| ts.to_rfc3339())
                        .unwrap_or_else(|| format!("invalid ({})", license.exp))
                };

                let mut row = Row::new();
                row.add_cell("status".into());
                row.add_cell("valid".into());
                table.add_row(row);

                let mut row = Row::new();
                row.add_cell("tier".into());
                row.add_cell(format!("{:?}", license.tier).into());
                table.add_row(row);

                let mut row = Row::new();
                row.add_cell("expires_at".into());
                row.add_cell(expires_at.into());
                table.add_row(row);

                let mut row = Row::new();
                row.add_cell("rwu_limit".into());
                row.add_cell(fmt_option(license.rwu_limit.map(|v| v.get())).into());

Review comment (refactor):
The magic number check `i64::MAX` for timestamp validation is duplicated. Consider extracting the timestamp formatting logic into a helper function that handles both validation and formatting to improve code reusability and reduce duplication.

Code after:

    fn write_license(&self, s: &mut String) {
        use comfy_table::presets::ASCII_BORDERS_ONLY;
        use comfy_table::{ContentArrangement, Row, Table};

        let mut table = Table::new();
        table.load_preset(ASCII_BORDERS_ONLY);
        table.set_content_arrangement(ContentArrangement::Dynamic);
        table.set_header({
            let mut row = Row::new();
            row.add_cell("field".into());
            row.add_cell("value".into());
            row
        });

        match LicenseManager::get().license() {
            Ok(license) => {
                let fmt_option = |value: Option<u64>| match value {
                    Some(v) => v.to_string(),
                    None => "unlimited".to_owned(),
                };

                let expires_at = if license.exp == u64::MAX {
                    "never".to_owned()
                } else {
                    let exp_i64 = license.exp as i64;
                    chrono::DateTime::<chrono::Utc>::from_timestamp(exp_i64, 0)
                        .map(|ts| ts.to_rfc3339())
                        .unwrap_or_else(|| format!("invalid ({})", license.exp))
                };

                let mut row = Row::new();
                row.add_cell("status".into());
                row.add_cell("valid".into());
                table.add_row(row);

                let mut row = Row::new();
                row.add_cell("tier".into());
                row.add_cell(license.tier.name().into());
                table.add_row(row);

                let mut row = Row::new();
                row.add_cell("expires_at".into());
                row.add_cell(expires_at.into());
                table.add_row(row);

                let mut row = Row::new();
                row.add_cell("rwu_limit".into());
                row.add_cell(fmt_option(license.rwu_limit.map(|v| v.get())).into());
                table.add_row(row);